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. Dismiss Notice

trying to get a PropertyDrawer script to work

Discussion in 'Scripting' started by drhousemd, Feb 20, 2018.

  1. drhousemd

    drhousemd

    Joined:
    Dec 6, 2012
    Posts:
    24
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Code (csharp):
    1.  
    2. //Usage: put these above the variables where you want the divider
    3. //[RXDivider]
    4. //[RXDivider("My header")]
    5. //[RXDivider("My header", "My subtitle")]
    6.  
     
  3. drhousemd

    drhousemd

    Joined:
    Dec 6, 2012
    Posts:
    24
    yes, that's the first thing I tried, but putting '[RXDivider]' results in "The type or namespace name `RXDivider' could not be found. Are you missing an assembly reference?"

    How do I reference RXDivider.cs from another script so it won't create an error?
     
    Last edited: Feb 21, 2018
  4. drhousemd

    drhousemd

    Joined:
    Dec 6, 2012
    Posts:
    24
    Please can anyone help, the way the script is presented it seems like it's supposed to be relatively simple to use.
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Put the property attribute inside a normal class outside of the editor folder.
     
  6. drhousemd

    drhousemd

    Joined:
    Dec 6, 2012
    Posts:
    24
    hmm, is this what you meant? I put the following in the script I want to use [RXDivider] in and the red message went away, but a yellow one appears with 'The type `RXDivider' conflicts with the imported type of same name'. Ignoring the imported type definition':
    Code (csharp):
    1. public class RXDivider : PropertyAttribute
    2. {
    3.     public string header;
    4.     public string subtitle;
    5.  
    6.     public RXDivider(string header, string subtitle) {}
    7.     public RXDivider(string header) {}
    8.     public RXDivider() {}
    9. }
     
  7. drhousemd

    drhousemd

    Joined:
    Dec 6, 2012
    Posts:
    24
    actuallly, I'm getting no errors now, but the script isn't making dividers :/
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Not exactly.. I should have explained more, sorry.
    I'm fairly sure that you have to move both of the property attributes (because there are 2 in the code) to a class outside of Unity.
    You then just have to add in the #if UNITY_EDITOR around the one that used to have it (when it was in the same file).

    So your file would look like:

    #if UNITY_EDITOR
    top property attribute
    #else
    other property attribute (one you just responded with)
    #endif

    Though I'm also .. fairly certain you could have just the main one, and exclude the second one all together. That may be there as an empty filler (guessing) for the build, but the difference to you might be.. * not much.
     
  9. drhousemd

    drhousemd

    Joined:
    Dec 6, 2012
    Posts:
    24
    so tricky... turns out no matter what you do the script won't work. Here's why: at times when I thought I'd set things up right, I'd get this line error and wasn't sure if the problem was with me or the script:
    Code (csharp):
    1. typeof(PropertyDrawer).GetField("s_PropertyDrawers", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic).GetValue(null) as Dictionary<string, PropertyDrawer>;
    While searching around I found a different script that still works and was the basis for the RXDivider script, but even after structuring the files properly in exactly the same way, the RXDivider script still wouldn't work with the error above, it seems 's_PropertyDrawers' is the problem and has changed from Unity 4 to Unity 5 which means it would have never worked no matter what I did.
     
  10. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Oh okay, sorry I didn't know that part :)
     
  11. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I tried it out.. I'm still very new to editor scripting, but I got it to work like this, with a few issues:
    Code (csharp):
    1. // change this line
    2. // DrawDefaultProperty(rect, prop, label, true);
    3. // with this
    4. EditorGUI.PropertyField(rect, prop, label, true);
    5.  
    If I used the 'Header' attribute, it didn't work. If I combined with 'Range' it didn't work. Tooltip did work. That's all I tested, so far.
     
  12. drhousemd

    drhousemd

    Joined:
    Dec 6, 2012
    Posts:
    24
  13. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I was curious. I actually - no joke - just found that 20 seconds ago.. but was too lazy to download the zip ;) lol
    If you want, for fun, you could post the code here? I do like the idea (option) of being able to have this divider in the inspector. I think I will try to keep it around - maybe I'll use it, who knows ;)
     
  14. drhousemd

    drhousemd

    Joined:
    Dec 6, 2012
    Posts:
    24
    sure, so 1st file goes in Editor folder, 2nd in main project, 3rd is how it's added to scripts...

    ... / Editor / DrawerInspectorNote.cs
    Code (csharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5.  
    6. [CustomPropertyDrawer(typeof(InspectorNoteAttribute))]
    7. public class DrawerInspectorNote : PropertyDrawer
    8. {
    9.     /// <summary>
    10.     /// Draw our editor
    11.     /// </summary>
    12.     /// <param name="position"></param>
    13.     /// <param name="property"></param>
    14.     /// <param name="label"></param>
    15.     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    16.     {
    17.         InspectorNoteAttribute note = attribute as InspectorNoteAttribute;
    18.  
    19.         // our header is always present
    20.         Rect posLabel = position;
    21.         posLabel.y += 13;
    22.         posLabel.x -= 2;
    23.         posLabel.height += 13;
    24.         EditorGUI.LabelField(posLabel, note.header, EditorStyles.whiteLargeLabel);
    25.  
    26.         // do we have a message too?
    27.         if (!string.IsNullOrEmpty(note.message))
    28.         {
    29.             Color color = GUI.color;
    30.             Color faded = color;
    31.             faded.a = 0.8f;
    32.  
    33.             Rect posExplain = posLabel;
    34.             posExplain.y += 15;
    35.             GUI.color = faded;
    36.             EditorGUI.LabelField(posExplain, note.message, EditorStyles.whiteMiniLabel);
    37.             GUI.color = color;
    38.         }
    39.  
    40.         Rect posLine = position;
    41.         posLine.y += string.IsNullOrEmpty(note.message) ? 30 : 42;
    42.         posLine.x += 15;;
    43.         posLine.height = 2;
    44.         GUI.Box(posLine, "");
    45.     }
    46.  
    47.     /// <summary>
    48.     /// Override height in case of error
    49.     /// </summary>
    50.     /// <param name="prop"></param>
    51.     /// <param name="label"></param>
    52.     /// <returns></returns>
    53.     public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    54.     {
    55.         InspectorNoteAttribute note = attribute as InspectorNoteAttribute;
    56.  
    57.         return string.IsNullOrEmpty(note.message) ? 38 : 50;
    58.     }
    59. }
    InspectorNoteAttribute.cs
    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public class InspectorNoteAttribute : PropertyAttribute
    4. {
    5.     public readonly string header;
    6.     public readonly string message;
    7.    
    8.     public InspectorNoteAttribute(string header, string message = "")
    9.     {
    10.         this.header = header;
    11.         this.message = message;
    12.     }
    13. }
    ExampleForInspectorNote.cs
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class ExampleForInspectorNote : MonoBehaviour
    6. {
    7.     [InspectorNote("Usage", "Apply to some unused boolean, its Unity PropertyDrawer just displays this stuff")]
    8.     public bool noteIntroduction;
    9.  
    10.     public float someFloat;
    11.     public float anotherFloat = 5f;
    12.  
    13.     public bool aBoolean;
    14.  
    15.     [InspectorNote("Header Only")]
    16.     public bool noteHeaderOnly;
    17.  
    18.     public int MoreStuff;
    19.     public Transform usualUnityStuff;
    20.  
    21.     [InspectorNote("Internal", "I like doing \"Internal\" sections instead of HideInInspector now")]
    22.     public bool noteInternal;
    23.  
    24.     public float yetMoreStuff;
    25.     public int hoorayPropertyDrawer;
    26. }
     
    KingsHere likes this.
  15. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Cool, pretty similar :) I like it.