搜索
首页数据库mysql教程Overview: Accessing Other Game Objects 访问其他游戏物体

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.

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

<code>function Update () {
    otherScript = GetComponent(OtherScript);
    otherScript.DoSomething();
}</code>

1. Through inspector assignable references. 
通过检视面板指定参数.

You can assign variables to any object type through the inspector:

你能通过检视面板为一些对象类型设置变量值:

  • C#
  • JavaScript

<code><span>// Translate the object dragged on the target slot
// 将要转换的对象拖拽到target位置</span>

var target : Transform;
function Update () {
    target.Translate(0, 1, 0);
}</code>

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

<code>using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
    public OtherScript target;
    void Update() {
        target.foo = 2;
        target.DoSomething("Hello");
    }
}</code>

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

<code><span>// Find the child "Hand" of the game object
//获得子游戏对象"Hand" 
// we attached the script to
// 我们现在的脚本为</span>

transform.Find("Hand").Translate(0, 1, 0);</code>

Once you have found the transform in the hierarchy, you can use GetComponent to get to other scripts.

一旦你在层次视图找到transform,你便能用 GetComponent 获取其他脚本.

  • C#
  • JavaScript

<code><span>// Find the child named "Hand".
// On the OtherScript attached to it, set foo to 2.
// 找到子对象 "Hand".
// 获取OtherScript,设置foo为2</span>.
transform.Find("Hand").GetComponent(OtherScript).foo = 2;

<span>// Find the child named "Hand".
// Call DoSomething on the OtherScript attached to it.
// 获得子对象"Hand".
// 调用附属于它的 OtherScript的DoSomething.</span>
transform.Find("Hand").GetComponent(OtherScript).DoSomething("Hello");

<span>// Find the child named "Hand".
// Then apply a force to the rigidbody attached to the hand.
//获得子对象"Hand".
// 加一个力到刚体上</span>
transform.Find("Hand").rigidbody.AddForce(0, 10, 0);</code>

You can loop over all children: 你能循环到所有的子对象:

  • C#
  • JavaScript

<code><span>// Moves all transform children 10 units upwards!
//向上移动所有的子对象1个单位!</span>

for (var child : Transform in transform) {
    child.Translate(0, 10, 0);
}</code>

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

<code>function Start () {
    <span>// By name 通过名字</span>
    var go = GameObject.Find("SomeGuy");
    go.transform.Translate(0, 1, 0);

    
    var player = GameObject.FindWithTag("Player");
    player.transform.Translate(0, 1, 0);

}</code>

You can use GetComponent on the result to get to any script or component on the found game object

你可以用GetComponent获得指定游戏对象上的任意脚本或组件.

  • C#
  • JavaScript

<code>function Start () {
    <span>// By name 通过名字</span>
    var go = GameObject.Find("SomeGuy");
    go.GetComponent(OtherScript).DoSomething();

    
    var player = GameObject.FindWithTag("Player");
    player.GetComponent(OtherScript).DoSomething();
}</code>

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 组件到处理函数.

OnTriggerStay gives us a reference to a collider. From the collider we can get to its attached rigidbody.

OnTriggerStay给我们一个碰撞体参数.通过这个碰撞体我们能得到它的刚体.

  • C#
  • JavaScript

<code>function OnTriggerStay( other : Collider ) {
    <span>// If the other collider also has a rigidbody
    // apply a force to it!
    // 如果碰撞体有一个刚体
    // 给他一个力!</span>

    if (other.rigidbody)
    other.rigidbody.AddForce(0, 2, 0);
}</code>

Or we can get to any component attached to the same game object as the collider.

或者我们可以通过collider得到这个物体的任何组件.

  • C#
  • JavaScript

<code>function OnTriggerStay( other : Collider ) {
    <span>// 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.
    // 如果其他的碰撞体附加了OtherScript 
    // 调用他的DoSomething.
    // 一般碰撞体没有附脚本,
    // 所以我们需要首先检查是否为null.</span>

    if (other.GetComponent(OtherScript))
    other.GetComponent(OtherScript).DoSomething();
}</code>

