Exemple #1
0
        private static UnityObject AddComponent(Dictionary <long, UnityObject> objects, GameObject go, HashSet <Type> requirements, PersistentDescriptor componentDescriptor, Type componentType)
        {
            Component component;
            bool      maybeComponentAlreadyAdded =
                requirements.Contains(componentType) ||
                componentType.IsSubclassOf(typeof(Transform)) ||
                componentType == typeof(Transform) ||
                componentType.IsDefined(typeof(DisallowMultipleComponent), true) ||
                m_dependencies.ContainsKey(componentType) && m_dependencies[componentType].Any(d => go.GetComponent(d) != null);

            if (maybeComponentAlreadyAdded)
            {
                component = go.GetComponent(componentType);
                if (component == null)
                {
                    component = go.AddComponent(componentType);
                }
            }
            else
            {
                component = go.AddComponent(componentType);
                if (component == null)
                {
                    component = go.GetComponent(componentType);
                }
            }
            if (component == null)
            {
                Debug.LogErrorFormat("Unable to add or get component of type {0}", componentType);
            }
            else
            {
                object[] requireComponents = component.GetType().GetCustomAttributes(typeof(RequireComponent), true);
                for (int j = 0; j < requireComponents.Length; ++j)
                {
                    RequireComponent requireComponent = requireComponents[j] as RequireComponent;
                    if (requireComponent != null)
                    {
                        if (requireComponent.m_Type0 != null && !requirements.Contains(requireComponent.m_Type0))
                        {
                            requirements.Add(requireComponent.m_Type0);
                        }
                        if (requireComponent.m_Type1 != null && !requirements.Contains(requireComponent.m_Type1))
                        {
                            requirements.Add(requireComponent.m_Type1);
                        }
                        if (requireComponent.m_Type2 != null && !requirements.Contains(requireComponent.m_Type2))
                        {
                            requirements.Add(requireComponent.m_Type2);
                        }
                    }
                }
                objects.Add(componentDescriptor.InstanceId, component);
            }

            return(component);
        }
Exemple #2
0
 public void TestAttribute()
 {
     foreach (Attribute at in Attribute.GetCustomAttributes(GetComponent <DerivedBehaviour>().GetType(), true))
     {
         if (at is RequireComponent)
         {
             RequireComponent rc = at as RequireComponent;
             Console.WriteLine("require type: " + rc.Type.Name);
         }
     }
 }
Exemple #3
0
        public static void RemoveDependancies(Component component)
        {
            if (component == null)
            {
                return;
            }

            Component[] components = component.GetComponents <Component>();
            if (components == null || components.Length == 0)
            {
                return;
            }

            System.Type compType = component.GetType();
            foreach (var c in components)
            {
                if (c == null)
                {
                    continue;
                }

                bool     deleteMe = false;
                object[] requires = c.GetType().GetCustomAttributes(typeof(RequireComponent), true);
                if (requires == null || requires.Length == 0)
                {
                    continue;
                }

                foreach (var r in requires)
                {
                    RequireComponent rc = r as RequireComponent;
                    if (rc == null)
                    {
                        continue;
                    }

                    if (rc.m_Type0 == compType ||
                        rc.m_Type1 == compType ||
                        rc.m_Type2 == compType)
                    {
                        deleteMe = true;
                        break;
                    }
                }

                if (deleteMe)
                {
                    Debug.LogWarningFormat("Deleting component dependency {0} found on {1}", c.GetType().Name, component.gameObject.name);

                    RemoveComponent(c);
                }
            }
        }
        /// <summary>
        /// List all the required types for a given component type.
        /// </summary>
        /// <param name="requieringType"></param>
        /// <returns></returns>
        internal static List <Type> GetRequieredTypes(Type requieringType)
        {
            List <Type> typesRequiredByComponent = new List <Type>();

            foreach (var requiredAttribute in Attribute.GetCustomAttributes(requieringType, typeof(RequireComponent)))
            {
                RequireComponent requiredType = (RequireComponent)requiredAttribute;
                TryAddRequiredComponent(typesRequiredByComponent, requiredType.m_Type0);
                TryAddRequiredComponent(typesRequiredByComponent, requiredType.m_Type1);
                TryAddRequiredComponent(typesRequiredByComponent, requiredType.m_Type2);
            }
            return(typesRequiredByComponent);
        }
        private static void RemoveDependencies(Component component)
        {
            if (component == null)
            {
                return;
            }

            Component[] components = component.GetComponents <Component>();
            if (components == null || components.Length == 0)
            {
                return;
            }

            System.Type compType = component.GetType();
            foreach (var c in components)
            {
                if (c == null)
                {
                    continue;
                }

                bool     deleteMe = false;
                object[] requires = c.GetType().GetCustomAttributes(typeof(RequireComponent), true);
                if (requires == null || requires.Length == 0)
                {
                    continue;
                }

                foreach (var r in requires)
                {
                    RequireComponent rc = r as RequireComponent;
                    if (rc == null)
                    {
                        continue;
                    }

                    if (rc.m_Type0 == compType ||
                        rc.m_Type1 == compType ||
                        rc.m_Type2 == compType)
                    {
                        deleteMe = true;
                        break;
                    }
                }

                if (deleteMe)
                {
                    RemoveComponent(c);
                }
            }
        }
