Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. We're looking for your feedback on the platforms you use and how you use them. Let us know!
    Dismiss Notice
  4. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  6. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  7. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  8. 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:
    279
    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.