コード例 #1
0
        void Start()
        {
            var some = new Some <int>(new List <int>()
            {
                1, 2, 3
            });

            var json = JsonUtility.ToJson(some);

            try
            {
                var t = JsonUtility.FromJson(json, typeof(Some <int>));
                //var t = JsonUtility.FromJson<Some<int>>(json);
            }
            catch (Exception)
            {
                Debug.Log("Cant parse with generic");

                try
                {
                }
                catch (Exception)
                {
                    Debug.Log("Cant parse");
                }
            }

            //s = _d.ToJsonString();
            forgeObject = ForgeObject.Polymorph(forgeObject);
        }
コード例 #2
0
        public static ForgeObject Clone(ForgeObject forgeObject)
        {
            var clone = (ForgeObject)forgeObject.MemberwiseClone();

            clone.Init();
            return(clone);
        }
コード例 #3
0
 private void DrawSingleElement(SerializedProperty property, ForgeObject forgeObject, FieldInfo fieldInfo, ref Rect position)
 {
     if (fieldInfo.FieldType == typeof(int))
     {
         DrawIntElement(property, forgeObject, fieldInfo, ref position);
     }
     else if (fieldInfo.FieldType == typeof(string))
     {
         DrawStringElement(property, forgeObject, fieldInfo, ref position);
     }
     else if (fieldInfo.FieldType == typeof(UnityEngine.Object) || fieldInfo.FieldType.IsSubclassOf(typeof(UnityEngine.Object)))
     {
         DrawObjectElement(property, forgeObject, fieldInfo, ref position);
     }
 }
コード例 #4
0
        private void DrawSingleElementInPlayMode(ForgeObject forgeObject, FieldInfo fieldInfo, ref Rect position)
        {
            if (fieldInfo.FieldType == typeof(int))
            {
                EditorGUI.IntField(position, new GUIContent(fieldInfo.Name), (int)fieldInfo.GetValue(forgeObject));
            }
            else if (fieldInfo.FieldType == typeof(string))
            {
                EditorGUI.TextField(position, new GUIContent(fieldInfo.Name), (string)fieldInfo.GetValue(forgeObject));
            }
            else if (fieldInfo.FieldType == typeof(UnityEngine.Object) || fieldInfo.FieldType.IsSubclassOf(typeof(UnityEngine.Object)))
            {
                EditorGUI.ObjectField(position, new GUIContent(fieldInfo.Name), (UnityEngine.Object)fieldInfo.GetValue(forgeObject), fieldInfo.FieldType, true);
            }

            position.y += EditorGUIUtility.singleLineHeight;
        }
コード例 #5
0
        /// <summary>
        /// This will get all fields that the Json utility can access in this ForgeObject and nested ForgeObjects.
        /// Wont get fields from non nested ForgeObjects.
        /// </summary>
        /// <returns>Info about all fields which the JsonUtility can access.</returns>
        public FieldInfo[] GetForgeObjectJsonFields()
        {
            if (_jsonFields == null)
            {
                _childParentMap = new Dictionary <FieldInfo, FieldInfo>();
                List <FieldInfo> fieldInfos = new List <FieldInfo>();

                _fieldNames = GetJsonFieldNames();

                for (int i = 0; i < _fieldNames.Length; i++)
                {
                    FieldInfo field;
                    Type      current   = GetType();
                    string    fieldName = _fieldNames[i];

                    do
                    {
                        field = current.GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);

                        current = current.BaseType;
                    } while (field == null && current != typeof(ForgeObject));

                    if (field != null)
                    {
                        if (field.FieldType.IsSubclassOf(typeof(ForgeObject)))
                        {
                            ForgeObject nested = (ForgeObject)field.GetValue(this);

                            var nestedFields = nested.GetForgeObjectJsonFields();
                            fieldInfos.AddRange(nestedFields);

                            for (int t = 0; t < nestedFields.Length; t++)
                            {
                                _childParentMap.Add(nestedFields[t], field);
                            }
                        }

                        fieldInfos.Add(field);
                    }
                }

                _jsonFields = fieldInfos.ToArray();
            }

            return(_jsonFields);
        }
コード例 #6
0
        private void DrawIntElement(SerializedProperty property, ForgeObject forgeObject, FieldInfo fieldInfo, ref Rect position)
        {
            int current = (int)fieldInfo.GetValue(forgeObject);

            EditorGUI.BeginChangeCheck();

            current = EditorGUI.IntField(position, fieldInfo.Name, current);

            if (EditorGUI.EndChangeCheck())
            {
                fieldInfo.SetValue(forgeObject, current);

                property.FindPropertyRelative("_polymorphismJsonData").stringValue = forgeObject.ToJsonString();
                property.serializedObject.ApplyModifiedProperties();
            }

            position.y += EditorGUIUtility.singleLineHeight;
        }
