示例#1
0
 /// <summary>
 /// Helps you to initialize references to components or GameObject easily.
 /// Call Component.InitComponentRefs() method inside a component in order to initializes references to components or GameObjects
 /// for any property of that component that use this attribute.
 /// </summary>
 /// <param name="_ReferenceObjectName">The name of the object from which you want to get the reference.</param>
 /// <param name="_WorldOnly">If enabled, the reference will be get from the world, so not from this GameObject or one of its
 /// child.</param>
 public ComponentRefAttribute(string _ReferenceObjectName, bool _WorldOnly)
 {
     m_Scope = _WorldOnly
         ? EComponentRefScope.World
         : EComponentRefScope.Local | EComponentRefScope.Children | EComponentRefScope.World;
     m_RefObjectName = _ReferenceObjectName;
 }
示例#2
0
 /// <summary>
 /// Helps you to initialize references to components or GameObject easily.
 /// Call Component.InitComponentRefs() method inside a component in order to initializes references to components or GameObjects
 /// for any property of that component that use this attribute.
 /// </summary>
 /// <param name="_AllowChildren">If enabled, the reference will be searched on the GameObject and in its hierarchy.</param>
 /// <param name="_AllowScene">If enabled, the reference will be searched in the scene (using FindObjectOfType()).</param>
 public ComponentRefAttribute(bool _AllowChildren = false, bool _AllowScene = false)
 {
     m_Scope = EComponentRefScope.Local;
     if (_AllowChildren)
     {
         m_Scope |= EComponentRefScope.Children;
     }
     if (_AllowScene)
     {
         m_Scope |= EComponentRefScope.World;
     }
 }
        /// <summary>
        /// Find a component reference depending on the given settings.
        /// </summary>
        /// <param name="_Component">The component from which you want to get a reference.</param>
        /// <param name="_PropertyType">The type of the component you want to get. THis can also be GameObject.</param>
        /// <param name="_Scope">The scope of the research to use for getting the reference.</param>
        /// <param name="_RefObjectName">The name of the object on which you want to get the reference.</param>
        /// <returns>Returns the found reference, or null if it failed.</returns>
        public static Object FindComponentRef(Component _Component, Type _PropertyType, EComponentRefScope _Scope = EComponentRefScope.Local, string _RefObjectName = null)
        {
            bool isGameObjectRef = _PropertyType == typeof(GameObject);

            if (!_PropertyType.IsSubclassOf(typeof(Component)) && !isGameObjectRef)
            {
                return(null);
            }

            // Get local references (using GetComponent())
            if (_Scope.HasFlag(EComponentRefScope.Local))
            {
                if (isGameObjectRef)
                {
                    if (string.IsNullOrEmpty(_RefObjectName) || _Component.gameObject.name == _RefObjectName)
                    {
                        return(_Component.gameObject);
                    }
                }
                else
                {
                    if (string.IsNullOrEmpty(_RefObjectName) || _Component.gameObject.name == _RefObjectName)
                    {
                        return(_Component.GetComponent(_PropertyType));
                    }
                }
            }

            // Get children references (using GetComponentsInHierarchy())
            if (_Scope.HasFlag(EComponentRefScope.Children))
            {
                // If we search for a GameObject
                if (isGameObjectRef)
                {
                    // If no target name has been mentionned, find the first GameObject in the hierarchy
                    if (string.IsNullOrEmpty(_RefObjectName))
                    {
                        foreach (Transform child in _Component.transform)
                        {
                            if (child != _Component.transform)
                            {
                                return(child.gameObject);
                            }
                        }
                    }
                    // Else, finds the GameObject in the hierarchy that has the given name
                    else
                    {
                        return(_Component.gameObject.Find(_RefObjectName, false, true));
                    }
                }
                // Else, if we search for a component
                else
                {
                    // If no object name has been mentionned, find the first component in the hierarchy with the given type
                    if (string.IsNullOrEmpty(_RefObjectName))
                    {
                        return(_Component.GetComponentInHierarchy(_PropertyType, false, true));
                    }

                    // Else, try to get the component from the object of the given name in the hierarchy
                    GameObject target = _Component.gameObject.Find(_RefObjectName, false, true);
                    if (target != null && target.TryGetComponent(_PropertyType, out Component comp))
                    {
                        return(comp);
                    }
                }
            }

            // Get World references (using FindObjectOfType())
            if (_Scope.HasFlag(EComponentRefScope.World))
            {
                if (isGameObjectRef)
                {
                    return(string.IsNullOrEmpty(_RefObjectName)
                        ? Object.FindObjectOfType <GameObject>()
                        : GameObject.Find(_RefObjectName));
                }
                else
                {
                    Object target = Object.FindObjectOfType(_PropertyType);

                    if (string.IsNullOrEmpty(_RefObjectName) || target.name == _RefObjectName)
                    {
                        return(target);
                    }

                    Object[] objs = Object.FindObjectsOfType(_PropertyType);
                    foreach (Object obj in objs)
                    {
                        if (obj.name == _RefObjectName)
                        {
                            return(obj);
                        }
                    }
                }
            }

            return(null);
        }
示例#4
0
 /// <summary>
 /// Helps you to initialize references to components or GameObject easily.
 /// Call Component.InitComponentRefs() method inside a component in order to initializes references to components or GameObjects
 /// for any property of that component that use this attribute.
 /// </summary>
 /// <param name="_Scope">Defines if you want to get references from this object (Local), its children (Children), in the scene
 /// (World), or several scopes using flags (for example EComponentRefScope.Local | EComponentRefScope.Children).</param>
 public ComponentRefAttribute(EComponentRefScope _Scope)
 {
     m_Scope = _Scope;
 }
示例#5
0
 /// <summary>
 /// Helps you to initialize references to components or GameObject easily.
 /// Call Component.InitComponentRefs() method inside a component in order to initializes references to components or GameObjects
 /// for any property of that component that use this attribute.
 /// </summary>
 /// <param name="_ChildName">The name of the child object from which you want to get the reference.</param>
 public ComponentRefAttribute(string _ChildName)
 {
     m_Scope         = EComponentRefScope.Local | EComponentRefScope.Children;
     m_RefObjectName = _ChildName;
 }
示例#6
0
 /// <summary>
 /// Helps you to initialize references to components or GameObject easily.
 /// Call Component.InitComponentRefs() method inside a component in order to initializes references to components or GameObjects
 /// for any property of that component that use this attribute.
 /// </summary>
 /// <param name="_ReferenceObjectName">The name of the object from which you want to get the reference.</param>
 /// <param name="_Scope">Defines if you want to get references from this object (Local), its children (Children), in the scene
 /// (World), or several scopes using flags (for example EComponentRefScope.Local | EComponentRefScope.Children).</param>
 public ComponentRefAttribute(string _ReferenceObjectName, EComponentRefScope _Scope)
 {
     m_RefObjectName = _ReferenceObjectName;
     m_Scope         = _Scope;
 }