文档详情

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

发布:2017-07-09约8.07千字共7页下载文档
文本预览下载声明
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脚本有很多方法去查找和访问他们的游戏对象和组件.下面我们假设一个脚本OtherScript.js附于场景中的一个游戏对象上.C# JavaScript 【狗刨学习网】using UnityEngine;using System.Collections;public class example : MonoBehaviour { void Update() { otherScript = GetComponentOtherScript(); otherScript.DoSomething(); }}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 using UnityEngine;using System.Collections;public class example : MonoBehaviour { public Transform target; void Update() { target.Translate(0, 1, 0); }}// 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 using UnityEngine;using System.Collections;public class example : MonoBehaviour { public OtherScript target; void Update() { target.foo = 2; target.DoSomething(Hello); }}// 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 exis
显示全部
相似文档