Search Unity

Custom Inspector: Accessing other classes using serializedProperty

Discussion in 'Immediate Mode GUI (IMGUI)' started by KEELAN, Jun 19, 2015.

  1. KEELAN

    KEELAN

    Joined:
    Oct 26, 2013
    Posts:
    51
    Hi there, I am following along with the live unity tutorial about editor scripting and I am having an issue replicating what they are doing.

    http://unity3d.com/learn/tutorials/...-archive/editor-basics/editor-scripting-intro


    I am trying to access an array of classes called BehaviourData within a custom editor for another script. Much like is done in the tutorial linked above.


    Below is my code for the class BehaviourData

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [System.Serializable]
    5. public class BehaviourData{
    6.  
    7.     public Transform target;
    8.  
    9.     public BehaviourData(Transform t)
    10.     {
    11.         target = t;
    12.     }
    13. }
    14.  


    and then in my custom inspector script i have this method

    Code (CSharp):
    1. private  BehaviourData[] GetBehaviourDataArray()
    2.     {
    3.         var arrayCount = behaviourCount.intValue;
    4.         BehaviourData[] transformArray = new BehaviourData[arrayCount];
    5.      
    6.         for (var i=0 ; i<arrayCount;i++)
    7.         {          
    8.             transformArray[i] = serializedObject.FindProperty(string.Format(behaviourTypeArrayData,i)).objectReferenceValue as BehaviourData;
    9.         }
    10.      
    11.         return transformArray;
    12.     }

    which mirrors the tutorial but I get compiler errors saying that they can not convert UnityEngine.Object to BehaviourData via a reference conversion, boxing conversion etc etc.


    I have tried multiple times to convert in different ways including "as System.Object as BehaviourData" but this brings a whole host of other issues.
    I have checked my code against the tutorial several times and besides their "Waypoint" method (which isnt ever shown) there are no differences other than names in our scripts. I am quite lost here

    Shouldn't be too hard to have access to an array of an object from a custom inspector
    Kind Regards
    Keelan




    [1]: /storage/temp/48484-tempunity.png
     
    Last edited: Jun 19, 2015
    dginovker likes this.
  2. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    You didn't show what is behaviourCount or behaviourTypeArrayData and how you get a reference to them.

    Assuming these are all fine, your issue is that FindProperty returns an instance of SerializedProperty.
    I like to think of the SerializedObject and SerializedProperty classes as "views" on top of actual objects that allow you (and the serialization system) to view an object as a generic serializable type, iterating over its properties and fields, etc.

    Using objectReferenceValue on this property returns an instance derived from UnityEngine.Object. It would probably work in case your object derived from that.

    So, a quick fix would be to have BehaviourData derive from UnityEngine.Object.
     
  3. KEELAN

    KEELAN

    Joined:
    Oct 26, 2013
    Posts:
    51
    Thanks for this reply. Yeah to be honest I dont really have a great knowledge on editor scripting. Mostly just find examples and modify the solutions to my needs. This helped my understanding quite a bit, so thanks.

    How exactly would one make a class derive from UnityEngine.Object?
    Would you just write it like this?
    Code (CSharp):
    1. public class Example : UnityEngine.Object () {...}
    Again thanks for your insight.
     
  4. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    Yes, exactly.
     
  5. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,657
    It's not possible to derive your own classes directly from UnityEngine.Object. You have to either derive them from MonoBehaviour (i.e. make them be components), or derive them from ScriptableObject.
     
    liortal likes this.
  6. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    I didn't realize that.

    BTW why is there no API for retrieving the target object instance from SerializedProperty for non "Object" types? (like i asked in the beta group today).

    The serialization system recognizes these objects so it should also provide a way to return the underlying instance IMO.
     
  7. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,657
    Probably the answer is 'nobody wrote it yet,' but one complication is that the underlying object may not actually exist; one of the features of the serialisation system is the ability to manipulate stored data without actually having to construct any managed objects.
     
  8. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    i knew you would copy/paste the "nobody wrote it yet response" !! :)
     
    BMayne likes this.