http://docs-jp.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.htmlのunity日本語翻訳
前:Accessing Components
次:Vectors
最も高度なゲーム・コードは単に単一のオブジェクトを操作しません。
ユニティのスクリプトを書くインターフェースにはそこにイン他のゲーム・オブジェクトおよびコンポーネントを見つけてアクセスする様々な方法があります。
の中で、次、私たちは、ゲーム・オブジェクトに現場に付けられたOtherScript.jsという名のスクリプトがあると考えます。
JavaScript
---------------------
function Update () {
var otherScript: OtherScript = GetComponent(OtherScript);
otherScript.DoSomething();
}
---------------------
C#
---------------------
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
void Update() {
OtherScript otherScript = GetComponent
otherScript.DoSomething();
}
}
---------------------
Boo
---------------------
import UnityEngine
import System.Collections
class example(MonoBehaviour):
def Update():
otherScript as OtherScript = GetComponent[of OtherScript]()
otherScript.DoSomething()
---------------------
1. 検査官割り当て可能な参照を通じて。
検査官によって任意のオブジェクト・タイプに変数を帰することができます:
JavaScript
------------------------
// Translate the object dragged on the target slot
var target : Transform;
function Update () {
target.Translate(0, 1, 0);
}
------------------------
C#
------------------------
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public Transform target;
void Update() {
target.Translate(0, 1, 0);
}
}
------------------------
Boo
------------------------
import UnityEngine
import System.Collections
class example(MonoBehaviour):
public target as Transform
def Update():
target.Translate(0, 1, 0)
-------------------------
2. オブジェクト階層によって位置しました。
ゲーム・オブジェクトのTransformコンポーネントによって既存のオブジェクトへの子どもおよび親オブジェクトを見つけることができます:
JavaScript
------------------
// Find the child "Hand" of the game object
// we attached the script to
transform.Find("Hand").Translate(0, 1, 0);
------------------
C#
------------------
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
void Example() {
transform.Find("Hand").Translate(0, 1, 0);
}
}
-------------------
Boo
--------------------
import UnityEngine
import System.Collections
class example(MonoBehaviour):
def Example():
transform.Find('Hand').Translate(0, 1, 0)
---------------------
一旦階層にtransformを見つけたならば、他のスクリプトに到着するためにGetComponentを使用することができます。
JavaScript
------------------------------
// 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);
-------------------------------
C#
-------------------------
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
void Example() {
transform.Find("Hand").GetComponent
transform.Find("Hand").GetComponent
transform.Find("Hand").rigidbody.AddForce(0, 10, 0);
}
}
---------------------------
Boo
-------------------------
import UnityEngine
import System.Collections
class example(MonoBehaviour):
def Example():
transform.Find('Hand').GetComponent[of OtherScript]().foo = 2
transform.Find('Hand').GetComponent[of OtherScript]().DoSomething('Hello')
transform.Find('Hand').rigidbody.AddForce(0, 10, 0)
-----------------------------
子どもをの上にすべてループにすることができます:
JavaScript
------------------------------
// Moves all transform children 10 units upwards!
for (var child : Transform in transform) {
child.Translate(0, 10, 0);
}
------------------------
C#
------------------------
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
void Example() {
foreach (Transform child in transform) {
child.Translate(0, 10, 0);
}
}
}
------------------------------
Boo
----------------------
import UnityEngine
import System.Collections
class example(MonoBehaviour):
def Example():
for child as Transform in transform:
child.Translate(0, 10, 0)
-----------------------
さらに詳しい情報のためのTransformクラスについては、ドキュメンテーションを参照してください。
3. 名前またはタグによって位置しました。
GameObject.FindWithTagとGameObject.FindGameObjectsWithTagを使用して、あるタグを備えたゲーム・オブジェクトを探索することができます。
名前によってゲーム・オブジェクトを見つけるためにGameObject.Findを使用してください。
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);
}
-------------------------
C#
-------------------------
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
void Start() {
GameObject go = GameObject.Find("SomeGuy");
go.transform.Translate(0, 1, 0);
GameObject player = GameObject.FindWithTag("Player");
player.transform.Translate(0, 1, 0);
}
}
--------------------------
Boo
-----------------------
import UnityEngine
import System.Collections
class example(MonoBehaviour):
def Start():
go as GameObject = GameObject.Find('SomeGuy')
go.transform.Translate(0, 1, 0)
player as GameObject = GameObject.FindWithTag('Player')
player.transform.Translate(0, 1, 0)
----------------------------------
主なカメラのようないくつかの特別のオブジェクトはCamera.mainを使用して、ズボン下カットをしています。
4. パラメーターとしてパスされました。
いくつかの出来事メッセージは、出来事に関する詳細情報を含んでいます。
例えば、トリガーの出来事は、衝突するオブジェクトのコライダー・コンポーネントを取扱い人機能へ渡します。
OnTriggerStayは、私たちにコライダーへの言及を与えます。
コライダーから、私たちはその付属の剛体に到着することができます。
JavaScript
-------------------------
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);
}
--------------------------
C#
---------------------------
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
void OnTriggerStay(Collider other) {
if (other.rigidbody)
other.rigidbody.AddForce(0, 2, 0);
}
}
----------------------------
Boo
------------------------------
import UnityEngine
import System.Collections
class example(MonoBehaviour):
def OnTriggerStay(other as Collider):
if other.rigidbody:
other.rigidbody.AddForce(0, 2, 0)
-----------------------------
あるいは、私たちは、コライダーと同じゲーム・オブジェクトに付けられた任意のコンポーネントに到着することができます。
JavaScript
-----------------------------
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();
}
------------------------------
C#
-----------------------------
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
void OnTriggerStay(Collider other) {
if (other.GetComponent
other.GetComponent
}
}
-----------------------------
Boo
-----------------------------
import UnityEngine
import System.Collections
class example(MonoBehaviour):
def OnTriggerStay(other as Collider):
if other.GetComponent[of OtherScript]():
other.GetComponent[of OtherScript]().DoSomething()
------------------------------
上記の例における別の変数を後に付けることによって、衝突するオブジェクトの内部のどんなコンポーネントにもアクセスすることができることに注意してください。
5. 1つのタイプのすべてのスクリプト
Object.FindObjectsOfTypeを使用する1つのクラスあるいはスクリプト名の任意のオブジェクトを見つけるか、あるいはObject.FindObjectOfTypeを使用して、1つのタイプの最初のオブジェクトを見つけてください。
JavaScript
---------------------
function Start () {
// Find the OtherScript which is attached to any game object in the scene.
var other : OtherScript = FindObjectOfType(OtherScript);
other.DoSomething();
}
---------------------
C#
-------------------------
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
void Start() {
OtherScript other = FindObjectOfType(typeof(OtherScript));
other.DoSomething();
}
}
-----------------------
Boo
---------------------
import UnityEngine
import System.Collections
class example(MonoBehaviour):
def Start():
other as OtherScript = FindObjectOfType(OtherScript)
other.DoSomething()
------------------------
前:Accessing Components
次:Vectors
