Example #1
0
    protected string GetFieldValue(Datablock datablock, FieldInfo field)
    {
        // If the field uses its parent's value, return an empty string
        if (!datablock.DoesOverridesParent(field) && !exportFullValues)
            return "";

        if (field.FieldType == typeof(int))
        {
            return ((int)datablock.GetFieldValue(field)).ToString();
        }
        else if (field.FieldType == typeof(float))
        {
            return ((float)field.GetValue(datablock)).ToString();
        }
        else if (field.FieldType == typeof(bool))
        {
            return ((bool)field.GetValue(datablock)) ? "True" : "False";
        }
        else if (field.FieldType == typeof(double))
        {
            return ((double)field.GetValue(datablock)).ToString();
        }
        else if (field.FieldType == typeof(string))
        {
            var str = ((string)field.GetValue(datablock));
            if (string.IsNullOrEmpty(str))
                return "(null)";
            return str;
        }
        else if (field.FieldType == typeof(Color))
        {
            var val = (Color)field.GetValue(datablock);
            return val.r.ToString() + ',' + val.g.ToString() + ',' + val.b.ToString() + ',' + val.a.ToString();
        }
        else if (field.FieldType == typeof(Vector2))
        {
            var val = (Vector2)field.GetValue(datablock);
            return val.x.ToString() + ',' + val.y.ToString();
        }
        else if (field.FieldType == typeof(Vector3))
        {
            var val = (Vector3)field.GetValue(datablock);
            return val.x.ToString() + ',' + val.y.ToString() + ',' + val.z.ToString();
        }
        else if (field.FieldType == typeof(Vector4))
        {
            var val = (Vector4)field.GetValue(datablock);
            return val.x.ToString() + ',' + val.y.ToString() + ',' + val.z.ToString() + ',' + val.w.ToString();
        }
        else if (field.FieldType.IsSubclassOf(typeof(Object)))
        {
            var obj = ((Object)field.GetValue(datablock));

            if (!obj)
                return "";

            return obj.name;
        }
        else if (field.FieldType.IsGenericType && field.FieldType.GetGenericTypeDefinition() == typeof(DatablockRef<>))
        {
            return ((IDatablockRef)field.GetValue(datablock)).GetPath();
        }
        else if (field.FieldType.IsEnum)
        {
            return ((Enum)field.GetValue(datablock)).ToString();
        }
        return "";
    }
Example #2
0
        protected string GetFieldValue(Datablock datablock, FieldInfo field)
        {
            // If the field uses its parent's value, return an empty string
            if (!datablock.DoesOverridesParent(field) && !exportFullValues)
            {
                return("");
            }

            if (field.FieldType == typeof(int))
            {
                return(((int)datablock.GetFieldValue(field)).ToString());
            }
            else if (field.FieldType == typeof(float))
            {
                return(((float)field.GetValue(datablock)).ToString());
            }
            else if (field.FieldType == typeof(bool))
            {
                return(((bool)field.GetValue(datablock)) ? "True" : "False");
            }
            else if (field.FieldType == typeof(double))
            {
                return(((double)field.GetValue(datablock)).ToString());
            }
            else if (field.FieldType == typeof(string))
            {
                var str = ((string)field.GetValue(datablock));
                if (string.IsNullOrEmpty(str))
                {
                    return("(null)");
                }
                return(str);
            }
            else if (field.FieldType == typeof(Color))
            {
                var val = (Color)field.GetValue(datablock);
                return(val.r.ToString() + ',' + val.g.ToString() + ',' + val.b.ToString() + ',' + val.a.ToString());
            }
            else if (field.FieldType == typeof(Vector2))
            {
                var val = (Vector2)field.GetValue(datablock);
                return(val.x.ToString() + ',' + val.y.ToString());
            }
            else if (field.FieldType == typeof(Vector3))
            {
                var val = (Vector3)field.GetValue(datablock);
                return(val.x.ToString() + ',' + val.y.ToString() + ',' + val.z.ToString());
            }
            else if (field.FieldType == typeof(Vector4))
            {
                var val = (Vector4)field.GetValue(datablock);
                return(val.x.ToString() + ',' + val.y.ToString() + ',' + val.z.ToString() + ',' + val.w.ToString());
            }
            else if (field.FieldType.IsSubclassOf(typeof(Object)))
            {
                var obj = ((Object)field.GetValue(datablock));

                if (!obj)
                {
                    return("");
                }

                return(obj.name);
            }
            else if (field.FieldType.IsGenericType && field.FieldType.GetGenericTypeDefinition() == typeof(DatablockRef <>))
            {
                return(((IDatablockRef)field.GetValue(datablock)).GetPath());
            }
            else if (field.FieldType.IsEnum)
            {
                return(((Enum)field.GetValue(datablock)).ToString());
            }
            return("");
        }
Example #3
0
        /// <summary>
        ///     Draw a field
        /// </summary>
        /// <param name="field">Field to draw</param>
        /// <param name="indent">Indent the display of the field</param>
        protected virtual void DrawField(FieldInfo field, int indent = 0)
        {
            bool overrideParentValue = datablock.DoesOverridesParent(field) || datablock.Parent == null;

            Type   fieldType = field.FieldType;
            object val       = null;

            bool isDatablockReference = false;

            if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition() == typeof(DatablockRef <>))
            {
                fieldType            = fieldType.GetGenericArguments()[0];
                val                  = datablock.GetFieldValue <IDatablockRef>(field).GetObject();
                isDatablockReference = true;
            }
            else
            {
                val = datablock.GetFieldValue(field);
            }

            // Set the indent
            EditorGUI.indentLevel = indent;

            // Field overrides parent, or doesn't have a parent
            if (fieldType == typeof(int))
            {
                EditorGUILayout.BeginHorizontal();

                //var val = (int) field.GetValue(datablock);
                int newVal = EditorGUILayout.IntField(FieldLabel(field), (int)val);

                if ((int)val != newVal)
                {
                    SetFieldValue(field, newVal);
                }
            }
            else if (fieldType == typeof(float))
            {
                EditorGUILayout.BeginHorizontal();

                float newVal = EditorGUILayout.FloatField(FieldLabel(field), (float)val);

                if ((float)val != newVal)
                {
                    SetFieldValue(field, newVal);
                }
            }
#if UNITY_5_0
            else if (fieldType == typeof(double))
            {
                EditorGUILayout.BeginHorizontal();

                double newVal = EditorGUILayout.DoubleField(FieldLabel(field), (double)val);

                if ((double)val != newVal)
                {
                    SetFieldValue(field, newVal);
                }
            }
#endif
            else if (fieldType == typeof(bool))
            {
                EditorGUILayout.BeginHorizontal();

                bool newVal = EditorGUILayout.Toggle(FieldLabel(field), (bool)val);

                if ((bool)val != newVal)
                {
                    SetFieldValue(field, newVal);
                }
            }
            else if (fieldType == typeof(Vector2))
            {
                EditorGUILayout.BeginHorizontal();

                Vector2 newVal = EditorGUILayout.Vector2Field(FieldLabel(field), (Vector2)val);

                if ((Vector2)val != newVal)
                {
                    SetFieldValue(field, newVal);
                }
            }
            else if (fieldType == typeof(Vector3))
            {
                EditorGUILayout.BeginHorizontal();

                Vector3 newVal = EditorGUILayout.Vector3Field(FieldLabel(field), (Vector3)val);

                if ((Vector3)val != newVal)
                {
                    SetFieldValue(field, newVal);
                }
            }
            else if (fieldType == typeof(Vector4))
            {
                EditorGUILayout.BeginHorizontal();

                Vector4 newVal = EditorGUILayout.Vector4Field(FormatName(field.Name), (Vector4)val);

                if ((Vector4)val != newVal)
                {
                    SetFieldValue(field, newVal);
                }
            }
            else if (fieldType == typeof(Color))
            {
                EditorGUILayout.BeginHorizontal();

                Color newVal = EditorGUILayout.ColorField(FieldLabel(field), (Color)val);

                if ((Color)val != newVal)
                {
                    SetFieldValue(field, newVal);
                }
            }
            else if (fieldType.IsSubclassOf(typeof(Object)))
            {
                EditorGUILayout.BeginHorizontal();

                Object newVal = EditorGUILayout.ObjectField(FieldLabel(field), (Object)val, fieldType, false);
                if ((Object)val != newVal)
                {
                    if (isDatablockReference)
                    {
                        object val2 = Activator.CreateInstance(field.FieldType, new object[] { newVal });
                        SetFieldValue(field, val2);
                    }
                    else
                    {
                        SetFieldValue(field, newVal);
                    }
                }
            }
            else if (fieldType == typeof(string))
            {
                EditorGUILayout.BeginHorizontal();

                string newVal = EditorGUILayout.TextField(FieldLabel(field), (string)val);

                if ((string)val != newVal)
                {
                    SetFieldValue(field, newVal);
                }
            }
            else if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition() == typeof(List <>))
            {
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.BeginVertical();

                serializedObject.Update();
                SerializedProperty listProperty = serializedObject.FindProperty(field.Name);

                try
                {
                    ReorderableListGUI.Title(field.Name);
                    ReorderableListGUI.ListField(listProperty);
                }
                catch (ExitGUIException)
                {
                    // suppress unity bug
                }

                serializedObject.ApplyModifiedProperties();
                EditorGUILayout.EndVertical();
            }
            else if (fieldType.IsEnum)
            {
                EditorGUILayout.BeginHorizontal();

                Enum newVal;

                if (Attribute.IsDefined(fieldType, typeof(FlagsAttribute)))
                {
                    newVal = EditorGUILayout.EnumMaskField(FieldLabel(field), (Enum)val);
                }
                else
                {
                    newVal = EditorGUILayout.EnumPopup(FieldLabel(field), (Enum)val);
                }

                if (!Equals((Enum)val, newVal))
                {
                    SetFieldValue(field, newVal);
                }
            }
            else
            {
                return;
            }

            EditorGUI.indentLevel = 0;
            EditorGUI.BeginDisabledGroup(datablock.Parent == null);
            {
                bool useParentValPrev = overrideParentValue;
                overrideParentValue = EditorGUILayout.Toggle("", overrideParentValue, GUILayout.MaxWidth(20));
                if (overrideParentValue != useParentValPrev)
                {
                    datablock.SetOverridesParent(field, overrideParentValue);

                    if (overrideParentValue)
                    {
                        field.SetValue(datablock, GetDefaultValue(fieldType));
                    }
                }
            }
            EditorGUI.EndDisabledGroup();
            EditorGUILayout.EndHorizontal();

            if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition() == typeof(List <>))
            {
                EditorGUILayout.Space();
                EditorGUILayout.Space();
            }
        }