unity怎么unity 获取当前对象游戏对象的size

& & & &unity提供了多种获取某个对象的方法,
& & & &1.&GameObject.Find("name");
& & & &2.&GameObject.FindGameObjectWithTag("tag");
& & & &3. GameObject.FindWithTag("tag");
& & &&find方法都是全场景寻找,算是unity里最耗事的一种方法,通过tag寻找次之,unity内置了tag系统,应当说是较常见的方法之一,如果需要全场景寻找,FindWithTag会是不错的选择。
& & & 4.transform.GetComponent&T&,transform.GetComponentInChildren&T&,transform.GetComponents&T&
& & & &这是基于当前脚本,通过类型Class来获取某个类,
& & &5.transform.Find("name");
& & &也可通过名字获取子对象,
& & & &另外也可选择在实例化对象时保存引用,这是最快的方法。
阅读(...) 评论()Unity3d查找游戏对象的方法
时间: 21:33:46
&&&& 阅读:294
&&&& 评论:
&&&& 收藏:0
标签:获取游戏对象有三种方法:
1.通过对象名称获取:objCube=GameObject.Find("Cube");
private var objCube:GameOprivate var isCubeRoate=
function Start () {& objCube=GameObject.Find("Cube");}
function Update(){& if(isCubeRoate){&&&& objCube.transform.Rotate(0.0f,Time.deltaTime*200,0.0f);& }}
function OnGUI(){& if(GUILayout.Button("旋转",GUILayout.Height(50))){&&&& isCubeRoate=& }}
2.通过tag标签获取单个游戏对象:objCube=GameObject.FindWithTag("Finish");
3.通过游戏标签获取多组游戏对象:objCube=GameObject.FindGameObjectsWithTag("Finish");标签:
&&国之画&&&& &&
版权所有 京ICP备号-2
迷上了代码!5345人阅读
Unity(5)
&&&&&& 因为项目中难免要多次进行获取子对象或者子对象的集合,所以同事之前写了一个单独的类,用来做这些操作。然后再实际的项目中,只需要使用 transform 或者 gameobject& 调用这些方法就可以快速的得到这些数据,而并不需要自己在每个单独的类里面都写上一遍。
&&&&&& 不想偷懒的程序员不是好的程序员。代码如下:
using System.Collections.G
using System.L
using System.T
using UnityE
public static partial class ExtentionMethod
/// &summary&
/// 获取子对象变换集合
/// &/summary&
/// &param name=&obj&&&/param&
/// &returns&&/returns&
public static List&Transform& GetChildCollection(this Transform obj)
List&Transform& list = new List&Transform&();
for (int i = 0; i & obj.childC i++)
list.Add(obj.GetChild(i));
/// &summary&
/// 获取子对象集合
/// &/summary&
/// &param name=&obj&&&/param&
/// &returns&&/returns&
public static List&GameObject& GetChildCollection(this GameObject obj)
var list = obj.transform.GetChildCollection();
return list.ConvertAll(T =& T.gameObject);
public static Transform GetRootParent(this Transform obj)
Transform Root = obj.
while(Root.parent != null)
//Root = Root.
//transform.root,方法可以直接获取最上父节点。
Root = Root.
/// &summary&
/// 把源对象身上的所有组件,添加到目标对象身上
/// &/summary&
/// &param name=&origin&&源对象&/param&
/// &param name=&target&&目标对象&/param&
public static void CopyComponent(GameObject origin, GameObject target)
var originComs = origin.GetComponents&Component&();
foreach (var item in originComs)
target.AddComponent(item.GetType());
/// &summary&
/// 改变游戏脚本
/// &/summary&
/// &param name=&origin&&&/param&
/// &param name=&target&&&/param&
public static void ChangeScriptTo(this MonoBehaviour origin, MonoBehaviour target)
target.enabled =
origin.enabled =
/// &summary&
/// 从当前对象的子对象中查找,返回一个用tag做标识的活动的游戏物体的链表.如果没有找到则为空.
/// &/summary&
/// &param name=&obj&&对象Transform&/param&
/// &param name=&tag&&标签&/param&
/// &param name=&transList&&结果Transform集合&/param& // 对一个父对象进行递归遍历,如果有子对象的tag和给定tag相符合时,则把该子对象存到 链表数组中
public static void FindGameObjectsWithTagRecursive(this Transform obj, string tag, ref List&Transform& transList)
foreach (var item in obj.transform.GetChildCollection())
// 如果子对象还有子对象,则再对子对象的子对象进行递归遍历
if (item.childCount & 0)
item.FindGameObjectsWithTagRecursive(tag, ref transList);
if (item.tag == tag)
transList.Add(item);
public static void FindGameObjectsWithTagRecursive(this GameObject obj, string tag, ref List&GameObject& objList)
List&Transform& list = new List&Transform&();
obj.transform.FindGameObjectsWithTagRecursive(tag, ref list);
objList.AddRange(list.ConvertAll(T =& T.gameObject));
/// &summary&
/// 从父对象中查找组件
/// &/summary&
/// &typeparam name=&T&&组件类型&/typeparam&
/// &param name=&com&&物体组件&/param&
/// &param name=&parentLevel&&向上查找的级别,使用 1 表示与本对象最近的一个级别&/param&
/// &param name=&searchDepth&&查找深度&/param&
/// &returns&查找成功返回相应组件对象,否则返回null&/returns&
public static T GetComponentInParent&T&(this Component com, int parentLevel = 1, int searchDepth = int.MaxValue) where T : Component
searchDepth--;
if (com != null && searchDepth & 0)
var component = com.transform.parent.GetComponent&T&();
if (component != null)
parentLevel--;
if (parentLevel == 0)
return com.transform.parent.GetComponentInParent&T&(parentLevel, searchDepth);
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:16754次
排名:千里之外
(1)(4)(2)(1)匿名用户不能发表回复!|
每天回帖即可获得10分可用分!小技巧:
你还可以输入10000个字符
(Ctrl+Enter)
请遵守CSDN,不得违反国家法律法规。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。

我要回帖

更多关于 unity 获取当前时间戳 的文章

 

随机推荐