首页 unity3D教程之访问其他游戏物体

unity3D教程之访问其他游戏物体

举报
开通vip

unity3D教程之访问其他游戏物体Most advanced game code does not only manipulate a single object. The Unity scripting interface has various ways to find and access other game objects and components there-in. In the following we assume there is a script named OtherScript.js attached to game o...

unity3D教程之访问其他游戏物体
Most advanced game code does not only manipulate a single object. The Unity scripting interface has various ways to find and access other game objects and components there-in. In the following we assume there is a script named OtherScript.js attached to game objects in the scene. 多数高级的游戏代码并不仅仅控制单独的游戏对象. Unity脚本有很多 方法 快递客服问题件处理详细方法山木方法pdf计算方法pdf华与华方法下载八字理论方法下载 去查找和访问他们的游戏对象和组件.下面我们假设一个脚本OtherScript.js附于场景中的一个游戏对象上. ?C# ?JavaScript 【狗刨学习网】 function Update () { otherScript = GetComponent(OtherScript); otherScript.DoSomething(); } 1. Through inspector assignable references. 通过检视面板指定参数. You can assign variables to any object type through the inspector: 你能通过检视面板为一些对象类型设置变量值: ?C# ?JavaScript // Translate the object dragged on the target slot // 将要转换的对象拖拽到target位置 var target : Transform; function Update () { target.Translate(0, 1, 0); } You can also expose references to other objects to the inspector. Below you can drag a game object that contains the OtherScript on the target slot in the inspector. 你也可以把参数显示在检视面板.随后你可以拖拽游戏对象OtherScript到检视面板中的target位置. ?C# ?JavaScript // Set foo DoSomething on the target variable assigned in the inspector. // 设置foo DoSomething到target变量指定在检视面板. var target : OtherScript; function Update () { // Set foo variable of the target object // 设置target 对象的foo变量 target.foo = 2; // Call do something on the target // 调用target的Dosomething target.DoSomething("Hello"); } 2. Located through the object hierarchy. 确定对象的层次关系 You can find child and parent objects to an existing object through the Transform component of a game object: 你能通过游戏对象的Transform 组件去找到它的子对象或父对象: ?C# ?JavaScript // Find the child "Hand" of the game object //获得子游戏对象"Hand" // we attached the script to // 我们现在的脚本为 transform.Find("Hand").Translate(0, 1, 0); Once you have found the transform in the hierarchy, you can use GetComponent to get to other scripts. 一旦你在层次视图找到transform,你便能用GetComponent 获取其他脚本. ?C# ?JavaScript // Find the child named "Hand". // On the OtherScript attached to it, set foo to 2. // 找到子对象 "Hand". // 获取OtherScript,设置foo为2. transform.Find("Hand").GetComponent(OtherScript).foo = 2; // Find the child named "Hand". // Call DoSomething on the OtherScript attached to it. // 获得子对象"Hand". // 调用附属于它的 OtherScript的DoSomething. transform.Find("Hand").GetComponent(OtherScript).DoSomething("Hello") ; // Find the child named "Hand". // Then apply a force to the rigidbody attached to the hand. //获得子对象"Hand". // 加一个力到刚体上 transform.Find("Hand").rigidbody.AddForce(0, 10, 0); You can loop over all children: 你能循环到所有的子对象: ?C# ?JavaScript // Moves all transform children 10 units upwards! //向上移动所有的子对象1个单位! for (var child : Transform in transform) { child.Translate(0, 10, 0); } See the documentation for the Transform class for further information. 查看文档Transform 类可以获得更多信息. 3. Located by name or Tag. 指定名字或标签. You can search for game objects with certain tags using GameObject.FindWithTag and GameObject.FindGameObjectsWithTag . Use GameObject.Find to find a game object by name. 你能用确定的标签搜索对象,使用GameObject.FindWithTag 和 GameObject.FindGameObjectsWithTag .使用GameObject.Find 通过名字获得游戏对象. ?C# ?JavaScript 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); } You can use GetComponent on the result to get to any script or component on the found game object 你可以用GetComponent获得指定游戏对象上的任意脚本或组件. ?C# ?JavaScript function Start () { // By name 通过名字 var go = GameObject.Find("SomeGuy"); go.GetComponent(OtherScript).DoSomething(); // By tag 通过标签 var player = GameObject.FindWithTag("Player"); player.GetComponent(OtherScript).DoSomething(); } Some special objects like the main camera have shorts cuts using Camera.main . 一些特殊对象,比如主摄像机,用快捷方式Camera.main . 4. Passed as parameters. 传递参数 Some event messages contain detailed information on the event. For instance, trigger events pass the Collider component of the colliding object to the handler function. 一些事件包含详细的消息信息.例如,触发事件传递碰撞对象的Collider 组件到处理函数.
本文档为【unity3D教程之访问其他游戏物体】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_682974
暂无简介~
格式:doc
大小:23KB
软件:Word
页数:0
分类:互联网
上传时间:2019-09-02
浏览量:17