CustomObjectDrawer and SharedVariables

persy

New member
Hi!
Can I somehow embed default SharedVariable drawer into my CustomObjectDrawer OnGUI method?
I have a custom serializable struct which I use as a public field for a handful of custom tasks. This struct has a CustomObjectDrawer which works fine. Now i want to add public SharedVariable field to this struct and be able to use default SharedVariable drawer inside my CustomObjectDrawer as I see fit without rewriting it from scratch.

C#:
    [System.Serializable]
    public struct SceneObject
    {
        public enum ValueType {
            UseGameObject,
            UseWaypointKey
        }
        public ValueType valueType;
        public GameObject gameObject;
        public string waypointKey;
    }

    [CustomObjectDrawer(typeof(SceneObject))]
    public class SceneObjectDrawer : ObjectDrawer
    {
        public override void OnGUI(GUIContent label)
        {
            SceneObject target = (SceneObject)value;

            EditorGUI.BeginChangeCheck();

            switch (target.valueType)
            {
                case SceneObject.ValueType.UseGameObject : {
                    target.gameObject = (GameObject)EditorGUILayout.ObjectField(label, target.gameObject, typeof(UnityEngine.GameObject), true);
                    break;
                }
                case SceneObject.ValueType.UseWaypointKey : {
                    target.waypointKey = EditorGUILayout.TextField(label, target.waypointKey);
                    break;
                }
            }
            target.valueType = (SceneObject.ValueType)EditorGUILayout.EnumPopup(target.valueType, GUILayout.MaxWidth(EditorGUIUtility.singleLineHeight));
            if(EditorGUI.EndChangeCheck())
            {
                value = target;
            }
        }
    }
This works just fine, but now I want to change SceneObject.gameObject field type to SharedGameObject and have this magic circle button appear in my custom drawer :)
1571645021696.png
Is there any way I can call a SharedGameObject drawer from my custom OnGUI?
 

Justin

Administrator
Staff member
The editor uses BehaviorDesigner.Editor.FieldInspector.DrawSharedVariable which you should be able to use within your object drawer :)
 

persy

New member
Thanks! Couldn't figure it out on my own. Now I am doing this:

CSS:
    [CustomObjectDrawer(typeof(SceneObject))]
    public class SceneObjectDrawer : ObjectDrawer
    {
        public override void OnGUI(GUIContent label)
        {
            SceneObject target = (SceneObject)value;
            BehaviorDesigner.Editor.FieldInspector.DrawSharedVariable(
                task,
                label,
                typeof(SceneObject).GetField("sharedGameObject"),
                typeof(BehaviorDesigner.Runtime.SharedGameObject),
                target.sharedGameObject);
        }
    }
It is drawn as expected, values are correctly serialized, however the magic button does not react. What am I missing here?
1571658281538.png
 

Justin

Administrator
Staff member
Can you send me a small repro project that has that object drawers as well as the scene object type? I'm not sure why the toggle variable button isn't working.
 

persy

New member
Rebuild it from scratch, behaves just like I described earlier:

I'm using unity 2019.1.14f1
Thanks!
 

Justin

Administrator
Staff member
Thanks. Please do not include Behavior Designer in the zip - this is a public forum so others without a BD license would also be able to download it.

I should have seen this earlier. You aren't assigning the shared variable so that's why the value didn't persist. If you do the following it will work:

Code:
            target.structField = BehaviorDesigner.Editor.FieldInspector.DrawSharedVariable(
                task,
                label,
                typeof(SampleTask.SampleStruct).GetField("structField"),
                typeof(BehaviorDesigner.Runtime.SharedGameObject), 
                target.structField) as Runtime.SharedGameObject;
 
Top