Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

DrawPropertiesExluding for non Monobehaviour classes?

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

  1. Distul

    Distul

    Joined:
    Dec 7, 2014
    Posts:
    79
    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:
    450
    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 Distul like this.
  3. Distul

    Distul

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