apieceoffruit

Unity Editor: Camera System Editor

Dec 22nd, 2021 (edited)
1,030
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.25 KB | None | 0 0
  1. using JasonStorey.Cameras;
  2. using UnityEditor;
  3. using UnityEngine;
  4.  
  5. namespace JasonStorey
  6. {
  7.     [CustomEditor(typeof(CameraSystem))]
  8.     public class CameraSystemEditor : CustomCameraSystemEditor<CameraSystem>
  9.     {
  10.         private void DrawAbout() => DrawAbout(nameof(CameraSystem),
  11.             "Camera System is the main controller of cameras. It sets a camera active, and uses a camera lookup to " +
  12.             "allow controls like 'GotoNext' etc");
  13.  
  14.         protected override void Draw()
  15.         {
  16.             _index = TabHeaders(_index, "Main", "Controls", "Cams","Events", "About");
  17.             TabPages(_index,DrawMain,DrawControls,DrawCameras,DrawEvents,DrawAbout);
  18.         }
  19.  
  20.         private void DrawCameras() =>_scroll = DrawCameraScroller("Cameras", ref _scroll,ref ColCount, Item.All);
  21.  
  22.         public void DrawEvents() => DrawEvents("CameraChanged");
  23.        
  24.         private void DrawControls()
  25.         {
  26.             StartBox();
  27.             StartRow();
  28.             for (int i = 0; i < 9; i++)
  29.             {
  30.                 if(Btn($"{i+1}")) Item.GotoIndex(i);
  31.             }
  32.             EndRow();
  33.             Space();
  34.             StartRow();
  35.             if(BtnSmall("First",SMALL_BUTTON_SIZE)) Item.GotoFirst();
  36.             if(BtnSmall("Prev",SMALL_BUTTON_SIZE)) Item.GotoPrev();
  37.             if(BtnSmall("Random",SMALL_BUTTON_SIZE)) Item.GotoRandom();
  38.             if(BtnSmall("Next",SMALL_BUTTON_SIZE)) Item.GotoNext();
  39.             if(BtnSmall("Last",SMALL_BUTTON_SIZE)) Item.GotoLast();
  40.             EndRow();
  41.  
  42.            
  43.             EndBox();
  44.         }
  45.  
  46.         private void DrawMain()
  47.         {
  48.             StartBox();
  49.            
  50.             DrawFixableDependency(x=>x.Lookup ==null,"Lookup","Camera system requires a CameraLookup reference to see the cameras.",TryAddLookup);
  51.             Field("ChangeWithSet");
  52.             EndBox();
  53.  
  54.             if (Application.isPlaying)
  55.                 DrawCurrentCamera();
  56.  
  57.         }
  58.  
  59.         private void DrawCurrentCamera()
  60.         {
  61.             StartBox();
  62.             if (Item.Current == null)
  63.             {
  64.                 Lbl("No Current Camera");
  65.             }
  66.             else
  67.             {
  68.                 DrawCameraTool(Item.Current);
  69.             }
  70.             EndBox();
  71.         }
  72.  
  73.         private void DrawCameraTool(SmartCamera cam)
  74.         {
  75.             var content = new GUIContent(cam.Name);
  76.             Header(content.text);
  77.  
  78.           DrawCamera(cam);
  79.  
  80.  
  81.           StartRow();
  82.           var c =  SceneViewFollow ? new Color(0.53f, 1f, 0.7f) : new Color(1f, 0.5f, 0.5f);
  83.           StartColor(c);
  84.           EditorGUILayout.Toggle(SceneViewFollow,GUILayout.Width(20));
  85.           EndColor();
  86.           if(Btn(SceneViewFollow ? "Stop Following" : "Follow Current Camera in Scene"))
  87.           {
  88.               SceneViewFollow = !SceneViewFollow;
  89.  
  90.           }
  91.          
  92.          
  93.  
  94.           EndRow();
  95.          
  96.           if(SceneViewFollow) SelectInScene(cam.transform);
  97.      
  98.         }
  99.  
  100.  
  101.         private bool SceneViewFollow;
  102.  
  103.         private void TryAddLookup()
  104.         {
  105.             var lu = Item.GetComponent<CameraLookup>();
  106.             Item.Lookup = lu != null ? lu : Item.gameObject.AddComponent<CameraLookup>();
  107.            
  108.         }
  109.  
  110.         protected override void DrawCamera(SmartCamera cam)
  111.         {
  112.             Color = new Color(0,0,0,0.1f);
  113.             StartBox();
  114.             StartRow();
  115.             Color = new Color(1,1,0,0.8f);
  116.             if (Btn(new GUIContent("?", "Where is this object?"), GUILayout.MaxWidth(120)))
  117.             {
  118.                 PingInInspector(cam);
  119.                 SelectInScene(cam.transform);
  120.             }
  121.             Color = cam.IsLive ? JSEditorColors.GoBtn : Color.white;
  122.             if (Btn(new GUIContent(cam.Name,"Activate this Camera"))) Item.SetCamera(cam);
  123.             Color = new Color(1f, 0.55f, 0.35f, 0.8f);
  124.             if (Btn(new GUIContent("goto","Select this object in the inspector"),GUILayout.MaxWidth(40))) SelectInInspector(cam);
  125.             EndRow();
  126.             EndBox();
  127.             Color = Color.white;
  128.         }
  129.        
  130.         private int _index;
  131.         private const float SMALL_BUTTON_SIZE = 60;
  132.         private int ColCount = 2;
  133.         private Vector2 _scroll;
  134.     }
  135. }
Add Comment
Please, Sign In to add comment