Exemple #6
0
        public void Awake()
        {
            MemberInfo type = GetType();

            object[] customAttributes = type.GetCustomAttributes(inherit: true);
            for (int i = 0; i < customAttributes.Length; i++)
            {
                RequireComponent requireComponent = customAttributes[i] as RequireComponent;
                if (requireComponent != null)
                {
                    addDependency(requireComponent.m_Type0);
                    addDependency(requireComponent.m_Type1);
                    addDependency(requireComponent.m_Type2);
                }
            }
        }
Exemple #7
0
        public static void RemoveCameras(GameObject currentAvatar, bool localPlayer, bool friend)
        {
            if (!localPlayer)
            {
                foreach (Camera camera in currentAvatar.GetComponentsInChildren <Camera>(true))
                {
                    Debug.LogWarning("Removing camera from " + camera.gameObject.name);

                    if (friend && camera.targetTexture != null)
                    {
                        camera.enabled = false;
                    }
                    else
                    {
                        Component[] components = camera.gameObject.GetComponents <Component>();
                        foreach (var c in components)
                        {
                            bool     deleteMe = false;
                            object[] requires = c.GetType().GetCustomAttributes(typeof(RequireComponent), true);
                            foreach (var r in requires)
                            {
                                RequireComponent rc = r as RequireComponent;
                                if (rc.m_Type0 == typeof(Camera) ||
                                    rc.m_Type1 == typeof(Camera) ||
                                    rc.m_Type2 == typeof(Camera))
                                {
                                    deleteMe = true;
                                }
                            }

                            if (deleteMe)
                            {
                                MonoBehaviour.DestroyImmediate(c);
                            }
                        }

                        camera.enabled = false;
                        if (camera.targetTexture != null)
                        {
                            camera.targetTexture = new RenderTexture(16, 16, 24);
                        }
                        MonoBehaviour.DestroyImmediate(camera);
                    }
                }
            }
        }
Exemple #8
0
        static public IEnumerable <Type> GetRequiredComponentTypes(this RequireComponent item)
        {
            if (item.m_Type0 != null)
            {
                yield return(item.m_Type0);
            }

            if (item.m_Type1 != null)
            {
                yield return(item.m_Type1);
            }

            if (item.m_Type2 != null)
            {
                yield return(item.m_Type2);
            }
        }
Exemple #9
0
    List <Type> RequirementsSelectMany(List <Type> requirements)
    {
        List <Type> retList = new List <Type>();

        //var r = requirements.SelectMany(it =>

        //it.GetCustomAttributes(typeof(RequireComponent), true).

        //Select(itm => new[] { ((RequireComponent)itm).m_Type0, ((RequireComponent)itm).m_Type1, ((RequireComponent)itm).m_Type2 }).
        //SelectMany(itm => itm).Where(itm => itm != null).Distinct()).ToList();


        foreach (var it in requirements)
        {
            object[] attrs = it.GetCustomAttributes(typeof(RequireComponent), true);

            foreach (var at in attrs)
            {
                if (at != null)
                {
                    RequireComponent rc = (RequireComponent)at;

                    if (retList.Contains(rc.m_Type0) == false)
                    {
                        retList.Add(rc.m_Type0);
                    }

                    if (retList.Contains(rc.m_Type1) == false)
                    {
                        retList.Add(rc.m_Type1);
                    }

                    if (retList.Contains(rc.m_Type2) == false)
                    {
                        retList.Add(rc.m_Type2);
                    }
                }
            }

            //attrs.Select()
        }


        return(retList);
    }
