Search Unity

Unity Custom Editor

Discussion in 'Immediate Mode GUI (IMGUI)' started by xJavier, Aug 27, 2014.

  1. xJavier

    xJavier

    Joined:
    May 10, 2014
    Posts:
    8
    Hi,

    I'm very new to Unity Custom Editor, this is the problem, I want to display a variable only if another is set to true, someone know how can I achieve that?

    let's say I have:

    public bool canStackJump;
    public int howManyJumpsCanStack;

    I want to hide "howManyJumpsCanStack" from the inspector unless "canStackJump" is set to true.
    any ideas?

    I'm working with c#, thanks in advanced.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4.  
    5. [CustomEditor(typeof(PlayerController))]
    6. [CanEditMultipleObjects]
    7. public class PlayerControllerUnityEditor : Editor {
    8.  
    9.  
    10.  
    11.     SerializedProperty
    12.         canStackJump,
    13.         howManyJumpStacks;
    14.    
    15.     void OnEnable() {
    16.         // Setup serialized property
    17.         canStackJump = serializedObject.FindProperty ("canStackJump");
    18.     }
    19.  
    20.     public override void OnInspectorGUI() {
    21.         DrawDefaultInspector();
    22.         serializedObject.Update ();
    23.  
    24.         if (canStackJump.boolValue) {
    25.             EditorGUILayout.LabelField("Level", canStackJump.boolValue.ToString());
    26.             //Somthing here to display the other property//
    27.         }
    28.         serializedObject.ApplyModifiedProperties ();
    29.     }
    30. }
     
  2. xJavier

    xJavier

    Joined:
    May 10, 2014
    Posts:
    8
    Nevermind,, I was able to do it, here's the code :


    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4.  
    5. [CustomEditor(typeof(PlayerController))]
    6. [CanEditMultipleObjects]
    7. public class PlayerControllerUnityEditor : Editor {
    8.  
    9.     PlayerController _playerController;
    10.     bool stackJump;
    11.  
    12.    
    13.     void OnEnable() {
    14.         // Setup serialized property
    15.         _playerController = (PlayerController)target;
    16.  
    17.  
    18.     }
    19.  
    20.     public override void OnInspectorGUI() {
    21.         DrawDefaultInspector();
    22.         //base.OnInspectorGUI ();
    23. //        EditorGUILayout.BeginVertical ();
    24. //        GUILayout.Label ("Player Properties");
    25. //        EditorGUILayout.BeginHorizontal ();
    26. //        GUILayout.Label ("JumpForce");
    27. //        _playerController.jumpForce = EditorGUILayout.Slider(_playerController,
    28. //        EditorGUILayout.EndVertical ();
    29.         EditorGUILayout.BeginFadeGroup (1);
    30.             stackJump = EditorGUILayout.BeginToggleGroup ("Stack Jump",stackJump);
    31.             EditorGUILayout.BeginHorizontal ();
    32.                 _playerController.canStackJump = stackJump;
    33.             EditorGUILayout.EndHorizontal ();
    34.             EditorGUILayout.BeginVertical ();
    35.                 if (_playerController.canStackJump) {
    36.                     EditorGUILayout.BeginHorizontal();
    37.                     GUILayout.Label("How Many Jump Stacks");
    38.                     _playerController.howManyJumpStacks = EditorGUILayout.IntSlider(_playerController.howManyJumpStacks,1,5);
    39.                     EditorGUILayout.EndHorizontal();
    40.                     //Somthing here to display the other property//
    41.                 }
    42.             EditorGUILayout.EndVertical ();
    43.             EditorGUILayout.EndToggleGroup ();
    44.         EditorGUILayout.EndFadeGroup ();
    45.  
    46.         //base.OnInspectorGUI ();
    47.  
    48.         //serializedObject.ApplyModifiedProperties ();
    49.         if (GUI.changed) {
    50.             EditorUtility.SetDirty(_playerController);      
    51.         }
    52.     }
    53. }