Advertisement
Ryoh

Custom Foldout Example

Nov 11th, 2022 (edited)
1,440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.16 KB | None | 0 0
  1. // Stats class
  2. using UnityEngine;
  3.  
  4. public class Stats : MonoBehaviour
  5. {
  6.     [SerializeField]
  7.     private int health;
  8. }
  9.  
  10. // Editor Class
  11. using UnityEditor;
  12.  
  13. // Note the use of CustomEditor.
  14. [CustomEditor(typeof(Stats))]
  15. public class StatsEditor : Editor // Note that we inherit from Editor.
  16. {
  17.     private static bool showGeneralSettings = true;
  18.  
  19.     public override void OnInspectorGUI()
  20.     {
  21.         // Create the foldout, and link it to our bool.
  22.         showGeneralSettings = EditorGUILayout.Foldout(showGeneralSettings, "General Settings");
  23.        
  24.         // Create a space to separate this foldout from the other potential variables
  25.         EditorGUILayout.Space();
  26.  
  27.         // This would make more sense as a switch when you add more settings.
  28.         // If the foldout is opened.
  29.         if (showGeneralSettings)
  30.         {
  31.             DisplayGeneralInfo();
  32.         }
  33.         // Apply changes made in the editor.
  34.         serializedObject.ApplyModifiedProperties();
  35.     }
  36.  
  37.     private void DisplayGeneralInfo()
  38.     {
  39.         // Find the health property for display.
  40.         EditorGUILayout.PropertyField(serializedObject.FindProperty("health"));
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement