예제 #1
0
        private static void EnsureDataCached(Type type)
        {
            if (!typeToMethods.ContainsKey(type))
            {
                var methods     = MethodLister.SerializeableMethodsOn(type);
                var methodNames = new string[methods.Count + 1];
                methodNames[0] = NoMethodSelected;
                for (int i = 0; i < methods.Count; i++)
                {
                    methodNames[i + 1] = MethodName(methods[i]);
                }

                var fields = FieldUtil.SerializableFields(type);

                typeToMethods[type] = methods;
                typeToFields[type]  = fields.Select(field => new SerializableFieldSetter(field)).ToList();
                typeToNames[type]   = methodNames.Concat(fields.Select(ShowFieldName)).ToArray();
            }
        }
예제 #2
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);
        }
예제 #4
0
        public static float GetHeightForType(Type type, float defaultHeight, int depth)
        {
            //nonstandard heights
            if (type == typeof(Bounds))
            {
                return(defaultHeight * 3f);
            }
            if (type == typeof(Rect))
            {
                return(defaultHeight * 2f);
            }

            if (type == typeof(Color))
            {
                return(defaultHeight);
            }
            if (type == typeof(AnimationCurve))
            {
                return(defaultHeight);
            }
            if (type.IsEnum)
            {
                return(defaultHeight);
            }
            if (type == typeof(float))
            {
                return(defaultHeight);
            }
            if (type == typeof(int))
            {
                return(defaultHeight);
            }
            if (type == typeof(string))
            {
                return(defaultHeight);
            }
            if (type == typeof(bool))
            {
                return(defaultHeight);
            }
            if (type == typeof(Vector2))
            {
                return(defaultHeight);
            }
            if (type == typeof(Vector3))
            {
                return(defaultHeight);
            }
            if (type == typeof(Vector4))
            {
                return(defaultHeight);
            }
            if (type == typeof(Quaternion))
            {
                return(defaultHeight);
            }
            if (typeof(Object).IsAssignableFrom(type))
            {
                return(defaultHeight);
            }

            return(depth < SerializableSystemType.MaxSerializationDepth ? FieldUtil.HeightRequiredToDraw(type, depth + 1) : defaultHeight);
        }