【Unity】Scene视角摄像机跟随指定物体

  在项目运行中,想要编辑快速移动的物体会很难操作,Unity自带的定位功能只能定位一次,定位之后物体瞬间又移动出屏幕了。所以为了解决这个问题,可以利用编辑器代码对场景摄像机进行控制。
  代码如下,3D和2D项目都可以使用。

using UnityEditor;
using UnityEngine;

public class SceneViewLookAtTarget : MonoBehaviour
{
    
    
    [SerializeField] private Transform target;

    private void OnEnable()
    {
    
    
        SceneView.duringSceneGui += LookTarget;
    }

    private void LookTarget(SceneView sceneView)
    {
    
    
        Quaternion quaternion = sceneView.in2DMode ? Quaternion.identity : sceneView.rotation;
        sceneView.LookAt(target.position, quaternion, sceneView.size, sceneView.in2DMode, true);
    }

    private void OnDisable()
    {
    
    
        SceneView.duringSceneGui -= LookTarget;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_39955460/article/details/131084531