//the variable data field
        object VariableField(Variable data, UnityEngine.Object contextParent, GUILayoutOption[] layoutOptions)
        {
            var o = data.value;
            var t = data.varType;

            if (t == typeof(VariableSeperator))
            {
                GUILayout.Space(0);
                return(o);
            }

            if (t == typeof(object))
            {
                GUILayout.Label(string.Format("({0})", t.FriendlyName()), Styles.leftLabel, layoutOptions);
                return(o);
            }

            if (t.IsAbstract && !typeof(UnityEngine.Object).IsAssignableFrom(t) && !t.IsInterface && t != typeof(System.Type))
            {
                GUILayout.Label(string.Format("Abstract({0})", t.FriendlyName()), Styles.leftLabel, layoutOptions);
                return(o);
            }

            ///----------------------------------------------------------------------------------------------
            bool handled;

            o = EditorUtils.DirectFieldControl(GUIContent.none, o, t, contextParent, out handled, layoutOptions);
            if (handled)
            {
                return(o);
            }
            ///----------------------------------------------------------------------------------------------

            //If some other type, show it in the generic object editor window
            if (GUILayout.Button(string.Format("{0} {1}", t.FriendlyName(), (o is IList) ? ((IList)o).Count.ToString() : string.Empty), layoutOptions))
            {
                //we use bb.GetVariableByID to avoid undo creating new instance of variable and thus generic inspector, left inspecting something else
                GenericInspectorWindow.Show(data.name, t, contextParent, () => { return(bb.GetVariableByID(data.ID).value); }, (newValue) => { bb.GetVariableByID(data.ID).value = newValue; });
            }

            return(o);
        }
        //the variable data field
        object VariableField(Variable data, UnityEngine.Object contextParent, GUILayoutOption[] layoutOptions)
        {
            var o = data.value;
            var t = data.varType;

            if (t == typeof(VariableSeperator))
            {
                GUILayout.Space(0);
                return(o);
            }

            //allow creation of derived classes for abstract classes via button
            if (o == null && t.IsAbstract && !typeof(UnityEngine.Object).IsAssignableFrom(t) && !t.IsInterface && t != typeof(System.Type))
            {
                if (GUILayout.Button("(null) Create", layoutOptions))
                {
                    EditorUtils.GetTypeSelectionMenu(t, (derived) => { data.value = System.Activator.CreateInstance(derived); }).ShowAsContext();
                }
                return(o);
            }

            ///----------------------------------------------------------------------------------------------
            bool handled;

            o = EditorUtils.DirectFieldControl(GUIContent.none, o, t, contextParent, null, out handled, layoutOptions);
            if (handled)
            {
                return(o);
            }
            ///----------------------------------------------------------------------------------------------

            //If some other type, show it in the generic object editor window with its true value type
            t = o != null?o.GetType() : t;

            if (GUILayout.Button(string.Format("{0} {1}", t.FriendlyName(), (o is IList) ? ((IList)o).Count.ToString() : string.Empty), layoutOptions))
            {
                //we use bb.GetVariableByID to avoid undo creating new instance of variable and thus generic inspector, left inspecting something else
                GenericInspectorWindow.Show(data.name, t, contextParent, () => { return(bb.GetVariableByID(data.ID).value); }, (newValue) => { bb.GetVariableByID(data.ID).value = newValue; });
            }

            return(o);
        }
