首頁 資料庫 mysql教程 Overview: Accessing Other Game Objects 访问其他游戏物体

Overview: Accessing Other Game Objects 访问其他游戏物体

Jun 07, 2016 pm 03:50 PM
game

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

1

2

3

4

<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

1

2

3

4

5

6

7

<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

1

2

3

4

5

6

7

8

9

10

<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

1

2

3

4

5

6

<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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<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

1

2

3

4

5

6

<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

1

2

3

4

5

6

7

8

9

10

<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

1

2

3

4

5

6

7

8

9

<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

1

2

3

4

5

6

7

8

9

<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

1

2

3

4

5

6

7

8

9

10

11

12

13

<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

1

2

3

4

5

6

7

8

9

<code>using UnityEngine;

using System.Collections;

 

public class example : MonoBehaviour {

    void Start() {

        OtherScript other = FindObjectOfType(typeof(OtherScript));

        other.DoSomething();

    }

}</code>

登入後複製
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱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

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1675
14
CakePHP 教程
1429
52
Laravel 教程
1333
25
PHP教程
1278
29
C# 教程
1257
24
Win11 Xbox Game Bar怎麼會徹底卸載?分享Xbox Game Bar卸載方法 Win11 Xbox Game Bar怎麼會徹底卸載?分享Xbox Game Bar卸載方法 Feb 10, 2024 am 09:21 AM

Win11XboxGameBar怎麼徹底卸載? XboxGameBar是系統中自帶的遊戲平台,它提供了用於遊戲錄製、截圖和社交功能的工具,不過很是佔用內存,也不好卸載,一些小伙伴想要將它卸載掉,但是不這道怎麼徹底卸載,下面就來為大家介紹一下。方法一、使用Windows終端機1、按【Win+X】組合鍵,或【右鍵】點選工作列上的【Windows開始功能表】,在開啟的的選單項目中,選擇【終端機管理員】。 2.用戶帳戶控制窗口,你要允許此應用程式對你的設備進行更改嗎?點選【是】。 3、執行以下指令:Get-AppxP

黑神話:悟空在發布後僅幾個小時就以 220 萬 Steam 玩家擊敗了競爭對手 黑神話:悟空在發布後僅幾個小時就以 220 萬 Steam 玩家擊敗了競爭對手 Aug 21, 2024 am 10:25 AM

隨著遊戲發售日期的臨近,《黑神話:悟空》的炒作已在全球範圍內感受到,它在 8 月 20 日推出時並沒有令人失望,受到了整個遊戲社區的熱烈歡迎。上線後

Nintendo Switch 的 Sonic X Shadow Generations 下載大小已透過官方清單公佈 Nintendo Switch 的 Sonic X Shadow Generations 下載大小已透過官方清單公佈 Jul 30, 2024 am 09:42 AM

以其前作《索尼克世代》(亞馬遜售價39 美元)的成功為基礎,世嘉將於2024 年10 月25 日發布《索尼克X 影子世代》。久,世嘉正在對原版遊戲進行擴充。

《沉睡的狗:最終版》PC 版在 GOG 上跌至歷史最低價 2.99 美元 《沉睡的狗:最終版》PC 版在 GOG 上跌至歷史最低價 2.99 美元 Aug 31, 2024 am 09:52 AM

目前,《Sleeping Dogs: Definitive Edition》在 GOG 的大幅折扣價僅為 2.99 美元,較原價 19.99 美元大幅折扣 85%。要利用此優惠,只需訪問 GOG 上的遊戲頁面,添加

Square Enix 射擊遊戲 Foamstars 在 2 月發售後玩家大出血,將免費開放 Square Enix 射擊遊戲 Foamstars 在 2 月發售後玩家大出血,將免費開放 Aug 28, 2024 pm 01:09 PM

Square Enix 的《Foamstars》一推出就獲得了非常熱烈的反響,據報道在推出當天就擊敗了熱門遊戲《地獄潛水員 2》——這可能是因為它是作為 PS Plus 月度遊戲計劃的一部分推出的。然而,玩家數量很快就下降了

微軟免費贈送復古建築遊戲,但僅限期限。 微軟免費贈送復古建築遊戲,但僅限期限。 Sep 07, 2024 pm 09:30 PM

Hero of the Kindgom II 並不是目前在 Microsoft Store 中免費提供的唯一一款 Lonely Troops 遊戲。在 9 月 9 日之前,玩家還可以免費獲得 Townpolis,這是一款 2008 年推出的建築遊戲,通常售價 5 美元。 Townpolis 投入播放器

Win11 Build 226×1.2271 預覽版更新,邀請所有 Windows Insider 頻道使用者體驗新版 Microsoft Store Win11 Build 226×1.2271 預覽版更新,邀請所有 Windows Insider 頻道使用者體驗新版 Microsoft Store Sep 17, 2023 am 09:29 AM

微軟公司今天針對Beta頻道發布Win11Build226×1.2271預覽版更新同時,邀請所有WindowsInsider頻道使用者體驗新版MicrosoftStore。 MicrosoftStore最新版本為22308.1401.x.x全新的GamePass頁面:微軟表示引進了全新的專用頁面,可以讓玩家探索並訂閱PCGamePass或GamePassUltimate。用戶可以了解GamePass諸多權益,包括獨佔遊戲、折扣、免費福利以及EAPlay等等。微軟希望用戶在不跳到

微軟贈送非常受歡迎的RPG冒險遊戲 微軟贈送非常受歡迎的RPG冒險遊戲 Sep 07, 2024 am 06:39 AM

《王國英雄II》是一款帶有RPG元素的冒險遊戲,玩家扮演一個簡單的農民,和他的妹妹住在一個安靜的村莊。但田園詩般的生活很快就被海盜襲擊所擾亂,於是他們出發去拯救國王

See all articles