コード例 #7
0
        private void DrawObjectElement(SerializedProperty property, ForgeObject forgeObject, FieldInfo fieldInfo, ref Rect position)
        {
            UnityEngine.Object current = (UnityEngine.Object)fieldInfo.GetValue(forgeObject);

            EditorGUI.BeginChangeCheck();

            current = EditorGUI.ObjectField(position, fieldInfo.Name, current, fieldInfo.FieldType, !property.serializedObject.targetObject.IsAsset());

            if (EditorGUI.EndChangeCheck())
            {
                fieldInfo.SetValue(forgeObject, current);
                current.GetSceneGuidAndObjectID(out string sceneGuid, out long objectID);

                var    pairs     = property.FindPropertyRelative("_pairs");
                string fieldPath = $"{fieldInfo.DeclaringType}.{fieldInfo.Name}";
                var    element   = pairs.FindInArray(s => s.FindPropertyRelative("_fieldPath").stringValue == fieldPath, out int index);

                if (index == -1)
                {
                    pairs.arraySize++;
                    var newestElement = pairs.GetArrayElementAtIndex(pairs.arraySize - 1);

                    newestElement.FindPropertyRelative("_key").stringValue            = $"{{_sceneGuid:{sceneGuid}, _objectID:{objectID}}}";
                    newestElement.FindPropertyRelative("_value").objectReferenceValue = current;
                    newestElement.FindPropertyRelative("_fieldPath").stringValue      = fieldPath;
                }
                else
                {
                    element.FindPropertyRelative("_key").stringValue            = $"{{_sceneGuid:{sceneGuid}, _objectID:{objectID}}}";
                    element.FindPropertyRelative("_value").objectReferenceValue = current;
                }

                property.FindPropertyRelative("_polymorphismJsonData").stringValue = forgeObject.ToJsonString();
                property.serializedObject.ApplyModifiedProperties();
            }

            position.y += EditorGUIUtility.singleLineHeight;
        }
コード例 #8
0
        public static ForgeObject Polymorph(ForgeObject original, Type forgeObjectChildType)
        {
            if (original._polymorphEnabled)
            {
                original._polymorphismJsonData = InsertTargetObjectInstanceID(original._polymorphismJsonData, original._pairs);
                var clone = CreateFromJson(original._polymorphismJsonData, forgeObjectChildType);

#if UNITY_EDITOR
                if (UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode)
                {
                    clone._polymorphEnabled     = original._polymorphEnabled;
                    clone._polymorphismType     = original._polymorphismType;
                    clone._polymorphismJsonData = original._polymorphismJsonData;
                }
#endif
                return(clone);
            }
            else
            {
                original.Init();
                return(original);
            }
        }
コード例 #9
0
 private void DrawCollection(SerializedProperty property, ForgeObject forgeObject, FieldInfo fieldInfo, ref Rect position)
 {
 }
コード例 #10
0
        private void Init(SerializedProperty property)
        {
            Selection.selectionChanged -= OnEditorLostFocus;
            Selection.selectionChanged += OnEditorLostFocus;

            if (_derivedtypes == null)
            {
                List <string> derivedtypes = new List <string>();

                if (fieldInfo.FieldType.IsArray)
                {
                    derivedtypes = ForgeObject.EditorFindDerivedTypeNames(fieldInfo.FieldType.GetElementType()).ToList();
                }
                else
                {
                    derivedtypes = ForgeObject.EditorFindDerivedTypeNames(fieldInfo.FieldType).ToList();
                }

                if (derivedtypes.Count > 0 && fieldInfo.FieldType != typeof(ForgeObject))
                {
                    derivedtypes.Insert(0, DONT_POLYMORPH);
                }

                _derivedtypes       = derivedtypes.ToArray();
                _displayDerivedType = new string[_derivedtypes.Length];

                for (int i = 0; i < _derivedtypes.Length; i++)
                {
                    _displayDerivedType[i] = _derivedtypes[i].Split(',')[0];
                }
            }

            var type = property.FindPropertyRelative("_polymorphismType");

            if (!string.IsNullOrEmpty(type.stringValue) && type.stringValue != DONT_POLYMORPH)
            {
                var forgeObjectType = Type.GetType(type.stringValue);

                ForgeObject forgeObject = null;

                var polymorphJsonData = property.FindPropertyRelative("_polymorphismJsonData");

                if (!string.IsNullOrEmpty(polymorphJsonData.stringValue))
                {
                    var original = fieldInfo.GetValue(property.serializedObject.targetObject);

                    forgeObject = ForgeObject.Polymorph((ForgeObject)original, forgeObjectType);
                }
                else
                {
                    try
                    {
                        forgeObject = ForgeObject.Create(forgeObjectType);
                    }
                    catch (MissingMethodException)
                    {
                        forgeObject = ForgeObject.CreateUninitialized(forgeObjectType);
                    }
                }

                _initialized.Add(property.propertyPath, new Tuple <ForgeObject, FieldInfo[]>(forgeObject, forgeObject.GetForgeObjectJsonFields()));
            }
            else if (_derivedtypes.Length > 0 && fieldInfo.FieldType == typeof(ForgeObject))
            {
                var forgeObjectType = Type.GetType(_derivedtypes[0]);

                ForgeObject forgeObject = null;

                var polymorphJsonData = property.FindPropertyRelative("_polymorphismJsonData");

                if (!string.IsNullOrEmpty(polymorphJsonData.stringValue))
                {
                    var original = fieldInfo.GetValue(property.serializedObject.targetObject);

                    forgeObject = ForgeObject.Polymorph((ForgeObject)original, forgeObjectType);
                }
                else
                {
                    try
                    {
                        forgeObject = ForgeObject.Create(forgeObjectType);
                    }
                    catch (MissingMethodException)
                    {
                        forgeObject = ForgeObject.CreateUninitialized(forgeObjectType);
                    }
                }

                _initialized.Add(property.propertyPath, new Tuple <ForgeObject, FieldInfo[]>(forgeObject, forgeObject.GetForgeObjectJsonFields()));
            }
            else
            {
                _initialized.Add(property.propertyPath, null);
            }
        }
