zoukankan      html  css  js  c++  java
  • Unity获取游戏对象

    (1)代码中声明一个公开的游戏对象,在Inspectorzl中指定游戏对象。一般用于获得预制体或Scene场景中存在的对象。

    例:public GameObject obj;

    (2)GameObject. Find(String name)

              通过对象名称查找对象。

    例:obj=GameObject.Find("Sphere")

    (3)FindGameObjectsWithTag(string tag);

             通过标签查找具有相同标签的对象,返回一个GameObject类型的数组,没有找到返回空数组。

             注:在使用标签之前,必须在标签管理器中声明标签。 如果标签不存在或传递空字符串或null作为标签,则将引发UnityException。

    (4)FindWithTag(string tag);

              返回一个具有tag标签且处于启用状态的GameObject。 如果未找到GameObject,则返回null。

              注:此方法返回找到的带有指定标签的第一个GameObject。 如果场景包含带有指定标签的多个GameObject,则不能保证此方法将返回特定的GameObject

    (5)FindObjectOfType(Type type);

              返回与指定类型匹配的Object。 如果没有Object匹配类型,则返回null。

              注:此功能非常慢。 不建议每帧使用此功能。

               例:Camera cam = (Camera)FindObjectOfType(typeof(Camera));

    (6)Transform. Find(string name)

               返回父物体的一个子物体,不返回级别更低的物体。如果name包含'/'字符,它将像路径名一样在层次结构中访问Transform。

               例:若Cube物体有一个直接子物体Cube1,Cube1有一个直接子物体Cube2,我们就可以在Cube的脚本中使用transform.Find("Cube1/Cube2"),找到Cube2。

  • 相关阅读:
    [JSOI2007][BZOJ1031] 字符加密Cipher|后缀数组
    leetcode Flatten Binary Tree to Linked List
    leetcode Pascal's Triangle
    leetcode Triangle
    leetcode Valid Palindrome
    leetcode Word Ladder
    leetcode Longest Consecutive Sequence
    leetcode Sum Root to Leaf Numbers
    leetcode Clone Graph
    leetcode Evaluate Reverse Polish Notation
  • 原文地址:https://www.cnblogs.com/unitywyb/p/12752354.html
Copyright © 2011-2022 走看看