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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Get Serialized Property of Object

Discussion in 'Scripting' started by DARKHAWX, Jun 6, 2018.

  1. DARKHAWX

    DARKHAWX

    Joined:
    Apr 15, 2017
    Posts:
    15
    A bit of a weird problem, in my script I have an object that I want a custom editor for. In that object is a Dictionary that links integer ids to object values. In my editor I have a ReorderableList that will be using a base list of integer ids as the serialized list object. What I'd like to achieve is for each element in the list to draw a custom inspector for the object values. Which means I get my current element in the list and obtain the int value of the property. using that I get my object from the dictionary. Then I need some code to convert my object into a serialized property to use in EditorGUI.PropertyField().

    Code (CSharp):
    1. baseGroupList.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) => {
    2.             SerializedProperty prop = baseGroupList.serializedProperty.GetArrayElementAtIndex(index);
    3.             MeshGroup mGroup = manager.groupList.GetGroup(prop.intValue);
    4.                         // Serialisation code here; mGroupProp = ???
    5.             rect.y += 2;
    6.             EditorGUI.PropertyField(rect, mGroupProp, new GUIContent("Group"), true);
    7.         };
    Now I know I could just define the custom inspector here for the object, however this object is recursive. It contains its own id list of its children, which links to the main dictionary. Therefore I need to be able to call EditorGUI.PropertyField so that I can recursively draw it's children. Happy to share additional code if needed.

    Any help would be appreciated. Thanks.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
  3. DARKHAWX

    DARKHAWX

    Joined:
    Apr 15, 2017
    Posts:
    15
    Had a look, doesn't seem to be what I need.

    However a workaround was made. I made a new class that extended scriptable object, was serializable and only had one variable, being the object that I want to convert into a property. Then I would make a new serialiable object using the new class and then I can call FindProperty() to get the property I want. Not a very elegant solution, but it seems to work.


    Code (CSharp):
    1. baseGroupList.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) => {
    2.     SerializedProperty prop = baseGroupList.serializedProperty.GetArrayElementAtIndex(index);
    3.     MeshGroupSerObj mGroup = manager.groupList.MakeMeshGroupSerObj(prop.intValue);
    4.     SerializedObject mGroupSerObj = new SerializedObject(mGroup);
    5.     rect.y += 2;
    6.     EditorGUI.PropertyField(rect, mGroupSerObj.FindProperty("mGroup"), new GUIContent("Group"), true);
    7. };