Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Discussion Array of Custom Class in Custom Inspector

Discussion in 'Immediate Mode GUI (IMGUI)' started by rathod_009, Jul 25, 2022.

  1. rathod_009

    rathod_009

    Joined:
    Apr 14, 2021
    Posts:
    5
    I am trying to write code for custom inspector in which based on the bool value. One array of custom class will be visible. But In that I am not able to add or change array size using length input. Plus and Minus sign to add and remove object is working fine.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System;
    5. using System.Linq;
    6.  
    7. #if UNITY_EDITOR
    8. using UnityEditor;
    9. #endif
    10.  
    11. //Condition is my custom class ->
    12. /**
    13. public class Condition
    14. {
    15.     public ConditionDescription description;
    16.     public bool isSatisfied;
    17.  
    18. }
    19.  
    20.  
    21. [Serializable]
    22. public enum ConditionDescription
    23. {
    24.     isEnter,
    25.     secondCondition
    26. }
    27.  
    28. **/
    29. [CustomPropertyDrawer(typeof(Condition))]
    30. public class ConditionDrawer : PropertyDrawer
    31. {
    32.  
    33.  
    34.     public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    35.     {
    36.         return EditorGUIUtility.singleLineHeight * 2.5f;
    37.     }
    38.     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    39.     {
    40.         EditorGUI.BeginProperty(position, label, property);
    41.  
    42.         // Create the rect for our field
    43.         var rect = new Rect(position.x, position.y + 4, position.width, 16);
    44.         var rect2 = new Rect(position.x, position.y + 24, position.width, 16);
    45.  
    46.         var primeProperty = property.FindPropertyRelative("description");
    47.         var secondaryProperty = property.FindPropertyRelative("isSatisfied");
    48.  
    49.         EditorGUI.PropertyField(rect, primeProperty);
    50.         EditorGUI.PropertyField(rect2, secondaryProperty);
    51.  
    52.         EditorGUI.EndProperty();
    53.     }
    54. }
    55.  
    56. public class SomeBehaviour : MonoBehaviour
    57. {
    58.     public bool isVisible;
    59.     public Condition[] conditions;
    60. }
    61.  
    62. [CustomEditor(typeof(SomeBehaviour))]
    63. public class SomeBehaviourEditor : Editor
    64. {
    65.     private SerializedProperty property;
    66.  
    67.     void OnEnable()
    68.     {
    69.         property = serializedObject.FindProperty("conditions");
    70.     }
    71.  
    72.     public override void OnInspectorGUI()
    73.     {
    74.         serializedObject.Update();
    75.  
    76.         SomeBehaviour item = target as SomeBehaviour;
    77.  
    78.         item.isVisible = EditorGUILayout.Toggle(item.isVisible);
    79.         if(item.isVisible)
    80.         {
    81.             EditorGUILayout.PropertyField(property, true);
    82.         }
    83.     }
    84. }
    85.  
     

    Attached Files: