Search Unity

custom editor for component

Discussion in 'Scripting' started by pierre_l, Nov 17, 2017.

  1. pierre_l

    pierre_l

    Joined:
    Aug 24, 2017
    Posts:
    28
    Hi all.
    I'm trying to add a popup_list to a custom editor of my new component to initialize data.
    I've managed to do it with a new Editor class :

    Code (CSharp):
    1.  
    2.  
    3. public class myscript : MonoBehaviour
    4. {
    5. public bool toto
    6. ...
    7. void Start()
    8.     {
    9. if (toto) { DoSomething; } else { DoSomethingElse; }
    10. }
    11. ...
    12. }
    13.  
    14. [CustomEditor(typeof(myscript))]
    15. public class myscriptEditor : Editor
    16. {
    17.     public enum OPTIONS
    18.     {
    19.         PARAM_INI_1= 0,
    20.         PARAM_INI_2= 1,
    21.     }
    22.  
    23.     public OPTIONS op;
    24.    bool Type_CI;
    25. public override void OnInspectorGUI()
    26.     {
    27.         DrawDefaultInspector();
    28.         myscript myScript = (myscript)target;
    29.  
    30.         op = (OPTIONS)EditorGUILayout.EnumPopup("Type of Initial Conditions:", op);
    31.         Type_CI = InstantiatePrimitive(op);
    32.         if (Type_CI) { myScript.toto = true; } else { myScript.toto = false; }
    33. }
    34. bool InstantiatePrimitive(OPTIONS op)
    35.     {
    36.         switch (op)
    37.         {
    38.             case OPTIONS.PARAM_INI_1:
    39.                 return true;
    40.                 break;
    41.             case OPTIONS.PARAM_INI_2:
    42.                 return false;
    43.                 break;
    44.             default:
    45.                 Debug.LogError("Unrecognized Option");
    46.                 return false;
    47.                 break;
    48.         }
    49.     }
    50. }
    The popup list works fine but the property named "toto" is also visible in the editor. I'd like to hide it as it an internal property. If I change it to private I can't modifiy it with my Editor class.
    Thks for your help
     
    Last edited: Nov 17, 2017
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,281
  3. pierre_l

    pierre_l

    Joined:
    Aug 24, 2017
    Posts:
    28
    wouahh great !
    works in one Attribute.
    Thks so much Karl
     
    karl_jones likes this.