Exemple #10
0
        private void AttachComponents()
        {
            Attribute[] attrs = Attribute.GetCustomAttributes(this.GetType());

            foreach (Attribute item in attrs)
            {
                RequireComponent componentAttribute = item as RequireComponent;

                if (componentAttribute == null)
                {
                    continue;
                }

                foreach (Type componentType in componentAttribute.ComponentTypes)
                {
                    // Add new component only when it's needed
                    if (GetComponentOfType(componentType) == null)
                    {
                        Component instance = Activator.CreateInstance(componentType) as Component;
                        AddComponent(instance);
                    }
                }
            }
        }
Exemple #11
0
        public GameObject()
        {
            Name = String.Empty;
            components.Add(new Transform(this, Vector3.Zero));

            RequireComponent comp = (RequireComponent)GetType().GetCustomAttribute(typeof(RequireComponent));

            if (comp != null)
            {
                // If component is not null OR if component doesn't exist in the components list
                if (comp.component != null ||
                    components.Find(component => component.GetType() == comp.component).GetType() !=
                    typeof(Component.Component))
                {
                    AddComponent(comp.component);
                    Debug.WriteLine("Added Component: " + comp.component.ToString() + " To \"" + this.Name + "\"");
                }

                else
                {
                    Debug.WriteLine("NOPE");
                }
            }
        }
        private static void RegisterNewObjects(List <UnityEngine.Object> newHierarchy, List <UnityEngine.Object> hierarchy, string actionName)
        {
            List <UnityEngine.Object> list = new List <UnityEngine.Object>();

            foreach (UnityEngine.Object current in newHierarchy)
            {
                bool flag = false;
                foreach (UnityEngine.Object current2 in hierarchy)
                {
                    if (current2.GetInstanceID() == current.GetInstanceID())
                    {
                        flag = true;
                        break;
                    }
                }
                if (!flag)
                {
                    list.Add(current);
                }
            }
            HashSet <Type> hashSet = new HashSet <Type>
            {
                typeof(Transform)
            };
            bool flag2 = false;

            while (list.Count > 0 && !flag2)
            {
                flag2 = true;
                for (int i = 0; i < list.Count; i++)
                {
                    UnityEngine.Object @object          = list[i];
                    object[]           customAttributes = @object.GetType().GetCustomAttributes(typeof(RequireComponent), true);
                    bool     flag3 = true;
                    object[] array = customAttributes;
                    for (int j = 0; j < array.Length; j++)
                    {
                        RequireComponent requireComponent = (RequireComponent)array[j];
                        if ((requireComponent.m_Type0 != null && !hashSet.Contains(requireComponent.m_Type0)) || (requireComponent.m_Type1 != null && !hashSet.Contains(requireComponent.m_Type1)) || (requireComponent.m_Type2 != null && !hashSet.Contains(requireComponent.m_Type2)))
                        {
                            flag3 = false;
                            break;
                        }
                    }
                    if (flag3)
                    {
                        if (@object is Transform)
                        {
                            @object = ((Transform)@object).gameObject;
                        }
                        Undo.RegisterCreatedObjectUndo(@object, actionName);
                        hashSet.Add(@object.GetType());
                        list.RemoveAt(i);
                        i--;
                        flag2 = false;
                    }
                }
            }
            foreach (UnityEngine.Object current3 in list)
            {
                Undo.RegisterCreatedObjectUndo(current3, actionName);
            }
        }