Esempio n. 3
0
        //While there is a similar method in EditorUtils, due to layouting and especialy no prefix name, this has to be redone a bit differently
        static object VariableField(Variable data, UnityEngine.Object context, GUILayoutOption[] layoutOptions)
        {
            var o = data.value;
            var t = data.varType;

            if (t == typeof(VariableSeperator))
            {
                GUILayout.Space(0);
                return(o);
            }

            //Check scene object type for UnityObjects. Consider Interfaces as scene object type. Assume that user uses interfaces with UnityObjects
            if (typeof(UnityEngine.Object).IsAssignableFrom(t) || t.IsInterface)
            {
                var isSceneObjectType = (typeof(Component).IsAssignableFrom(t) || t == typeof(GameObject) || t.IsInterface);
                return(EditorGUILayout.ObjectField((UnityEngine.Object)o, t, isSceneObjectType, layoutOptions));
            }

            //Check Type second
            if (t == typeof(System.Type))
            {
                return(EditorUtils.Popup <System.Type>(string.Empty, (System.Type)o, UserTypePrefs.GetPreferedTypesList(true), layoutOptions));
            }

            t = o != null?o.GetType() : t;

            if (t.IsAbstract)
            {
                GUILayout.Label(string.Format("({0})", t.FriendlyName()), layoutOptions);
                return(o);
            }

            if (o == null && !t.IsAbstract && !t.IsInterface && (t.GetConstructor(System.Type.EmptyTypes) != null || t.IsArray))
            {
                if (GUILayout.Button("(null) Create", layoutOptions))
                {
                    if (t.IsArray)
                    {
                        return(System.Array.CreateInstance(t.GetElementType(), 0));
                    }
                    return(System.Activator.CreateInstance(t));
                }
                return(o);
            }

            if (t == typeof(bool))
            {
                return(EditorGUILayout.Toggle((bool)o, layoutOptions));
            }

            if (t == typeof(Color))
            {
                return(EditorGUILayout.ColorField((Color)o, layoutOptions));
            }

            if (t == typeof(AnimationCurve))
            {
                return(EditorGUILayout.CurveField((AnimationCurve)o, layoutOptions));
            }

            if (t.IsSubclassOf(typeof(System.Enum)))
            {
                if (t.IsDefined(typeof(System.FlagsAttribute), true))
                {
                    return(EditorGUILayout.EnumMaskPopup(GUIContent.none, (System.Enum)o, layoutOptions));
                }
                return(EditorGUILayout.EnumPopup((System.Enum)o, layoutOptions));
            }

            if (t == typeof(float))
            {
                GUI.backgroundColor = UserTypePrefs.GetTypeColor(t);
                return(EditorGUILayout.FloatField((float)o, layoutOptions));
            }

            if (t == typeof(int))
            {
                GUI.backgroundColor = UserTypePrefs.GetTypeColor(t);
                return(EditorGUILayout.IntField((int)o, layoutOptions));
            }

            if (t == typeof(string))
            {
                GUI.backgroundColor = UserTypePrefs.GetTypeColor(t);
                return(EditorGUILayout.TextField((string)o, layoutOptions));
            }

            if (t == typeof(Vector2))
            {
                return(EditorGUILayout.Vector2Field(string.Empty, (Vector2)o, layoutOptions));
            }

            if (t == typeof(Vector3))
            {
                return(EditorGUILayout.Vector3Field(string.Empty, (Vector3)o, layoutOptions));
            }

            if (t == typeof(Vector4))
            {
                return(EditorGUILayout.Vector4Field(string.Empty, (Vector4)o, layoutOptions));
            }

            if (t == typeof(Quaternion))
            {
                var q = (Quaternion)o;
                var v = new Vector4(q.x, q.y, q.z, q.w);
                v = EditorGUILayout.Vector4Field(string.Empty, v, layoutOptions);
                return(new Quaternion(v.x, v.y, v.z, v.w));
            }

            if (t == typeof(LayerMask))
            {
                return(EditorUtils.LayerMaskField(string.Empty, (LayerMask)o, layoutOptions));
            }

            //If some other type, show it in the generic object editor window
            if (GUILayout.Button(string.Format("{0} {1}", t.FriendlyName(), (o is IList)? ((IList)o).Count.ToString() : string.Empty), layoutOptions))
            {
                GenericInspectorWindow.Show(data.ID, o, t, context);
            }

            //if we are externaly inspecting value and it's this one, get value from the external editor. This is basicaly done for structs
            if (GenericInspectorWindow.current != null && GenericInspectorWindow.current.inspectedID == data.ID)
            {
                return(GenericInspectorWindow.current.value);
            }

            return(o);
        }
