Esempio n. 1
0
        private static void ApplyDefaultAsSingle(SerializedProperty property, System.Type restrictionType, EntityRelativity relativity)
        {
            object value = property.GetPropertyValue(false);

            if (value != null)
            {
                return;
            }

            var targ = GameObjectUtil.GetGameObjectFromSource(property.serializedObject.targetObject);

            if (object.ReferenceEquals(targ, null))
            {
                value = ObjUtil.GetAsFromSource(restrictionType, property.serializedObject.targetObject);
            }
            else
            {
                value = GetFromTarget(targ, restrictionType, relativity);
            }

            if (value != null)
            {
                property.SetPropertyValue(value);
            }
        }
Esempio n. 2
0
 public ForceFromSelfAttribute(EntityRelativity relativity)
 {
     this.Relativity = relativity;
 }
Esempio n. 3
0
        public static object GetFromTarget(GameObject targ, System.Type restrictionType, EntityRelativity relativity)
        {
            switch (relativity)
            {
            case EntityRelativity.Entity:
            {
                targ = targ.FindRoot();

                var obj = ObjUtil.GetAsFromSource(restrictionType, targ);
                if (object.ReferenceEquals(obj, null) && ComponentUtil.IsAcceptableComponentType(restrictionType))
                {
                    obj = targ.GetComponentInChildren(restrictionType);
                }
                return(obj);
            }

            case EntityRelativity.Self:
            {
                return(ObjUtil.GetAsFromSource(restrictionType, targ));
            }

            case EntityRelativity.SelfAndChildren:
            {
                var obj = ObjUtil.GetAsFromSource(restrictionType, targ);
                if (object.ReferenceEquals(targ, null) && ComponentUtil.IsAcceptableComponentType(restrictionType))
                {
                    obj = targ.GetComponentInChildren(restrictionType);
                }
                return(obj);
            }

            default:
                return(null);
            }
        }
