Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using UnityEditor;
- using UnityEngine;
- namespace JasonStorey
- {
- public abstract class CustomEditor<T> : Editor where T : Component
- {
- protected T Item;
- protected virtual void OnEnable()
- {
- Item = (T) target;
- }
- protected abstract void Draw();
- public override void OnInspectorGUI()
- {
- serializedObject.Update();
- Draw();
- serializedObject.ApplyModifiedProperties();
- }
- protected void SelectInScene(Transform camTransform)
- {
- SceneView sceneCamera = SceneView.lastActiveSceneView;
- sceneCamera.LookAt(camTransform.position);
- }
- protected void EndColor() => Color = _oldColor;
- private Color _oldColor;
- protected void StartColor(Color col)
- {
- _oldColor = Color;
- Color = col;
- }
- protected void DrawFixableDependency(Func<T,bool> item,string field,string message,Action fix)
- {
- Space();
- if (item.Invoke(Item))
- {
- Field(field);
- Info(message,JSEditorColors.Warning);
- Color = JSEditorColors.GoBtn;
- if (Btn("Fix")) fix?.Invoke();
- Color = JSEditorColors.Editor;
- }
- else
- {
- Field(field);
- }
- }
- protected void TabPages(int index, params Action[] drawFunctions)
- {
- if (index > -1 && index < drawFunctions.Length)
- drawFunctions[index].Invoke();
- }
- protected Vector2 BeginScroll(Vector2 scroll,float min = 50,float max = 200) => EditorGUILayout.BeginScrollView(scroll, GUILayout.MaxHeight(max), GUILayout.MinHeight(max));
- protected void EndScroll() => EditorGUILayout.EndScrollView();
- protected void SelectInInspector(Component component) => Selection.activeObject = component;
- protected void PingInInspector(Component component) => EditorGUIUtility.PingObject(component);
- protected int TabHeaders(int tab,params string[] tabNames)=>GUILayout.Toolbar (tab, tabNames);
- protected void StartRow() => EditorGUILayout.BeginHorizontal();
- protected void EndRow() => EditorGUILayout.EndHorizontal();
- protected void StartColumn() => EditorGUILayout.BeginVertical();
- protected void EndColumn() => EditorGUILayout.EndVertical();
- protected bool Ask(string title, string message, string yes, string no) =>
- EditorUtility.DisplayDialog(title, message, yes, no);
- protected bool AskToDelete(string thing,string message) => Ask($"Delete {thing}", message, $"Yes. Delete {thing}", "No. Do Nothing");
- protected void MaxEditorSize(float size) => EditorGUILayout.BeginVertical(GUILayout.MinHeight(size));
- protected void SetSize(float size) => EditorGUILayout.BeginVertical(GUILayout.Height(size));
- void EndSize() => EditorGUILayout.EndVertical();
- protected void EndSetSize() => EditorGUILayout.EndVertical();
- protected void Info(string message) => EditorGUILayout.HelpBox(message, MessageType.None);
- protected void Info(string message,Color col)
- {
- var c = Color;
- Color = col;
- EditorGUILayout.HelpBox(message, MessageType.None);
- Color = c;
- }
- protected void StartBox(string style = BOXSTYLE_GROUP, params GUILayoutOption[] options) => EditorGUILayout.BeginVertical(style,options);
- protected void EndBox() => EditorGUILayout.EndVertical();
- protected Color Color
- {
- get => GUI.color;
- set => GUI.color = value;
- }
- protected bool BtnSmall(string lbl,float size) => GUILayout.Button(lbl, GUILayout.MaxWidth(size));
- protected bool Btn(string lbl) => GUILayout.Button(lbl);
- protected bool Btn(string lbl,string tooltip) => GUILayout.Button(new GUIContent(lbl,tooltip));
- protected bool Btn(string lbl, params GUILayoutOption[] options) => Btn(new GUIContent(lbl), options);
- protected bool Btn(GUIContent content,params GUILayoutOption[] options) => GUILayout.Button(content,options);
- protected void Lbl(string lbl,params GUILayoutOption[] options) => EditorGUILayout.LabelField(lbl,options);
- protected void Lbl(string lbl, string tooltip, params GUILayoutOption[] options) =>
- EditorGUILayout.LabelField(new GUIContent(lbl, tooltip), options);
- protected void Header(string lbl) => EditorGUILayout.LabelField(lbl, EditorStyles.boldLabel);
- protected bool IsEditor => !Application.isPlaying;
- protected void Space() => EditorGUILayout.Space();
- protected void Field(string fld) => EditorGUILayout.PropertyField(serializedObject.FindProperty(fld));
- protected const string BOXSTYLE_HELP = "HelpBox";
- protected const string BOXSTYLE_GROUP = "GroupBox";
- }
- }
Add Comment
Please, Sign In to add comment