apieceoffruit

Unity Editor: Custom Editor Base

Dec 22nd, 2021 (edited)
959
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.09 KB | None | 0 0
  1. using System;
  2. using UnityEditor;
  3. using UnityEngine;
  4.  
  5. namespace JasonStorey
  6. {
  7.     public abstract class CustomEditor<T> : Editor where T : Component
  8.     {
  9.         protected T Item;
  10.        
  11.         protected virtual void OnEnable()
  12.         {
  13.             Item = (T) target;
  14.         }
  15.  
  16.         protected abstract void Draw();
  17.  
  18.         public override void OnInspectorGUI()
  19.         {
  20.             serializedObject.Update();
  21.             Draw();
  22.             serializedObject.ApplyModifiedProperties();
  23.         }
  24.  
  25.         protected void SelectInScene(Transform camTransform)
  26.         {
  27.             SceneView sceneCamera = SceneView.lastActiveSceneView;
  28.             sceneCamera.LookAt(camTransform.position);
  29.         }
  30.  
  31.         protected void EndColor() => Color = _oldColor;
  32.  
  33.         private Color _oldColor;
  34.  
  35.         protected void StartColor(Color col)
  36.         {
  37.             _oldColor = Color;
  38.             Color = col;
  39.         }
  40.  
  41.        
  42.  
  43.         protected void DrawFixableDependency(Func<T,bool> item,string field,string message,Action fix)
  44.         {
  45.             Space();
  46.             if (item.Invoke(Item))
  47.             {
  48.                 Field(field);
  49.                 Info(message,JSEditorColors.Warning);
  50.                 Color = JSEditorColors.GoBtn;
  51.                 if (Btn("Fix")) fix?.Invoke();
  52.                 Color = JSEditorColors.Editor;
  53.             }
  54.             else
  55.             {
  56.                 Field(field);
  57.             }
  58.         }
  59.  
  60.         protected void TabPages(int index, params Action[] drawFunctions)
  61.         {
  62.             if (index > -1 && index < drawFunctions.Length)
  63.                 drawFunctions[index].Invoke();
  64.         }
  65.        
  66.         protected Vector2 BeginScroll(Vector2 scroll,float min = 50,float max = 200) => EditorGUILayout.BeginScrollView(scroll, GUILayout.MaxHeight(max), GUILayout.MinHeight(max));
  67.         protected void EndScroll() => EditorGUILayout.EndScrollView();
  68.  
  69.  
  70.         protected void SelectInInspector(Component component) => Selection.activeObject = component;
  71.         protected void PingInInspector(Component component) => EditorGUIUtility.PingObject(component);
  72.        
  73.         protected int TabHeaders(int tab,params string[] tabNames)=>GUILayout.Toolbar (tab, tabNames);
  74.        
  75.         protected void StartRow() => EditorGUILayout.BeginHorizontal();
  76.         protected void EndRow() => EditorGUILayout.EndHorizontal();
  77.         protected void StartColumn() => EditorGUILayout.BeginVertical();
  78.         protected void EndColumn() => EditorGUILayout.EndVertical();
  79.  
  80.         protected bool Ask(string title, string message, string yes, string no) =>
  81.             EditorUtility.DisplayDialog(title, message, yes, no);
  82.  
  83.         protected bool AskToDelete(string thing,string message) => Ask($"Delete {thing}", message, $"Yes. Delete {thing}", "No. Do Nothing");
  84.        
  85.        
  86.         protected void MaxEditorSize(float size) => EditorGUILayout.BeginVertical(GUILayout.MinHeight(size));
  87.  
  88.         protected void SetSize(float size) => EditorGUILayout.BeginVertical(GUILayout.Height(size));
  89.         void EndSize() => EditorGUILayout.EndVertical();
  90.         protected void EndSetSize() => EditorGUILayout.EndVertical();
  91.  
  92.         protected void Info(string message) => EditorGUILayout.HelpBox(message, MessageType.None);
  93.  
  94.         protected void Info(string message,Color col)
  95.         {
  96.             var c = Color;
  97.             Color = col;
  98.             EditorGUILayout.HelpBox(message, MessageType.None);
  99.             Color = c;
  100.         }
  101.  
  102.         protected void StartBox(string style = BOXSTYLE_GROUP, params GUILayoutOption[] options) => EditorGUILayout.BeginVertical(style,options);
  103.         protected void EndBox() => EditorGUILayout.EndVertical();
  104.  
  105.         protected Color Color
  106.         {
  107.             get => GUI.color;
  108.             set => GUI.color = value;
  109.         }
  110.        
  111.         protected bool BtnSmall(string lbl,float size) => GUILayout.Button(lbl, GUILayout.MaxWidth(size));
  112.         protected bool Btn(string lbl) => GUILayout.Button(lbl);
  113.         protected bool Btn(string lbl,string tooltip) => GUILayout.Button(new GUIContent(lbl,tooltip));
  114.  
  115.         protected bool Btn(string lbl, params GUILayoutOption[] options) => Btn(new GUIContent(lbl), options);
  116.         protected bool Btn(GUIContent content,params GUILayoutOption[] options) => GUILayout.Button(content,options);
  117.         protected void Lbl(string lbl,params GUILayoutOption[] options) => EditorGUILayout.LabelField(lbl,options);
  118.  
  119.         protected void Lbl(string lbl, string tooltip, params GUILayoutOption[] options) =>
  120.             EditorGUILayout.LabelField(new GUIContent(lbl, tooltip), options);
  121.  
  122.         protected void Header(string lbl) => EditorGUILayout.LabelField(lbl, EditorStyles.boldLabel);
  123.  
  124.         protected bool IsEditor => !Application.isPlaying;
  125.        
  126.         protected void Space() => EditorGUILayout.Space();
  127.         protected void Field(string fld) => EditorGUILayout.PropertyField(serializedObject.FindProperty(fld));
  128.  
  129.         protected const string BOXSTYLE_HELP = "HelpBox";
  130.         protected const string BOXSTYLE_GROUP = "GroupBox";
  131.  
  132.     }
  133. }
Add Comment
Please, Sign In to add comment