Ejemplo n.º 1
0
        private static void FillProperty(MyEditor.ObjectField property)
        {
            var apAttribute = property.Field
                              .GetCustomAttributes(typeof(AutoPropertyAttribute), true)
                              .FirstOrDefault() as AutoPropertyAttribute;

            if (apAttribute == null)
            {
                return;
            }
            Func <Object, bool> predicateMethod = apAttribute.PredicateMethodTarget == null ?
                                                  apAttribute.PredicateMethodName == null ?
                                                  _ => true :
                                                  (Func <Object, bool>)Delegate.CreateDelegate(typeof(Func <Object, bool>),
                                                                                               property.Context,
                                                                                               apAttribute.PredicateMethodName) :
                                                  (Func <Object, bool>)Delegate.CreateDelegate(typeof(Func <Object, bool>),
                                                                                               apAttribute.PredicateMethodTarget,
                                                                                               apAttribute.PredicateMethodName);

            var matchedObjects = ObjectsGetters[apAttribute.Mode]
                                 .Invoke(property, predicateMethod);

            if (property.Field.FieldType.IsArray)
            {
                if (matchedObjects != null && matchedObjects.Length > 0)
                {
                    var serializedObject   = new SerializedObject(property.Context);
                    var serializedProperty = serializedObject.FindProperty(property.Field.Name);
                    serializedProperty.ReplaceArray(matchedObjects);
                    serializedObject.ApplyModifiedProperties();
                    return;
                }
            }
            else
            {
                var obj = matchedObjects.FirstOrDefault();
                if (obj != null)
                {
                    var serializedObject   = new SerializedObject(property.Context);
                    var serializedProperty = serializedObject.FindProperty(property.Field.Name);
                    serializedProperty.objectReferenceValue = obj;
                    serializedObject.ApplyModifiedProperties();
                    return;
                }
            }

            Debug.LogError($"{property.Context.name} caused: {property.Field.Name} is failed to Auto Assign property. No match",
                           property.Context);
        }
Ejemplo n.º 2
0
        private static void FillProperty(MyEditor.ObjectField property)
        {
            var apAttribute = property.Field
                              .GetCustomAttributes(typeof(AutoPropertyAttribute), true)
                              .FirstOrDefault() as AutoPropertyAttribute;

            if (apAttribute == null)
            {
                return;
            }

            if (property.Field.FieldType.IsArray)
            {
                var objects = MultipleObjectsGetters[apAttribute.Mode].Invoke(property);
                if (objects != null && objects.Length > 0)
                {
                    var serializedObject   = new SerializedObject(property.Context);
                    var serializedProperty = serializedObject.FindProperty(property.Field.Name);
                    serializedProperty.ReplaceArray(objects);
                    serializedObject.ApplyModifiedProperties();
                    return;
                }
            }
            else
            {
                var obj = SingularObjectGetters[apAttribute.Mode].Invoke(property);
                if (obj != null)
                {
                    var serializedObject   = new SerializedObject(property.Context);
                    var serializedProperty = serializedObject.FindProperty(property.Field.Name);
                    serializedProperty.objectReferenceValue = obj;
                    serializedObject.ApplyModifiedProperties();
                    return;
                }
            }

            Debug.LogError($"{property.Context.name} caused: {property.Field.Name} is failed to Auto Assign property. No match",
                           property.Context);
        }