真Unity3d_控制SceneView镜头分享一些基于Editor扩展开发代码

2013年的代码,感受一下:

曾经的才是最美好的,俱往矣??

//1.简单获取Scene摄像机
var sceneCamera:Camera = SceneView.lastActiveSceneView.camera;


//2.比较完整的例子,实现点击移动Scene摄像机
//code form u3d forum by shawn
using UnityEngine;
using UnityEditor;
public class SceneViewCameraTest : ScriptableObject
{
    [MenuItem("Custom/Move Scene View Camera")]
    static public void MoveSceneViewCamera()
    {
        Vector3 position = SceneView.lastActiveSceneView.pivot;
        position.z -= 10.0f;
        SceneView.lastActiveSceneView.pivot = position;
        SceneView.lastActiveSceneView.Repaint();
    }
}

//3.获取所有scene中所有视角的摄像机
var sceneCameras:Camera[] = SceneView.GetAllSceneCameras();

//4.更多,WIKI上实现play mode联动更新scene摄像机的效果
http://wiki.unity3d.com/index.php/SceneViewCameraFollower

以上代码没有完全测试,不过基本不会太差,因为明显是大牛写的。。。。。。。

懂的人就懂了,不懂的人不需要了解

另外,分享另一个SceneView控制类

/* 同一个角度看模型和贴图,便于观察Pbr(v1.0)
 */
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
[CustomEditor(typeof(ModelMonitor))]
public class ModelMonitoEditor : Editor
{
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        if (GUILayout.Button("按钮"))
        {
            ModelMonitor mon = target as ModelMonitor;
            mon.ChangeModel();
        }
    }

    void OnSceneGUI()
    { 
        
    }
}
#endif
/// 
/// 同一个角度看模型和贴图,
/// 
public class ModelMonitor : MonoBehaviour
{
    public GameObject[] models;
    public int currModel;
    GameObject lasetModel;
    public void ChangeModel()
    {
        if (models == null || models.Length <= 1)
        { //TODO:单个物体,z做一个放大缩小动画
            if (models.Length == 1)
            { 
            
            }
            return;
        }
        currModel++;
        if (currModel >= models.Length)
        {
            currModel = 0;
            lasetModel = models[models.Length - 1];
        }
        else
        {
            lasetModel = models[currModel - 1];
        }
        Camera camera = null;
        foreach (var cam in SceneView.GetAllSceneCameras())
        {
            camera = cam;
            break;
        }
        if (camera == null) return;
        //Vector3 pos = camera.transform.position;
        //camera.transform.position = new Vector3(pos.x, pos.y + 1, pos.z);

        var moveDir = models[currModel].transform.position - lasetModel.transform.position;
        Vector3 position = SceneView.lastActiveSceneView.pivot;
        position += moveDir;
        SceneView.lastActiveSceneView.pivot = position;
        SceneView.lastActiveSceneView.Repaint();

    }
    // Start is called before the first frame update
    void Start()
    {
        
    }
}

 

 

你可能感兴趣的:(u3d代码分享,每天一点u3d)