Пример #1
0
            //The equivalent of a SerializedPropertyField but for objects serialized using JSON.
            public static T ObjectField <T>(T obj, GUIContent label, out bool dataChanged)
            {
                //If object is an array show an editable array field
                if (typeof(T).IsArray)
                {
                    bool  arrayChanged = false;
                    Array arrayObj     = obj as Array;
                    arrayObj = ArrayField(label, arrayObj, typeof(T).GetElementType(), ref arrayChanged);

                    if (arrayChanged)
                    {
                        dataChanged = true;
                        return((T)(arrayObj as object));
                    }

                    dataChanged = false;
                    return(obj);
                }

                //If the object is a IJSONCustomEditable then just need to call its render properties function.
                if (obj != null && obj is IJSONCustomEditable)
                {
                    dataChanged = ((IJSONCustomEditable)obj).RenderObjectProperties(label);
                    return(obj);
                }

                Type objType = obj == null ? typeof(T) : obj.GetType();

                //Otherwise check the class has a object editor class associated with it.
                JSONEditorAttribute.RenderPropertiesDelegate renderPropertiesDelegate = GetEditorDelegateForObject(objType);
                if (renderPropertiesDelegate != null)
                {
                    //If it has one then just need to call its render properties function.
                    return((T)renderPropertiesDelegate(obj, label, out dataChanged));
                }

                //Otherwise loop through each JSON field in object and render each as a property field
                {
                    dataChanged = false;
                    JSONField[] JSONFields = JSONConverter.GetJSONFields(objType);
                    foreach (JSONField JSONField in JSONFields)
                    {
                        if (!JSONField.HideInEditor())
                        {
                            //Create GUIContent for label and optional tooltip
                            string           fieldName       = StringUtils.FromCamelCase(JSONField.GetID());
                            TooltipAttribute fieldToolTipAtt = SystemUtils.GetAttribute <TooltipAttribute>(JSONField);
                            GUIContent       labelContent    = fieldToolTipAtt != null ? new GUIContent(fieldName, fieldToolTipAtt.tooltip) : new GUIContent(fieldName);

                            bool   fieldChanged;
                            object nodeFieldObject = JSONField.GetValue(obj);

                            if (JSONField.GetFieldType().IsArray)
                            {
                                fieldChanged    = false;
                                nodeFieldObject = ArrayField(labelContent, nodeFieldObject as Array, JSONField.GetFieldType().GetElementType(), ref fieldChanged);
                            }
                            else
                            {
                                nodeFieldObject = ObjectField(nodeFieldObject, labelContent, out fieldChanged);
                            }

                            if (fieldChanged)
                            {
                                dataChanged = true;
                                JSONField.SetValue(obj, nodeFieldObject);
                            }
                        }
                    }

                    return(obj);
                }
            }