Note that by suffixing the other variable in the above example, you can access any component inside the colliding object.

注意, 在上面的例子中使用后缀的方式访问其他变量.同样,你能访问到碰撞对象包含的任意组件。

5. All scripts of one Type 某个类型的脚本

Find any object of one class or script name using Object.FindObjectsOfType or find the first object of one type using Object.FindObjectOfType .

找到某个类型的对象或脚本可以用 Object.FindObjectsOfType 或获得某个类型的第一个对象使用 Object.FindObjectOfType .

  • C#
  • JavaScript

<code>using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
    void Start() {
        OtherScript other = FindObjectOfType(typeof(OtherScript));
        other.DoSomething();
    }
}</code>
声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
MySQL如何处理数据复制?MySQL如何处理数据复制?Apr 28, 2025 am 12:25 AM

MySQL通过异步、半同步和组复制三种模式处理数据复制。1)异步复制性能高但可能丢失数据。2)半同步复制提高数据安全性但增加延迟。3)组复制支持多主复制和故障转移,适用于高可用性需求。

您如何使用解释性语句分析查询性能?您如何使用解释性语句分析查询性能?Apr 28, 2025 am 12:24 AM

EXPLAIN语句可用于分析和提升SQL查询性能。1.执行EXPLAIN语句查看查询计划。2.分析输出结果,关注访问类型、索引使用情况和JOIN顺序。3.根据分析结果,创建或调整索引,优化JOIN操作,避免全表扫描,以提升查询效率。

您如何备份并还原MySQL数据库?您如何备份并还原MySQL数据库?Apr 28, 2025 am 12:23 AM

使用mysqldump进行逻辑备份和MySQLEnterpriseBackup进行热备份是备份MySQL数据库的有效方法。1.使用mysqldump备份数据库:mysqldump-uroot-pmydatabase>mydatabase_backup.sql。2.使用MySQLEnterpriseBackup进行热备份:mysqlbackup--user=root--password=password--backup-dir=/path/to/backupbackup。恢复时,使用相应的命

MySQL中慢速查询的常见原因是什么?MySQL中慢速查询的常见原因是什么?Apr 28, 2025 am 12:18 AM

MySQL慢查询的主要原因包括索引缺失或不当使用、查询复杂度、数据量过大和硬件资源不足。优化建议包括:1.创建合适的索引;2.优化查询语句;3.使用分表分区技术;4.适当升级硬件。

MySQL中有什么看法?MySQL中有什么看法?Apr 28, 2025 am 12:04 AM

MySQL视图是基于SQL查询结果的虚拟表,不存储数据。1)视图简化复杂查询,2)增强数据安全性,3)维护数据一致性。视图是数据库中的存储查询,可像表一样使用,但数据动态生成。

MySQL和其他SQL方言之间的语法有什么区别?MySQL和其他SQL方言之间的语法有什么区别?Apr 27, 2025 am 12:26 AM

mysqldiffersfromothersqldialectsinsyntaxforlimit,自动启动,弦乐范围,子征服和表面上分析。1)MySqluessLipslimit,whilesqlserverusestopopandoraclesrontersrontsrontsrontsronnum.2)

什么是mysql分区?什么是mysql分区?Apr 27, 2025 am 12:23 AM

MySQL分区能提升性能和简化维护。1)通过按特定标准(如日期范围)将大表分成小块,2)物理上将数据分成独立文件,3)查询时MySQL可专注于相关分区,4)查询优化器可跳过不相关分区,5)选择合适的分区策略并定期维护是关键。

您如何在MySQL中授予和撤销特权?您如何在MySQL中授予和撤销特权?Apr 27, 2025 am 12:21 AM

在MySQL中,如何授予和撤销权限?1.使用GRANT语句授予权限,如GRANTALLPRIVILEGESONdatabase_name.TO'username'@'host';2.使用REVOKE语句撤销权限,如REVOKEALLPRIVILEGESONdatabase_name.FROM'username'@'host',确保及时沟通权限变更。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器