Exemple #13
0
        public static void SortComponents(UnityEditor.MenuCommand command)
        {
            var behaviour        = (Component)command.context;
            var behaviour_object = behaviour.gameObject;

            List <Component> components = new List <Component>();
            List <Component> sorted     = new List <Component>();

            Type[] types = new Type[] { typeof(Transform), typeof(MeshFilter), typeof(Renderer), typeof(Collider), typeof(Rigidbody), typeof(MonoBehaviour) };

            behaviour_object.GetComponents(components);
            sorted.AddRange(components);

            sorted.Sort((a, b) =>
            {
                var aType = a.GetType();
                var bType = b.GetType();

                //Sort by type first
                {
                    var length = types.Length;
                    var aIdx   = length;
                    var bIdx   = length;

                    for (int i = 0; i < length; ++i)
                    {
                        var type = types[i];
                        if (aIdx == length && (aType == type || aType.IsSubclassOf(type)))
                        {
                            aIdx = i;
                        }

                        if (bIdx == length && (bType == type || bType.IsSubclassOf(type)))
                        {
                            bIdx = i;
                        }

                        if (aIdx != length && bIdx != length)
                        {
                            break;
                        }
                    }

                    if ((aIdx != length || bIdx != length) && aIdx != bIdx)
                    {
                        return(aIdx - bIdx);
                    }
                }

                //Types with RequireComponent
                {
                    RequireComponent aRq = null;
                    {
                        var atts = aType.GetCustomAttributes(true);
                        foreach (var att in atts)
                        {
                            aRq = att as RequireComponent;
                            if (aRq != null)
                            {
                                break;
                            }
                        }
                    }

                    RequireComponent bRq = null;
                    {
                        var atts = bType.GetCustomAttributes(true);
                        foreach (var att in atts)
                        {
                            bRq = att as RequireComponent;
                            if (bRq != null)
                            {
                                break;
                            }
                        }
                    }

                    if (aRq == null && bRq != null)
                    {
                        return(-1);
                    }
                    else if (aRq != null && bRq == null)
                    {
                        return(1);
                    }
                }

                //Finish ordering by name compare
                return(String.Compare(aType.Name, bType.Name));
            });

            for (int iSort = 0; iSort < sorted.Count; ++iSort)
            {
                components.Clear();
                behaviour_object.GetComponents(components);

                var cmp  = sorted[iSort];
                int iCmp = components.FindIndex(x => x == cmp);

                int failtest = 20;
                var diff     = iSort - iCmp;
                var dir      = diff <= 0 ? 1 : -1;
                while (diff != 0)
                {
                    diff += dir;
                    if (dir > 0)
                    {
                        UnityEditorInternal.ComponentUtility.MoveComponentUp(cmp);
                    }
                    else
                    {
                        UnityEditorInternal.ComponentUtility.MoveComponentDown(cmp);
                    }

                    if (--failtest < 0)
                    {
                        break;
                    }
                }
            }
        }
    private void Reset()
    {
        GameObject gameObject = this.gameObject;

        // Check if this scene already contains an instance of the Singleton
        bool sceneHasInstance = gameObject.scene.GetRootGameObjects()
                                .Any(go => go.GetComponentInChildren <T>(true) &&
                                     (!go.GetComponentInChildren <T>(true).gameObject.Equals(gameObject) ||
                                      gameObject.GetComponents <T>().Length > 1)
                                     );

        // If the scene already contains an instance, remove THIS instance and all its added RequiredComponents
        if (sceneHasInstance)
        {
            DestroyImmediate(this);

            List <Type> requiredComponentsType = new List <Type>();
            FindRequiredComponentsOfType(typeof(T));
            requiredComponentsType.Reverse();

            Stack <Component> components = new Stack <Component>(gameObject.GetComponents <Component>());

            foreach (var type in requiredComponentsType)
            {
                if (components.Pop().GetType() == type)
                {
                    Undo.DestroyObjectImmediate(gameObject.GetComponent(type));
                    Undo.IncrementCurrentGroup();
                }
                else
                {
                    break;
                }
            }

            void FindRequiredComponentsOfType(Type type)
            {
                if (type == null)
                {
                    return;
                }

                MemberInfo memberInfo = type;

                RequireComponent[] requiredComponentsOfType =
                    Attribute.GetCustomAttributes(memberInfo, typeof(RequireComponent), true)
                    as RequireComponent[];

                for (int i = 0; i < requiredComponentsOfType.Length; i++)
                {
                    RequireComponent rc = requiredComponentsOfType[i];

                    HandleType(rc.m_Type0);
                    HandleType(rc.m_Type1);
                    HandleType(rc.m_Type2);
                }

                void HandleType(Type rcType)
                {
                    if (rcType == null || rcType == typeof(Transform))
                    {
                        return;
                    }

                    if (!requiredComponentsType.Contains(rcType))
                    {
                        FindRequiredComponentsOfType(rcType);
                        requiredComponentsType.Add(rcType);
                    }
                }
            }
        }
    }
        protected static IEnumerator RemoveDependencies(Component targetComponent)
        {
            if (targetComponent == null)
            {
                yield break;
            }

            Component[] siblingComponents = targetComponent.GetComponents <Component>();
            if (siblingComponents == null || siblingComponents.Length == 0)
            {
                yield break;
            }

            System.Type componentType = targetComponent.GetType();
            foreach (Component siblingComponent in siblingComponents)
            {
                if (siblingComponent == null)
                {
                    continue;
                }

                bool     deleteMe = false;
                object[] requireComponentAttributes = siblingComponent.GetType().GetCustomAttributes(typeof(RequireComponent), true);
                if (requireComponentAttributes.Length == 0)
                {
                    continue;
                }

                foreach (var requireComponentObject in requireComponentAttributes)
                {
                    RequireComponent requireComponentAttribute = requireComponentObject as RequireComponent;
                    if (requireComponentAttribute == null)
                    {
                        continue;
                    }

                    if (
                        requireComponentAttribute.m_Type0 != componentType &&
                        requireComponentAttribute.m_Type1 != componentType &&
                        requireComponentAttribute.m_Type2 != componentType
                        )
                    {
                        continue;
                    }

                    deleteMe = true;
                    break;
                }

                if (!deleteMe)
                {
                    continue;
                }

                #if VERBOSE_COMPONENT_REMOVAL
                Debug.LogWarningFormat("Deleting component dependency {0} found on {1}", siblingComponent.GetType().Name, targetComponent.gameObject.name);
                #endif

                yield return(RemoveComponent(siblingComponent));
            }
        }
