Last active
February 13, 2021 21:25
-
-
Save AbrahamArmasCordero/54d88bee74f3d7c3c564d81c0d9f4fc0 to your computer and use it in GitHub Desktop.
Demo For Unity GUI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEditor; | |
| public class DemoUnityGUIFeature | |
| { | |
| //name to identify this feature also used in the tabs of DemoUnityWindow | |
| public string name = "F"; | |
| public DemoUnityGUIFeature() | |
| { | |
| } | |
| public DemoUnityGUIFeature(string _name) | |
| { | |
| name = _name; | |
| } | |
| public void DrawGUI() | |
| { | |
| //method to show when this feature is selected | |
| EditorGUILayout.LabelField($"Im you feature with Name: {name}"); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| //this is a monobehavior with all private variables with Unity Attributes, | |
| //System Attributes and Custom Attributes so we demostrate how to show everything i a custom editor | |
| //without any extra code for the editor | |
| public class DemoUnityGUIMonoBehaviour : MonoBehaviour | |
| { | |
| [SerializeField] | |
| private string myString = "Hello World"; | |
| [SerializeField] | |
| private bool groupEnabled; | |
| [SerializeField] | |
| private bool myBool = false; | |
| [Range(0,1)] | |
| [SerializeField] | |
| private float myFloat = 1.23f; | |
| [MinMaxRange(0f,10f)][SerializeField][Tooltip("Esto es una prueba")] | |
| private RangedFloat rangedFloat = new RangedFloat(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEditor; | |
| using UnityEngine; | |
| [CanEditMultipleObjects] | |
| [CustomEditor(typeof(DemoUnityGUIMonoBehaviour))] | |
| public class DemoUnityGUIMonoBehaviourEditor : Editor | |
| { | |
| //cached properties that we are going to get | |
| SerializedProperty myString; | |
| SerializedProperty groupEnabled; | |
| SerializedProperty myBool; | |
| SerializedProperty myFloat; | |
| SerializedProperty rangedFloat; | |
| //Cacheamos todas las propiedas serializadas | |
| public void OnEnable() | |
| { | |
| SetUp(); | |
| } | |
| // Setup the SerializedProperties. | |
| // si los nombres cambian esto peta | |
| //cacheamos las propiedades | |
| public void SetUp() | |
| { | |
| myString = serializedObject.FindProperty("myString"); | |
| groupEnabled = serializedObject.FindProperty("groupEnabled"); | |
| myBool = serializedObject.FindProperty("myBool"); | |
| myFloat = serializedObject.FindProperty("myFloat"); | |
| rangedFloat = serializedObject.FindProperty("rangedFloat"); | |
| } | |
| public override void OnInspectorGUI() | |
| { | |
| //need to calls this at the start of the onInspectorGUI because READ THIS: https://docs.unity3d.com/ScriptReference/SerializedObject.html | |
| serializedObject.Update(); | |
| myString.stringValue = EditorGUILayout.TextField("Text Field", myString.stringValue); | |
| groupEnabled.boolValue = EditorGUILayout.BeginToggleGroup("Optional Settings", groupEnabled.boolValue); | |
| myBool.boolValue = EditorGUILayout.Toggle("Toggle", myBool.boolValue); | |
| if (myBool.boolValue) | |
| { | |
| //THIS SHOWS ALL THE ATTRIBUTES WITH THE PROPERTY | |
| EditorGUILayout.PropertyField(myFloat); | |
| EditorGUILayout.PropertyField(rangedFloat); | |
| } | |
| EditorGUILayout.EndToggleGroup(); | |
| //need to calls this at the end of the onInspectorGUI | |
| serializedObject.ApplyModifiedProperties(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections.Generic; | |
| using UnityEditor; | |
| using UnityEngine; | |
| public class DemoUnityWindow : EditorWindow | |
| { | |
| //random MonoBehaviour that we pick up or create in the scene for demostration | |
| private DemoUnityGUIMonoBehaviour o = null; | |
| // if the monobehavior wasnt found create and delete after window closes | |
| private bool monobehaviourWasNull = false; | |
| //current selected tab | |
| private int selelectedTab = 0; | |
| //random class that has a DrawGUI method for encapsualting logic | |
| private List<DemoUnityGUIFeature> features = new List<DemoUnityGUIFeature>(); | |
| //names of the tabs filled with the names of the llist above | |
| private List<string> featureNames = new List<string>(); | |
| [MenuItem( "Window/My Window" )] | |
| public static void ShowWindow ( ) | |
| { | |
| GetWindow<DemoUnityWindow>(); | |
| } | |
| private void Awake() | |
| { | |
| o = FindObjectOfType<DemoUnityGUIMonoBehaviour>(); | |
| if(o == null) | |
| { | |
| monobehaviourWasNull = true; | |
| GameObject a = new GameObject(); | |
| o = a.AddComponent<DemoUnityGUIMonoBehaviour>(); | |
| } | |
| //fill list used for the window GUI | |
| features.AddRange(new List<DemoUnityGUIFeature> | |
| { | |
| new DemoUnityGUIFeature("Scenes"), | |
| new DemoUnityGUIFeature("Visualization"), | |
| new DemoUnityGUIFeature("Creation") | |
| }); | |
| features.ForEach(x => featureNames.Add(x.name)); | |
| } | |
| private void OnGUI ( ) | |
| { | |
| //showing a Label | |
| GUILayout.Label("Base Settings", EditorStyles.boldLabel); | |
| //Drawing Tabs with the names of the feature and selecting a tab | |
| selelectedTab = GUILayout.Toolbar(selelectedTab, featureNames.ToArray()); | |
| //draw the selected tab under this toolbar | |
| if(selelectedTab >= 0 && selelectedTab < featureNames.Count) | |
| { | |
| features[selelectedTab].DrawGUI(); | |
| } | |
| //draw the editor of the monobehaviour cached from scene also caches if it has a custom editor | |
| if (o != null) | |
| { | |
| //gets the editor for that Unity.Object and shows it | |
| Editor.CreateEditor(o).OnInspectorGUI(); | |
| } | |
| } | |
| private void OnDestroy() | |
| { | |
| //deletes the monobehaviour if it was created fro demostration | |
| if (monobehaviourWasNull) | |
| { | |
| DestroyImmediate(o.gameObject); | |
| monobehaviourWasNull = false; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment