Ejemplo n.º 1
0
        public static object CreateDefaultFor(Type type, int depth = 0)
        {
            if (type.IsValueType)
            {
                return(Activator.CreateInstance(type));
            }
            if (type == typeof(AnimationCurve))
            {
                return(new AnimationCurve());
            }
            if (type == typeof(Color))
            {
                return(Color.white);
            }
            if (typeof(Object).IsAssignableFrom(type))
            {
                return(null);
            }

            object parseResult = null;

            try
            {
                deserializer.TryDeserialize(emptyfsJsonData, type, ref parseResult).AssertSuccess();
            }
            catch (Exception)
            {
                //Deserialization failed, parseResult is now garbage.
                return(null);
            }

            if (depth < SerializableSystemType.MaxSerializationDepth)
            {
                var internalFields = FieldUtil.DrawableFields(parseResult.GetType());
                foreach (var field in internalFields)
                {
                    field.SetValue(parseResult, CreateDefaultFor(field.FieldType, depth + 1));
                }
            }

            return(parseResult);
        }
        public static object Draw(Rect position, GUIContent label, object o, int currentDepth)
        {
            position.height = EditorGUIUtility.singleLineHeight;
            if (o == null)
            {
                EditorGUI.LabelField(position, "Drawing null!");
                return(o);
            }

            EditorGUI.LabelField(position, label);
            EditorGUI.indentLevel++;
            foreach (var field in FieldUtil.DrawableFields(o.GetType()))
            {
                var fieldValue = field.GetValue(o);
                var valueType  = field.FieldType;
                position = EditorUtil.NextPosition(position, FieldUtil.HeightRequiredToDraw(field.FieldType));
                var newValue = SerializableArgumentDrawer.DrawObjectOfType(position, new GUIContent(field.Name), valueType, fieldValue, currentDepth + 1);
                field.SetValue(o, newValue);
            }
            EditorGUI.indentLevel--;

            return(o);
        }