LoginSignup
6
5

More than 5 years have passed since last update.

【エディタ拡張】カメラ一覧(CameraChecker)

Last updated at Posted at 2017-03-20

カメラを複数使うときに、どのカメラを使用しているか分かりやすいようにしてみました。

image

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 ();
    }
}
6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5