Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

DrawPropertiesExluding for non Monobehaviour classes?

Discussion in 'Scripting' started by Punfish, Aug 4, 2018.

  1. Punfish

    Punfish

    Joined:
    Dec 7, 2014
    Posts:
    89
    Having an error trying to assign classAbilities from target under OnInspectorGUI(). Error is cannot convert ClassAbilities to UnityEngine.Object. I realize it's because ClassAbilities doesn't inherit Monobehaviour, but I haven't been able to find a solution for this.

    Code (CSharp):
    1. [CustomEditor(typeof(ClassAbilities))]
    2. public class ClassAbilitiesEditor : Editor
    3. {
    4.     public override void OnInspectorGUI()
    5.     {
    6.         ClassAbilities classAbilities = target as ClassAbilities;
    7.         List<string> excludedProperties = new List<string>();
    8.  
    9.         if (!classAbilities.CanUse)
    10.             excludedProperties.Add("_abilities");
    11.  
    12.         if (excludedProperties.Count > 0)
    13.             DrawPropertiesExcluding(serializedObject, excludedProperties.ToArray());
    14.         else
    15.             DrawDefaultInspector();
    16.  
    17.         ProcessChanges(classAbilities);
    18.     }
    19. }

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3.  
    4. namespace FirstGearGames.MOBAROyale.Abilities
    5. {
    6.     [System.Serializable]
    7.     public class ClassAbilities
    8.     {
    9.         /// <summary>
    10.         /// Use CanUse.
    11.         /// </summary>
    12.         [Tooltip("True if the unit can use this weapon in the specified HandPlacements.")]
    13.         [SerializeField]
    14.         private bool _canUse = true;
    15.         /// <summary>
    16.         /// True if the unit can use this weapon in the specified HandPlacements.
    17.         /// </summary>
    18.         public bool CanUse { get { return _canUse; } }
    19.         /// <summary>
    20.         /// Use Abilities.
    21.         /// </summary>
    22.         [Tooltip("Abilities for the UnitClass using this weapon.")]
    23.         [SerializeField]
    24.         private List<Ability> _abilities = new List<Ability>();
    25.         /// <summary>
    26.         /// Abilities for the UnitClass using this weapon.
    27.         /// </summary>
    28.         public List<Ability> Abilities { get { return _abilities; } }
    29.     }
    30.  
    31.  
    32. }
     
  2. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    591
    As non-MonoBehaviour/SO classes aren't ever given an editor of their own, having an editor class for one doesn't make much sense. If your ClassAbilities class is meant to be a property in another SO or MonoBehaviour, you want to use a PropertyDrawer instead.
     
    Kiwasi and Punfish like this.
  3. Punfish

    Punfish

    Joined:
    Dec 7, 2014
    Posts:
    89
    This was helpful, thank you.