版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/CJB_King/article/details/102852607
使用编辑器:
Unity2018.4.2
工具来由
当Unity场景中的物体太多,无法在scene视图定位到游戏中看到的物体时,可以通过使用SceneView对当前视图进行定位
应用举例
代码语言:javascript复制using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class SyncSceneView : MonoBehaviour
{
#if UNITY_EDITOR
private SceneView view = null;
// Use this for initialization
void Awake()
{
view = SceneView.lastActiveSceneView;
//view.m_RenderMode = DrawCameraMode.Mipmaps;
SceneView.CameraMode cameraMode = view.cameraMode;
cameraMode.drawMode = DrawCameraMode.Mipmaps;
}
private void LateUpdate()
{
if (view != null)
{
view.LookAt(transform.position, transform.rotation, 0f);
}
}
private void OnDestroy()
{
if (view != null)
{
view.LookAt(transform.position, transform.rotation, 5f);
}
}
public void CloseTool()
{
//view.m_RenderMode = (DrawCameraMode)0;
SceneView.CameraMode cameraMode = view.cameraMode;
cameraMode.drawMode = DrawCameraMode.Textured;
view = null;
Destroy(this);
}
#endif
}
工具编辑
代码语言:javascript复制using UnityEngine;
using UnityEditor;
using UnityEngine.SceneManagement;
using UnityEngine.Events;
public class MipmapTool {
static SyncSceneView curViewTool;
static Camera mainCamera;
static UnityAction<Scene, LoadSceneMode> onSceneLoaded = new UnityAction<Scene, LoadSceneMode>((scene, sceneMode) => AddSyncViewToMainCamera());
static Action<PlayModeStateChange> onPlayStateChanged = OnPlayModeChanged;
[MenuItem("MipmapTool/Open")]
static void OpenMipmapTool()
{
SceneManager.sceneLoaded = onSceneLoaded;
EditorApplication.playModeStateChanged = onPlayStateChanged;
AddSyncViewToMainCamera();
}
[MenuItem("MipmapTool/Open", true)]
static bool CheckOpen()
{
return curViewTool == null && EditorApplication.isPlaying == true;
}
[MenuItem("MipmapTool/Close")]
static void CloseMipmapTool()
{
if(curViewTool != null)
{
curViewTool.CloseTool();
curViewTool = null;
}
else
{
SceneView view = SceneView.lastActiveSceneView;
//view.m_RenderMode = DrawCameraMode.Textured;
SceneView.CameraMode cameraMode = view.cameraMode;
cameraMode.drawMode = DrawCameraMode.Textured;
}
SceneManager.sceneLoaded -= onSceneLoaded;
//EditorApplication.playmodeStateChanged -= onPlayStateChanged;
EditorApplication.playModeStateChanged -= onPlayStateChanged;
}
[MenuItem("MipmapTool/CLose", true)]
static bool CheckClose()
{
return curViewTool != null;
}
static void AddSyncViewToMainCamera()
{
mainCamera = Camera.main;
if (mainCamera == null)
{
Debug.LogError("场景中没有主摄像机,无法开启MipmapTool!!!");
return;
}
SyncSceneView ssv = mainCamera.gameObject.GetComponent<SyncSceneView>();
if (ssv == null)
{
ssv = mainCamera.gameObject.AddComponent<SyncSceneView>();
}
curViewTool = ssv;
}
static void OnPlayModeChanged(PlayModeStateChange playMode)
{
if(!EditorApplication.isPlaying)
{
CloseMipmapTool();
}
}
}