コード例 #11
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            if (!_initialized.ContainsKey(property.propertyPath))
            {
                Init(property);
            }

            position.height = EditorGUIUtility.singleLineHeight;

            if (_derivedtypes.Contains(DONT_POLYMORPH))
            {
                EditorGUI.PropertyField(position, property, label, false);
            }
            else
            {
                property.isExpanded = EditorGUI.Foldout(position, property.isExpanded, label);
            }

            if (property.isExpanded)
            {
                position.y += EditorGUIUtility.singleLineHeight;
                EditorGUI.indentLevel++;

                if (_derivedtypes.Length > 0)
                {
                    var type = property.FindPropertyRelative("_polymorphismType");

                    int selected = Array.IndexOf(_derivedtypes, type.stringValue);

                    if (selected < 0)
                    {
                        selected = 0;
                    }

                    if (!EditorApplication.isPlayingOrWillChangePlaymode)
                    {
                        EditorGUI.BeginChangeCheck();

                        selected = EditorGUI.Popup(position, "Polymorph", selected, _displayDerivedType);

                        position.y      += EditorGUIUtility.singleLineHeight;
                        type.stringValue = _derivedtypes[selected];

                        if (EditorGUI.EndChangeCheck() && _derivedtypes[selected] != DONT_POLYMORPH)
                        {
                            property.FindPropertyRelative("_polymorphEnabled").boolValue       = true;
                            property.FindPropertyRelative("_polymorphismJsonData").stringValue = string.Empty;
                            property.FindPropertyRelative("_pairs").arraySize = 0;

                            property.serializedObject.ApplyModifiedProperties();
                            var         forgeObjectType = Type.GetType(type.stringValue);
                            ForgeObject forgeObject     = null;

                            try
                            {
                                forgeObject = ForgeObject.Create(forgeObjectType);
                            }
                            catch (MissingMethodException)
                            {
                                forgeObject = ForgeObject.CreateUninitialized(forgeObjectType);
                            }

                            _initialized[property.propertyPath] = new Tuple <ForgeObject, FieldInfo[]>(forgeObject, forgeObject.GetForgeObjectJsonFields());

                            Draw(position, property);
                        }
                        else if (_derivedtypes[selected] != DONT_POLYMORPH)
                        {
                            Draw(position, property);
                        }
                        else
                        {
                            if (fieldInfo.FieldType != typeof(ForgeObject))
                            {
                                var iterator = property.Copy();

                                while (iterator.NextVisible(true))
                                {
                                    EditorGUI.PropertyField(position, iterator);
                                    position.y += EditorGUIUtility.singleLineHeight;
                                }
                            }

                            property.FindPropertyRelative("_polymorphEnabled").boolValue       = false;
                            property.FindPropertyRelative("_polymorphismJsonData").stringValue = string.Empty;
                            property.FindPropertyRelative("_pairs").arraySize = 0;

                            _initialized[property.propertyPath] = null;
                            property.serializedObject.ApplyModifiedProperties();
                        }
                    }
                    else
                    {
                        GUI.enabled = false;
                        selected    = EditorGUI.Popup(position, "Polymorph", selected, _displayDerivedType);

                        position.y      += EditorGUIUtility.singleLineHeight;
                        type.stringValue = _derivedtypes[selected];

                        if (_initialized[property.propertyPath] != null)
                        {
                            for (int i = 0; i < _initialized[property.propertyPath].Item2.Length; i++)
                            {
                                if (_initialized[property.propertyPath].Item2[i].DeclaringType != fieldInfo.FieldType)
                                {
                                    DrawSingleElementInPlayMode(_initialized[property.propertyPath].Item1, _initialized[property.propertyPath].Item2[i], ref position);
                                }
                            }
                        }

                        GUI.enabled = true;

                        var iterator = property.Copy();

                        while (iterator.NextVisible(true))
                        {
                            EditorGUI.PropertyField(position, iterator);
                            position.y += EditorGUIUtility.singleLineHeight;
                        }
                    }
                }
                else
                {
                    var iterator = property.Copy();

                    while (iterator.NextVisible(true))
                    {
                        EditorGUI.PropertyField(position, iterator);
                        position.y += EditorGUIUtility.singleLineHeight;
                    }
                }
            }
        }