[편집기 확장] 카메라 리스트(카메라 리스트)

14307 단어 Unity
여러 개의 카메라를 사용할 때 어떤 카메라를 사용했는지 알아봐라.

Assets/Editor/CameraChecker.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class CameraChecker {

    public class CameraComparer : IComparer
    {
        public int Compare(object x, object y) {
            Camera c1 = x as Camera;
            Camera c2 = y as Camera;
            if (c1 == null && c2 == null) {
                return 0;
            }
            if (c1 == null) {
                return -1;
            }
            if (c2 == null) {
                return 1;
            }
            return c1.depth.CompareTo (c2.depth);
        }
    }

    public class BackgroundColorScope : GUI.Scope
    {
        private readonly Color color;
        public BackgroundColorScope(Color color) {
            this.color = GUI.backgroundColor;
            GUI.backgroundColor = color;
        }
        protected override void CloseScope() {
            GUI.backgroundColor = color;
        }
    }

    static int windowID = 1001;
    static Rect windowRect = new Rect(10, 10, 200, 100);
    static CameraComparer comp = new CameraComparer();

    [MenuItem("Tools/CameraChecker")]
    static void SwitchCameraChecker() {
        string menuPath = "Tools/CameraChecker";
        bool val = Menu.GetChecked(menuPath);
        Menu.SetChecked(menuPath, !val);
        SceneView.RepaintAll ();
    }

    [InitializeOnLoadMethod]
    static void InitCameraChecker() {
        SceneView.onSceneGUIDelegate += OnGUI;
    }

    static void OnGUI(SceneView sceneView ) {
        if (!Menu.GetChecked ("Tools/CameraChecker")) {
            return;
        }

        Handles.BeginGUI ();
        windowRect = GUILayout.Window (windowID, windowRect, WindowFunction, "CameraChecker");
        Handles.EndGUI ();
    }

    static void WindowFunction(int _windowID) {
        Camera[] cameras = Object.FindObjectsOfType<Camera> ();
        System.Array.Sort (cameras, comp);
        System.Array.Reverse (cameras);
        if (cameras != null) {
            for (int i = 0; i < cameras.Length; i++) {
                string cameraName = cameras [i].name;
                if (cameraName.Length > 10) {
                    cameraName = cameraName.Substring (0, 10);
                }
                string txt = string.Format("D({0}) {1} [{2}]", cameras[i].depth, cameraName, (cameras [i].enabled ? "ON" : "OFF"));
                if (cameras [i].gameObject == Selection.activeGameObject) {
                    using (new BackgroundColorScope (Color.green)) {
                        if (GUILayout.Button (txt, GUILayout.Width (200))) {
                            Selection.activeGameObject = cameras [i].gameObject;
                        }
                    }
                } else {
                    if (GUILayout.Button (txt, GUILayout.Width (200))) {
                        Selection.activeGameObject = cameras [i].gameObject;
                    }
                }
            }
        }
        GUI.DragWindow ();
    }
}

좋은 웹페이지 즐겨찾기