示例#1
0
        /// <summary>
        /// Finds a game object with the specified name, returning any match in scene before
        /// trying to return any non-scene matches in the project. Checks GameObjects registered
        /// with the specifier as its actor name first.
        /// </summary>
        /// <returns>
        /// The specified game object.
        /// </returns>
        /// <param name='specifier'>
        /// The name to search for.
        /// </param>
        /// <param name='onlyActiveInScene'>Only search active objects in the scene.</param>
        public static GameObject FindSpecifier(string specifier, bool onlyActiveInScene = false)
        {
            if (string.IsNullOrEmpty(specifier))
            {
                return(null);
            }

            // Check for 'tag=' keyword:
            if (SpecifierSpecifiesTag(specifier))
            {
                var tag      = GetSpecifiedTag(specifier);
                var taggedGO = GameObject.FindGameObjectWithTag(tag);
                if (taggedGO != null)
                {
                    return(taggedGO);
                }
                var results = Tools.FindGameObjectsWithTagHard(tag);
                return((results.Length > 0) ? results[0] : null);
            }

            // Search registered actors:
            var t = CharacterInfo.GetRegisteredActorTransform(specifier);

            if (t != null)
            {
                return(t.gameObject);
            }

            // Search for active objects in scene:
            var match = GameObject.Find(specifier);

            if (match != null)
            {
                return(match);
            }

            if (onlyActiveInScene)
            {
                return(null);
            }

            // Search for all objects in scene, including inactive as long as it's a child of an active object:
            match = Tools.GameObjectHardFind(specifier);
            if (match != null)
            {
                return(match);
            }

            // Search for all objects, including loaded-but-not-instantiated (i.e., prefabs):
            foreach (GameObject go in Resources.FindObjectsOfTypeAll(typeof(GameObject)) as GameObject[])
            {
                if (string.Compare(specifier, go.name, System.StringComparison.OrdinalIgnoreCase) == 0)
                {
                    return(go);
                }
            }
            return(null);
        }