Exemple #16
0
        private UnityObject AddComponent(Dictionary <int, UnityObject> idToObj, GameObject go, Dictionary <Type, bool> requirements, PersistentDescriptor componentDescriptor, Type componentType)
        {
            Component component;
            bool      isReqFulfilled             = requirements.ContainsKey(componentType) && requirements[componentType];
            bool      maybeComponentAlreadyAdded =
                !isReqFulfilled ||
                componentType.IsSubclassOf(typeof(Transform)) ||
                componentType == typeof(Transform) ||
                componentType.IsDefined(typeof(DisallowMultipleComponent), true) ||
                ComponentDependencies.ContainsKey(componentType) && ComponentDependencies[componentType].Any(d => go.GetComponent(d) != null);

            if (maybeComponentAlreadyAdded)
            {
                component = go.GetComponent(componentType);
                if (component == null)
                {
                    component = go.AddComponent(componentType);
                }
                if (!isReqFulfilled)
                {
                    requirements[componentType] = true;
                }
            }
            else
            {
                component = go.AddComponent(componentType);
                if (component == null)
                {
                    component = go.GetComponent(componentType);
                }
            }
            if (component == null)
            {
                Debug.LogErrorFormat("Unable to add or get component of type {0}", componentType);
            }
            else
            {
                object[] requireComponents = component.GetType().GetCustomAttributes(typeof(RequireComponent), true);
                for (int j = 0; j < requireComponents.Length; ++j)
                {
                    RequireComponent requireComponent = requireComponents[j] as RequireComponent;
                    if (requireComponent != null)
                    {
                        if (requireComponent.m_Type0 != null && !requirements.ContainsKey(requireComponent.m_Type0))
                        {
                            bool fulfilled = go.GetComponent(requireComponent.m_Type0);
                            requirements.Add(requireComponent.m_Type0, fulfilled);
                        }
                        if (requireComponent.m_Type1 != null && !requirements.ContainsKey(requireComponent.m_Type1))
                        {
                            bool fulfilled = go.GetComponent(requireComponent.m_Type1);
                            requirements.Add(requireComponent.m_Type1, fulfilled);
                        }
                        if (requireComponent.m_Type2 != null && !requirements.ContainsKey(requireComponent.m_Type2))
                        {
                            bool fulfilled = go.GetComponent(requireComponent.m_Type2);
                            requirements.Add(requireComponent.m_Type2, fulfilled);
                        }
                    }
                }
                idToObj.Add(unchecked ((int)componentDescriptor.PersistentID), component);
            }

            return(component);
        }
 static bool DoesntRequire(RequireComponent attribute, Type target)
 {
     return(attribute.m_Type0 != target &&
            attribute.m_Type1 != target &&
            attribute.m_Type2 != target);
 }