unity中按格子移动物体

可以针对特定某个物体,写个脚本:

using UnityEngine;

[ExecuteInEditMode]
public class EditModeGridSnap : MonoBehaviour
{
  public float snapValue = 0.5f;
  private bool snapX = true;
  private bool snapY = true;
  private bool snapZ = true;

  private void Update()
  {
    if (Application.isPlaying)
      return;
    float num = 1f / this.snapValue;
    this.transform.localPosition = new Vector3(!this.snapX ? this.transform.localPosition.x : Mathf.Round(this.transform.localPosition.x * num) / num, !this.snapY ? this.transform.localPosition.y : Mathf.Round(this.transform.localPosition.y * num) / num, !this.snapZ ? this.transform.localPosition.z : Mathf.Round(this.transform.localPosition.z * num) / num);
  }
}

你可能感兴趣的:(unity,editor)