什么方法可以找到场景下所有的unity3d gameobjectt

更多公众号:Imgtec全球最领先的GPU架构 PowerVR;
世界上最高效的CPU架构MIPS;
全球多媒体通信技术的领先企业,致力于开发和授权用于图形、视频和显示、嵌入式处理、多标准通信和互连以及跨平台V.VoIP & VoLTE领域的顶尖处理器解决方案。最新文章相关推荐搜狗:感谢您阅读干货|Unity 5.3新功能:多场景编辑,本文可能来自网络,如果侵犯了您的相关权益,请联系管理员。QQ:22575人阅读
通讯,动态创建,Prefab,销毁
通过Hierarchy面板下的Create菜单可以手动地创建一个GameObject,它可以是一个相机,一个灯光,或者一个简单的模型,当我们要在程序里面动态地创建一个相机的时候,可以new一个GameObject,然后把Camera组件Add给它就可以了,创建灯光,GUI等也类似,但要创建简单模型的时候,我发现并没有类似Cube,Sphere这样的组件可供添加,后面知道,通过GameObject这个类的一个静态方法可以达到这个目的
static GameObject CreatePrimitive(PrimitiveType type);
可以根据PrimitiveType创建五种类型的基础模型
PrimitiveType.Plane,PrimitiveType.Cube,PrimitiveType.Sphere,PrimitiveType.Capsule,PrimitiveType.Cylinder
Unity3D提供一种被称为Prefab的预置对象,它是以文件的形式保存在硬盘上的一个GameObject,它里面可能包含了各种设置,组件,还有一些脚本。Prefab允许我们在不同的Scene,甚至Project中使用同一个对象,例如我实现了一个子弹,通过打包成Prefab,我可以在另外一个游戏里面直接使用它。
当一个Prefab对象被修改的时候,它的所有实例都会相应地被修改
在代码里面动态地实例化一个Prefab对象
public GameO
obj = Instantiate( obj&&);
要先创建一个公有变量,然后把这个Prefab拖到这个变量上,然后才可以用它来实例化对象(我感到一种蛋蛋的忧伤,能不拖吗)
通过Resources这个类的静态方法可以将Prefab对象动态加载进来,创建GameObject
GameObject之间的通讯,在游戏中,我们往往需要知道其他对象的一些信息,所以我们经常需要动态地查询另外一个GameObject
1. 查找父节点 transform.parent
2. 查找子节点 transform.Find(&name&);&&transform.Find(&Arm/Hand/Finger&); 没有找到会返回null,名字如果包含&/&字符将像路径一样穿越层次
3. 查找场景中的其他GameObject
GameObject.Find 从Scene下开始查找,根据GameObject的名字进行查找,允许使用&/&穿越层次查找,
GameObject.FindWithTag根据Tag查找一个GameObject,
GameObject.FindGameObjectsWithTag 根据Tag批量查找GameObject
GameObject的name和Tag可以直接设置,但Tag需要在标签管理器先定义一个Tag,这个Tag才可用,你不能将一个未定义的Tag赋给GameObject的Tag变量
最后还有一种搓搓的通讯方法,通过Message来调用其他GameObject,默认是垃圾短信群发,也可以将一个Object穿进去,然后调它的方法
methodName为方法名,value为方法的参数,options表示是否必须有对象接收该消息(如果是,且没有对象接受,u3d报错)
//调用本级别所有的GameObject上所有脚本的methodName方法
void SendMessage(string methodName, object value = null, SendMessageOptions options = SendMessageOptions.RequireReceiver);
//朝本级别和上级父节点发送调用命令
void SendMessageUpwards(...)
//目标是本级别和全部子节点...
void BroadcastMessage(...)
调用Object的Destroy方法可以销毁一个GameObject,组件,或者资源。这是一个静态方法,函数的原型为 static void Destroy(Object obj, float t = 0.0F );
可以通过将this传入,销毁自己。
GameObject的成员变量很多
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:946528次
积分:10194
积分:10194
排名:第1195名
原创:78篇
转载:350篇
评论:74条
(2)(3)(2)(1)(3)(1)(4)(2)(2)(6)(25)(2)(5)(3)(65)(20)(2)(4)(4)(58)(12)(10)(82)(75)(25)(1)(1)(1)(3)(2)(8)(1)unity(23)
1、通过标签来寻找&
void&ForeachObjs()
&&&&&&&&objs
= GameObject.FindGameObjectsWithTag(&Cube_00&);
&&&&&&&&foreach&(GameObject
obj&in&objs)
&&&&&&&&&&&&obj.AddComponent(&OnTiggerEnterSendMessage&);
2、通过Transform来寻找并附加脚本&
foreach&(Transform
child&in&gameObject.transform)
&&&&&&&&&&&child.gameObject.AddComponent(&OnTiggerEnterSendMessage&);
3、另外一种方式(转)&
GetTransform(Transform check,string&name)
&&foreach&(Transform
t&in&check.GetComponentsInChildren&Transform&())
&&&&if(t.name==name){return&t;}
&&&&GetTransform(t,name);
&&return&null;
4、(转)&
很多高级游戏代码不仅仅控制单一物体。Unity脚本接口有很多方法获得和访问其他游戏物体和组件。下面我们假设有一个名为OtherScript.js的脚本附属于场景中的游戏物体。&
var foo = 5;&
function DoSomething ( param : String) {&
print(param + & with foo: & + foo);&
1.使用检视视图指定参数。&
你能通过检视视图为一些游戏类型指定变量:&
// Translate the object dragged on the target slot&
var target : T&
function Update () {&
target.Translate(0, 1, 0);&
也能显示参数在其他物体检视视图。下面你能在Inspector面板中拖拽包含OtherScript的游戏物体到target上。&
// Set foo DoSomething on the target variable assigned in the inspector.&
var target : OtherS&
function Update () {&
// Set foo variable of the target object&
target.foo = 2;&
// Call do something on the target&
target.DoSomething(&Hello&);&
2.通过继承结构&
你能通过Transform组件获得一个子和父物体到现在的物体。&
// Find the child &Hand& of the game object&
// we attached the script to&
transform.Find(&Hand&).Translate(0, 1, 0);&
你只要在hierarchy面板建立了transform,你就能用GetComponent去获取other scripts。&
// Find the child named &Hand&.&
// On the OtherScript attached to it, set foo to 2.&
transform.Find(&Hand&).GetComponent(OtherScript).foo = 2;&
// Find the child named &Hand&.&
// Call DoSomething on the OtherScript attached to it.&
transform.Find(&Hand&).GetComponent(OtherScript).DoSomething(&Hello&);&
// Find the child named &Hand&.&
// Then apply a force to the rigidbody attached to the hand.&
transform.Find(&Hand&).rigidbody.AddForce(0, 10, 0);&
你可以循环到它所有的子物体。&
// Moves all transform children 10 units upwards!&
for (var child : Transform in transform) {&
child.Translate(0, 1, 0);&
查看文档Transform类 可以获得更多信息。&
3.通过名字或标签&
你能用确定的标签搜索物体,使用GameObject.FindWithTag 和GameObject.FindGameObjectsWithTag。使用GameObject.Find通过名字获得游戏物体。&
function Start ()&
// By name&
var go = GameObject.Find(&SomeGuy&);&
go.transform.Translate(0, 1, 0);&
// By tag&
var player = GameObject.FindWithTag(&Player&);&
player.transform.Translate(0, 1, 0);&
你能使用GetComponent获得脚本或组件在找到的游戏物体上。&
function Start ()&
// By name&
var go = GameObject.Find(&SomeGuy&);&
go.GetComponent(OtherScript).DoSomething();&
// By tag&
var player = GameObject.FindWithTag(&Player&);&
player.GetComponent(OtherScript).DoSomething();&
一些特殊对象如主摄像机有快捷方式,可以使用Camera.main。&
4.传递参数&
一些事件中包含详细的事件。例如:扳机事件传递(碰撞)Collider组件到碰撞物体的handler函数。&
OnTriggerStay给我们一个碰撞参数.从碰撞体我们能给他赋一个刚体。&
function OnTriggerStay( other : Collider ) {&
// If the other collider also has a rigidbody&
// apply a force to it!&
if (other.rigidbody) {&
other.rigidbody.AddForce(0, 2, 0);&
或者我们可以获取游戏中碰撞者包含的任意组件。&
function OnTriggerStay( other : Collider ) {&
// If the other collider has a OtherScript attached&
// call DoSomething on it.&
// Most of the time colliders won't have this script attached,&
// so we need to check first to avoid null reference exceptions.&
if (other.GetComponent(OtherScript)) {&
other.GetComponent(OtherScript).DoSomething();&
注意上面的例子,使用其他变量上的后缀,你能获取一些碰撞物体里的组件。&
5.某个类型的所有脚本&
从一个类找到任意对象或脚本,用Object.FindObjectsOfType或获得某个类型的第一个物体使用Object.FindObjectOfType.&
function Start ()&
// Find the OtherScript which is attached to any game object in the scene.&
var other : OtherScript = FindObjectOfType(OtherScript);&
other.DoSomething();&
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------&
5、自己写的深度优先遍历,找物体的孩子并做些什么事情&
using&UnityE
using&System.C
using&UnityE
public&class&ChangeMeshRenderQuality
&&&&[MenuItem(&GameObject/ChangeMeshRenderQuality&)]
&&&&static&void&FindChildMeshRender()
&&&&&&&&Debug.LogWarning(&Change
&&&&&&&&Transform[]
selection =& Selection.GetTransforms( SelectionMode.TopLevel | SelectionMode.Editable);
&&&&&&&&foreach(Transform
t&in&selection)
&&&&&&&&{&&&&&&&&&&
&&&&&&&&&&&&ChangeQuality(t);
&&&&static&void&ChangeQuality(Transform
&&&&&&&&for(int&i
= 0 ; i & T.childC i ++)
&&&&&&&&&&&&Transform
childTransform = T.GetChild(i);
&&&&&&&&&&&&if(childTransform.GetComponent&SkinnedMeshRenderer&()
&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&childTransform.GetComponent&SkinnedMeshRenderer&().quality
= SkinQuality.Bone4;
&&&&&&&&&&&&}
&&&&&&&&&&&&ChangeQuality(childTransform);
&&&&[MenuItem
(&GameObject/ChangeMeshRenderQuality&,&true)]
&&&&static&bool&ValidateSelection()
&&&&&&&&return&Selection.activeGameObject
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:18474次
排名:千里之外
原创:24篇
(3)(10)(2)(2)(1)(1)(6)(7)(2)Unity中的各种寻找GameObject方法 - 博客频道 - CSDN.NET
Cxihu的游戏开发备忘录
无她,惟手熟尔。等我神作发布;
分类:Find(&GameObject&)
1.GameObject.Find():寻找Hierarchy面板中的activie 不为false的游戏对象;
路径如官方事例写法:
public class ExampleClass : MonoBehaviour {
& & public GameO
& & void Example() {
& & & & hand = GameObject.Find(&Hand&);
& & & & hand = GameObject.Find(&/Hand&);
& & & & hand = GameObject.Find(&/Monster/Arm/Hand&);
& & & & hand = GameObject.Find(&Monster/Arm/Hand&);
2.Transform.Find()
官方解释是通过名字获取到一个子物体(Finds a child by&name&and
returns it),该方法可以获取到隐藏(inactive)的GameObject,
可以通过先获得父对象(active必须为true),再通过寻找孩子Transform.Find()
& & & &GameObject rootObj = GameObject.Find(&GameObject&); & & &&
& & & & &GameObject transformObj= &rootObj.transform.Find(&xxxx&).gameO
transformObj.SetActive(true)
3.transform.GetChild(index);index为索引,0,1,2,3,4代表第几个child.
补充:关于场景中的消失
排名:千里之外
(1)(1)(1)(2)(0)(1)(4)(1)(0)(1)(2)(12)(1)(1)(4)(1)(1)(2)(1)(1)(1)(1)(0)(1)(2)

我要回帖

更多关于 unity gameobject数组 的文章

 

随机推荐