使用编辑器:
Unity2018.4.2
工具来由
当Unity场景中的物体太多,无法在scene视图定位到游戏中看到的物体时,可以通过使用SceneView对当前视图进行定位
应用举例
using UnityEngine
;
#if UNITY_EDITOR
using UnityEditor
;
#endif
public class SyncSceneView : MonoBehaviour
{
#if UNITY_EDITOR
private SceneView view
= null;
void Awake()
{
view
= SceneView
.lastActiveSceneView
;
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()
{
SceneView.CameraMode cameraMode
= view
.cameraMode
;
cameraMode
.drawMode
= DrawCameraMode
.Textured
;
view
= null;
Destroy(this);
}
#endif
}
工具编辑
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
;
SceneView.CameraMode cameraMode
= view
.cameraMode
;
cameraMode
.drawMode
= DrawCameraMode
.Textured
;
}
SceneManager
.sceneLoaded
-= onSceneLoaded
;
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();
}
}
}
转载请注明原文地址: https://mac.8miu.com/read-500807.html