我们可以对它进行重命名,选中物体后,在Inspector最上方的输入栏中即可重新输入物体的名字,输入后按回车即可生效,比如我这里将物体重命名为EmptyGameObject。 皮皮:“为什么这个EmptyGameObject在Game视图中看不见?” 我:“问得好,但我先不回答你这个问题,我们先来创建一个看得见的GameObject。” 6.2.2 创建几何体物体...
可以使用SceneManager.GetActiveScene方法获取当前活动的场景,然后使用SceneManager.GetRootGameObjects方法获取该场景中的所有根对象。这种方法适用于在多个场景之间进行对象传递或查找。 使用标签(Tag):可以为场景中的对象添加标签,然后使用GameObject.FindGameObjectsWithTag方法根据标签查找对象。这种方法适用于在场...
if (listOfObjects[i].tag == "Player") { // do something with this object } } 1. 2. 3. 4. 5. 优化方法是使用CompareTag 代替tag ,例如下面的例子 void Update() { int numTests = 10000000; if (Input.GetKeyDown(KeyCode.Alpha1)) { for(int i = 0; i < numTests; ++i) { if (...
Object[] os = GameObject.FindObjectsOfType(typeof(GameObj)); for (int i = 0; i < os.Length; i++) { Debug.Log(os[i].ToString()); } // 删除游戏对象 GameObject.Destroy(Cube,5); GameObject.DestroyObject(Cube, 5); // 不要在加载的时候删除后面的游戏对象 DontDestroyOnLoad(Cube); ...
{continue; }if(Application.isEditor) {stringsAssetPath =AssetDatabase.GetAssetPath(pObject.transform.root.gameObject);if(!string.IsNullOrEmpty(sAssetPath)) {continue; } } Debug.Log(pObject.name); }
GameObject.FindGameObjectsWithTag(“tag”) GameObject.FindWithTag(“tag”) 根据一个标记来获取游戏对象,返回一个 或者 一个数组,我个人觉得这个两个方法没啥用,因为既然需要用到标记那么相比这个游戏对象必然是非常特殊的一个,所以我会把它存在内存中。
public Component GetComponent (Type type); public T GetComponent (); public Component GetComponent (string type); public Component[] GetComponents (Type type); public T[] GetComponents (); 这些成员函数被声明在GameObject中,使用方法参考AddComponent。
GameObject.FindGameObjectsWithTag(“tag”) GameObject.FindWithTag(“tag”) 根据一个标记来获取游戏对象,返回一个 或者 一个数组,我个人觉得这个两个方法没啥用,因为既然需要用到标记那么相比这个游戏对象必然是非常特殊的一个,所以我会把它存在内存中。
transform.GetChild(0) 找到了一个即使隐藏root节点gameObject也能进行查找的方法。http://answers.unity3d.com/questions/52560/gameobjectfind-work-on-inactive-objects.html GameObject[] pAllObjects = (GameObject[])Resources.FindObjectsOfTypeAll(typeof(GameObject));foreach(GameObject pObjectinpAllObjects) ...
GetComponents GetComponentsInChildren GetComponentsInParent 注:可以在一个GamObject上调用,例如go.GetCom...也可以在一个脚本对象(组件对象)上直接调用transform.GetCom... 查找的范围都是这个GameObject上挂载的所有脚本(组件) 增加组件 gameObject.AddComponent<RotateSelf> (); ...