在Unity中,获得包含某种component的gameobject
2015-11-12 15:20
459 查看
游戏开发的过程中可能有时想要去找所有包含某种component的gameobject。Unity似乎没有提供这样的接口。但是通过下面几个步骤是可以实现这种操作的,这里介绍在场景中所有包含Animation这种component的游戏对象。关键接口是GameObject.FindObjectsOfType<T>(); 1.首先找到所有的这些组建 2.通过这些组建的gameObject返回包含这些组建的游戏对象。具体代码如下:
public static GameObject [] FindAllHeroModels() { //1.find the hero's animation component object [] heroAnimations = GameObject.FindObjectsOfType<Animation>(); //assigned the array of hero's model GameObject[] HeroModels = new GameObject[heroAnimations.Length]; //test for (int i = 0; i < heroAnimations.Length; i++) { if (heroAnimations[i] is Animation)//check the type { HeroModels[i] = (heroAnimations[i] as Animation).gameObject; } else { Debug.Log("Can't find the game object FUNC:FindAllHeroModels POS:AnimationController.cs"); return null; } } return HeroModels; }
通过上面的代码,我们可以找到所有包含Animation的游戏对象,并且分配到HeroModels这个数组中,接下来你就可以通过这个数组然后,对模型进行各种操作了,比如说播放动画。
相关文章推荐
- 亲测成功 unity直连android真机测试
- Unity给力插件之MeshBaker
- 记一次基于Unity的Profiler性能分析
- Unity错误(3)—— java.lang.IllegalArgumentException: ResClass is not initialized
- Unity错误(1)—— 安装应用后,空白界面,不显示设置的按钮、label等
- Unity 错误(1)——The name `xxx' does not exist in the current context
- unity 消息分发
- Unity3d优化
- 【Unity实用小方法】随机生成不重复的数字
- 【笨木头Unity】入门之旅007:Demo之四处找死(二)_主角移动和旋转
- 蒙皮的网格渲染器
- PrefabType 预置类型
- cardboard sdk for unity 下载地址
- Unity3D-单例模式
- Unity+NGUI性能优化方法总结
- 增强现实(AR)笔记2:Vuforia+Unity例程ObjectRecognition
- Unity3D-动态加载资源
- 在Unity中生成二维码
- 转:Oculus Unity Development Guide开发指南(2015-7-21更新)
- Unity3D-Terrain地形编辑