多语言展示
当前在线:1878今日阅读:19今日分享:20

unity3d代码广度查找物体

unity3d自带的查找子物体方法过于消耗资源,对于模型换装武器或者是对NGUI的添加或者删除Panel不利,本人也两次遇到此类问题,在此贴上代码,大家可以在此基础上进行优化和修改
工具/原料

unity3d

方法/步骤
1

public class LookGameObjFunc{ public static GameObject FindGameObjByPath(string name, GameObject obj) { string[] strNamePath = name.Split(new char[]{'/'},System.StringSplitOptions.RemoveEmptyEntries); if(strNamePath.Length < 0) return null; GameObject tranName = obj; for(int index = 0; index < strNamePath.Length; ++index) {    FindGameObjbyName(strNamePath[index],ref tranName); }  return tranName; } private static void FindGameObjbyName(string name, ref GameObject  tranf) { foreach(Transform child in tranf.transform) {     Debug.Log('childName:' + child.name);     if(child.name.Equals(name)) {     tranf = child.gameObject;     return; } }}}

2

在unity3dStart函数中调用:GameObject Ui = GameObject.Find('ninjia'); string strPathName = 'Bip01/Bip01 Pelvis//Bip01 Spine/Bip01 Spine1/Bip01 Neck/Bip01 R Clavicle/Bip01 R UpperArm/Bip01 R Forearm/wuqi_bone1'; GameObject tmp = LookGameObjFunc.FindGameObjByPath(strPathName,Ui); Debug.Log(tmp.name);

3

最后的结果:

4

大家可以在此基础上根据自己的情况进行修改

注意事项

代码适用于C#

推荐信息