Esempio n. 4
0
        private static void ApplyDefaultAsList(SerializedProperty property, System.Type elementType, System.Type restrictionType, EntityRelativity relativity)
        {
            if (property.arraySize != 0)
            {
                return;
            }
            if (elementType == null || restrictionType == null)
            {
                return;
            }

            if (TypeUtil.IsType(elementType, typeof(VariantReference)))
            {
                var targ = GameObjectUtil.GetGameObjectFromSource(property.serializedObject.targetObject);
                if (object.ReferenceEquals(targ, null))
                {
                    var obj = ObjUtil.GetAsFromSource(restrictionType, property.serializedObject.targetObject);
                    if (obj != null)
                    {
                        property.arraySize = 1;
                        var variant = EditorHelper.GetTargetObjectOfProperty(property.GetArrayElementAtIndex(0)) as VariantReference;
                        if (variant == null)
                        {
                            return;
                        }
                        variant.Value = obj;
                        property.serializedObject.Update();
                        GUI.changed = true;
                    }
                    else if (property.arraySize > 0)
                    {
                        property.arraySize = 0;
                        GUI.changed        = true;
                    }
                    return;
                }

                switch (relativity)
                {
                case EntityRelativity.Entity:
                {
                    targ = targ.FindRoot();
                    var arr = ObjUtil.GetAllFromSource(restrictionType, targ, true);

                    property.arraySize = arr.Length;
                    for (int i = 0; i < arr.Length; i++)
                    {
                        var variant = EditorHelper.GetTargetObjectOfProperty(property.GetArrayElementAtIndex(i)) as VariantReference;
                        if (variant != null)
                        {
                            variant.Value = arr[i];
                        }
                    }
                    property.serializedObject.Update();
                    GUI.changed = true;
                }
                break;

                case EntityRelativity.Self:
                {
                    var arr = ObjUtil.GetAllFromSource(restrictionType, targ, false);

                    property.arraySize = arr.Length;
                    for (int i = 0; i < arr.Length; i++)
                    {
                        var variant = EditorHelper.GetTargetObjectOfProperty(property.GetArrayElementAtIndex(i)) as VariantReference;
                        if (variant != null)
                        {
                            variant.Value = arr[i];
                        }
                    }
                    property.serializedObject.Update();
                    GUI.changed = true;
                }
                break;

                case EntityRelativity.SelfAndChildren:
                {
                    var arr = ObjUtil.GetAllFromSource(restrictionType, targ, true);

                    property.arraySize = arr.Length;
                    for (int i = 0; i < arr.Length; i++)
                    {
                        var variant = EditorHelper.GetTargetObjectOfProperty(property.GetArrayElementAtIndex(i)) as VariantReference;
                        if (variant != null)
                        {
                            variant.Value = arr[i];
                        }
                    }
                    property.serializedObject.Update();
                    GUI.changed = true;
                }
                break;
                }
            }
            else if (TypeUtil.IsType(elementType, typeof(UnityEngine.Object)))
            {
                var targ = GameObjectUtil.GetGameObjectFromSource(property.serializedObject.targetObject);
                if (object.ReferenceEquals(targ, null))
                {
                    var obj = ObjUtil.GetAsFromSource(restrictionType, property.serializedObject.targetObject) as UnityEngine.Object;
                    if (obj != null)
                    {
                        property.arraySize = 1;
                        property.GetArrayElementAtIndex(0).objectReferenceValue = obj;
                        GUI.changed = true;
                    }
                    else if (property.arraySize > 0)
                    {
                        property.arraySize = 0;
                        GUI.changed        = true;
                    }
                    return;
                }

                switch (relativity)
                {
                case EntityRelativity.Entity:
                {
                    targ = targ.FindRoot();
                    var arr = ObjUtil.GetAllFromSource(restrictionType, targ, true);

                    property.arraySize = arr.Length;
                    for (int i = 0; i < arr.Length; i++)
                    {
                        property.GetArrayElementAtIndex(i).objectReferenceValue = arr[i] as UnityEngine.Object;
                    }
                }
                break;

                case EntityRelativity.Self:
                {
                    var arr = ObjUtil.GetAllFromSource(restrictionType, targ, false);

                    property.arraySize = arr.Length;
                    for (int i = 0; i < arr.Length; i++)
                    {
                        property.GetArrayElementAtIndex(i).objectReferenceValue = arr[i] as UnityEngine.Object;
                    }
                }
                break;

                case EntityRelativity.SelfAndChildren:
                {
                    var arr = ObjUtil.GetAllFromSource(restrictionType, targ, true);

                    property.arraySize = arr.Length;
                    for (int i = 0; i < arr.Length; i++)
                    {
                        property.GetArrayElementAtIndex(i).objectReferenceValue = arr[i] as UnityEngine.Object;
                    }
                }
                break;
                }
            }
        }
        private static void ApplyDefaultAsSingle(SerializedProperty property, System.Type fieldType, System.Type restrictionType, EntityRelativity relativity)
        {
            if (fieldType == null)
            {
                return;
            }

            if (TypeUtil.IsType(fieldType, typeof(VariantReference)))
            {
                var variant = EditorHelper.GetTargetObjectOfProperty(property) as VariantReference;
                if (variant == null)
                {
                    return;
                }
                if (variant.Value != null)
                {
                    return;
                }

                var targ = GameObjectUtil.GetGameObjectFromSource(property.serializedObject.targetObject);
                if (targ == null)
                {
                    var obj = ObjUtil.GetAsFromSource(restrictionType, property.serializedObject.targetObject);
                    if (obj != null)
                    {
                        variant.Value = obj;
                        property.serializedObject.Update();
                        GUI.changed = true;
                    }
                    return;
                }

                switch (relativity)
                {
                case EntityRelativity.Entity:
                {
                    targ = targ.FindRoot();

                    var obj = ObjUtil.GetAsFromSource(restrictionType, targ);
                    if (obj == null && ComponentUtil.IsAcceptableComponentType(restrictionType))
                    {
                        obj = targ.GetComponentInChildren(restrictionType);
                    }
                    if (obj != null)
                    {
                        variant.Value = obj;
                        property.serializedObject.Update();
                    }
                }
                break;

                case EntityRelativity.Self:
                {
                    var obj = ObjUtil.GetAsFromSource(restrictionType, targ);
                    if (obj != null)
                    {
                        variant.Value = obj;
                        property.serializedObject.Update();
                    }
                }
                break;

                case EntityRelativity.SelfAndChildren:
                {
                    var obj = ObjUtil.GetAsFromSource(restrictionType, targ);
                    if (obj == null && ComponentUtil.IsAcceptableComponentType(restrictionType))
                    {
                        obj = targ.GetComponentInChildren(restrictionType);
                    }
                    if (obj != null)
                    {
                        variant.Value = obj;
                        property.serializedObject.Update();
                    }
                }
                break;
                }
            }
            else if (property.propertyType == SerializedPropertyType.ObjectReference)
            {
                if (property.objectReferenceValue != null)
                {
                    return;
                }

                var targ = GameObjectUtil.GetGameObjectFromSource(property.serializedObject.targetObject);
                if (targ == null)
                {
                    property.objectReferenceValue = ObjUtil.GetAsFromSource(restrictionType, property.serializedObject.targetObject) as UnityEngine.Object;
                    return;
                }

                switch (relativity)
                {
                case EntityRelativity.Entity:
                {
                    targ = targ.FindRoot();

                    var obj = ObjUtil.GetAsFromSource(restrictionType, targ) as UnityEngine.Object;
                    if (obj == null && ComponentUtil.IsAcceptableComponentType(restrictionType))
                    {
                        obj = targ.GetComponentInChildren(restrictionType);
                    }
                    if (obj != null)
                    {
                        property.objectReferenceValue = obj;
                        GUI.changed = true;
                    }
                }
                break;

                case EntityRelativity.Self:
                {
                    var obj = ObjUtil.GetAsFromSource(restrictionType, targ) as UnityEngine.Object;
                    if (obj != null)
                    {
                        property.objectReferenceValue = obj;
                        GUI.changed = true;
                    }
                }
                break;

                case EntityRelativity.SelfAndChildren:
                {
                    var obj = ObjUtil.GetAsFromSource(restrictionType, targ) as UnityEngine.Object;
                    if (obj == null && ComponentUtil.IsAcceptableComponentType(restrictionType))
                    {
                        obj = targ.GetComponentInChildren(restrictionType);
                    }
                    if (obj != null)
                    {
                        property.objectReferenceValue = obj;
                        GUI.changed = true;
                    }
                }
                break;
                }
            }
        }
Esempio n. 6
0
        private void ApplyDefaultAsSingle(SerializedProperty property, System.Type fieldType, EntityRelativity relativity)
        {
            if (object.ReferenceEquals(property.objectReferenceValue, null))
            {
                return;
            }

            var self = GameObjectUtil.GetGameObjectFromSource(property.serializedObject.targetObject);

            if (object.ReferenceEquals(self, null))
            {
                return;
            }

            var go = GameObjectUtil.GetGameObjectFromSource(property.objectReferenceValue);

            if (object.ReferenceEquals(go, null))
            {
                return;
            }


            switch (relativity)
            {
            case EntityRelativity.Entity:
                if (go.FindRoot() != self.FindRoot())
                {
                    property.objectReferenceValue = null;
                }
                break;

            case EntityRelativity.Self:
                if (go != self)
                {
                    property.objectReferenceValue = null;
                }
                break;

            case EntityRelativity.SelfAndChildren:
                if (go != self && !self.IsParentOf(go))
                {
                    property.objectReferenceValue = null;
                }
                break;
            }
        }
Esempio n. 7
0
 public DefaultFromSelfAttribute(EntityRelativity relativity = EntityRelativity.Self)
 {
     this.Relativity = relativity;
 }