Esempio n. 4
0
        //While there is a similar method in EditorUtils, due to layouting and especialy no prefix name, this has to be redone a bit differently
        static object EditorField(object o, System.Type t, bool isPersistent, GUILayoutOption[] layoutOptions)
        {
            //Check scene object type for UnityObjects. Consider Interfaces as scene object type. Assumpt that user uses interfaces with UnityObjects
            var isSceneObjectType = (typeof(Component).IsAssignableFrom(t) || t == typeof(GameObject) || t.IsInterface);

/*
 *                      if (isPersistent && isSceneObjectType){
 *                              GUI.enabled = false;
 *                              EditorGUILayout.ObjectField((UnityEngine.Object)o, t, true, layoutOptions);
 *                              GUI.enabled = true;
 *                              return o;
 *                      }
 */
            if (typeof(UnityEngine.Object).IsAssignableFrom(t) || t.IsInterface)
            {
                return(UnityEditor.EditorGUILayout.ObjectField((UnityEngine.Object)o, t, isSceneObjectType, layoutOptions));
            }

            t = o != null?o.GetType() : t;

            if (t.IsAbstract)
            {
                GUILayout.Label(string.Format("({0})", t.FriendlyName()), layoutOptions);
                return(o);
            }

            if (o == null && !t.IsAbstract && !t.IsInterface && t.GetConstructor(System.Type.EmptyTypes) != null)
            {
                if (GUILayout.Button("(null) Create", layoutOptions))
                {
                    return(System.Activator.CreateInstance(t));
                }
                return(o);
            }

            if (t == typeof(bool))
            {
                return(UnityEditor.EditorGUILayout.Toggle((bool)o, layoutOptions));
            }
            if (t == typeof(Color))
            {
                return(UnityEditor.EditorGUILayout.ColorField((Color)o, layoutOptions));
            }
            if (t == typeof(AnimationCurve))
            {
                return(UnityEditor.EditorGUILayout.CurveField((AnimationCurve)o, layoutOptions));
            }
            if (t.IsSubclassOf(typeof(System.Enum)))
            {
                return(UnityEditor.EditorGUILayout.EnumPopup((System.Enum)o, layoutOptions));
            }
            if (t == typeof(float))
            {
                GUI.backgroundColor = UserTypePrefs.GetTypeColor(t);
                return(UnityEditor.EditorGUILayout.FloatField((float)o, layoutOptions));
            }
            if (t == typeof(int))
            {
                GUI.backgroundColor = UserTypePrefs.GetTypeColor(t);
                return(UnityEditor.EditorGUILayout.IntField((int)o, layoutOptions));
            }
            if (t == typeof(string))
            {
                GUI.backgroundColor = UserTypePrefs.GetTypeColor(t);
                return(UnityEditor.EditorGUILayout.TextField((string)o, layoutOptions));
            }
            if (t == typeof(Vector2))
            {
                return(UnityEditor.EditorGUILayout.Vector2Field("", (Vector2)o, new GUILayoutOption[] { GUILayout.MaxWidth(100), GUILayout.ExpandWidth(true), GUILayout.MaxHeight(18) }));
            }
            if (t == typeof(Vector3))
            {
                return(UnityEditor.EditorGUILayout.Vector3Field("", (Vector3)o, new GUILayoutOption[] { GUILayout.MaxWidth(100), GUILayout.ExpandWidth(true), GUILayout.MaxHeight(18) }));
            }
            if (t == typeof(Quaternion))
            {
                var q = (Quaternion)o;
                var v = new Vector4(q.x, q.y, q.z, q.w);
                v = UnityEditor.EditorGUILayout.Vector4Field("", v, layoutOptions);
                return(new Quaternion(v.x, v.y, v.z, v.w));
            }

            //If some other type, show it in the generic object editor window
            if (GUILayout.Button(string.Format("{0} {1}", t.FriendlyName(), (o is IList)? ((IList)o).Count.ToString() : ""), layoutOptions))
            {
                GenericInspectorWindow.Show(o, t, isPersistent);
            }

            return(o);
        }