private void OnEnable()
        {
            _script = target.GetScript();
            _target = new SerializedObject(target);

            var        methodTargetingDataArray = _target.FindProperty("_methodTargetingData");
            List <int> indexesToClear           = new List <int>();

            for (int i = methodTargetingDataArray.arraySize - 1; i >= 0; i--)
            {
                var sceneGuid = methodTargetingDataArray.GetArrayElementAtIndex(i).FindPropertyRelative("_sceneGuid").stringValue;
                var objectID  = methodTargetingDataArray.GetArrayElementAtIndex(i).FindPropertyRelative("_objectID").intValue;

                Object obj = UnityEditorObjectExtensions.GetObjectByInstanceID(objectID, sceneGuid);

                if (!obj)
                {
                    indexesToClear.Add(i);
                }
                else
                {
                    _references.Add(obj);
                }
            }

            if (indexesToClear.Count > 0)
            {
                for (int i = 0; i < indexesToClear.Count; i++)
                {
                    methodTargetingDataArray.DeleteArrayElementAtIndex(indexesToClear[i]);
                }

                _target.ApplyModifiedProperties();

                AssetDatabase.SaveAssets();
            }
        }
예제 #2
0
        protected void DrawButtons()
        {
            for (int i = 0; i < _buttonMethods.Count; i++)
            {
                if (GUILayout.Button(_buttonMethods[i].Name))
                {
                    object[] parameters = null;

                    if (_buttonMethods[i].GetParameters().Length > 0)
                    {
                        var param = _container.GetParameters(_objectID, _sceneGuid, _buttonMethods[i].Name, _buttonMethods[i].GetParameters().Length);

                        if (param != null)
                        {
                            parameters = new object[param.Parameters.Length];

                            for (int t = 0; t < parameters.Length; t++)
                            {
                                Type parameterType = Type.GetType(param.Parameters[t].Type);

                                if (parameterType.IsSubclassOf(typeof(EditorField)))
                                {
                                    var editorField = JsonUtility.FromJson(param.Parameters[t].JsonData, parameterType);
                                    var fieldValue  = editorField.GetType().BaseType.GetField("_fieldValue", BindingFlags.Instance | BindingFlags.NonPublic);
                                    parameters[t] = fieldValue.GetValue(editorField);
                                }
                                else if (parameterType == typeof(UnityEngine.Object) || parameterType.IsSubclassOf(typeof(UnityEngine.Object)))
                                {
                                    StringCollectionField stringCollectionField = JsonUtility.FromJson <StringCollectionField>(param.Parameters[t].JsonData);

                                    string sceneGuid = stringCollectionField.FieldValue[0];
                                    int    objectID  = int.Parse(stringCollectionField.FieldValue[1]);

                                    parameters[t] = UnityEditorObjectExtensions.GetObjectByInstanceID(objectID, sceneGuid);
                                }
                                else if (parameterType.GetElementType() != null && (parameterType.GetElementType() == typeof(UnityEngine.Object) ||
                                                                                    parameterType.GetElementType().IsSubclassOf(typeof(UnityEngine.Object))))
                                {
                                    StringCollectionField stringCollectionField = JsonUtility.FromJson <StringCollectionField>(param.Parameters[t].JsonData);

                                    List <int>    objectIDs  = new List <int>();
                                    List <string> sceneGuids = new List <string>();
                                    IList         objects    = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(parameterType.GetElementType()));

                                    for (int c = 0; c < stringCollectionField.FieldValue.Length; c++)
                                    {
                                        if (c % 2 == 0)
                                        {
                                            sceneGuids.Add(stringCollectionField.FieldValue[c]);
                                        }
                                        else
                                        {
                                            objectIDs.Add(int.Parse(stringCollectionField.FieldValue[c]));
                                        }
                                    }

                                    for (int o = 0; o < sceneGuids.Count; o++)
                                    {
                                        objects.Add(UnityEditorObjectExtensions.GetObjectByInstanceID(objectIDs[o], sceneGuids[o]));
                                    }

                                    var castMethod = typeof(Enumerable).GetMethod("Cast", BindingFlags.Public | BindingFlags.Static).
                                                     MakeGenericMethod(parameterType.GetElementType());

                                    var toArrayMethod = typeof(Enumerable).GetMethod("ToArray", BindingFlags.Public | BindingFlags.Static).
                                                        MakeGenericMethod(parameterType.GetElementType());

                                    parameters[t] = toArrayMethod.Invoke(null, new object[] { castMethod.Invoke(null, new object[] { objects }) });
                                }
                                else
                                {
                                    parameters[t] = JsonUtility.FromJson(param.Parameters[t].JsonData, parameterType);
                                }
                            }
                        }
                        else
                        {
                            MakeParametersWindow.ShowWindow(_container, _objectID, _sceneGuid, _buttonMethods[i].Name, _buttonMethods[i].GetParameters(), target);
                            return;
                        }
                    }

                    _buttonMethods[i].Invoke(target, parameters);
                }
            }
        }