Object.Instantiate 違うものがひとつ混じっています。
パラメーター
original | コピーしたい既存オブジェクト |
position | Position for the new object. |
rotation | Orientation of the new object. |
parent | Parent that will be assigned to the new object. |
instantiateInWorldSpace | Pass true when assigning a parent Object to maintain the world position of the Object, instead of setting its position relative to the new parent. Pass false to set the Object's position relative to its new parent. |
戻り値
Object The instantiated clone.
説明
original のオブジェクトをクローンします
この関数は、エディタのDuplicateコマンドと同様にオブジェクトのコピーを作成します。GameObjectをクローンしている場合は、オプションでその位置と回転を指定することもできます(これらのデフォルトは元のGameObjectの位置と回転をデフォルトにします)。コンポーネントをクローンしている場合は、それがアタッチされているGameObjectも、オプションの位置と回転で再びクローンされます。GameObjectまたはComponent
をクローンすると、すべての子オブジェクトとコンポーネントも元のオブジェクトのプロパティと同様にプロパティが設定された状態で複製されます。デフォルトでは、親
新しいオブジェクトのnullはnullになるので、元の "兄弟"にはなりません。ただし、オーバーロードされたメソッドを使用して親を設定することはできます。親が指定されていて、位置と回転が指定されていない場合は、インスタンスの位置と回転が、クローンオブジェクトのローカル位置と回転、またはinstantiateInWorldSpaceパラメータがtrueの場合のワールド位置と回転に使用されます。位置と回転が指定されている場合、それらはワールド空間でのオブジェクトの位置と回転として使用されます。
クローン作成時にGameObjectのアクティブステータスが渡されるので、オリジナルがアクティブでない場合、クローンも非アクティブ状態で作成されます。
関連項目:詳細は「Prefab」を参照してください。
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour
{
public GameObject projectile;
public GameObject clone;
void Update()
{
for (int i = 0; i < Input.touchCount; ++i)
{
if (Input.GetTouch(i).phase == TouchPhase.Began)
clone = Instantiate(projectile, transform.position, transform.rotation) as GameObject;
}
}
}
using UnityEngine; using System.Collections;
public class ExampleClass : MonoBehaviour { public GameObject particle; void Update() { for (int i = 0; i < Input.touchCount; ++i) { if (Input.GetTouch(i).phase == TouchPhase.Began) { // Construct a ray from the current touch coordinates Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position); // Create a particle if hit if (Physics.Raycast(ray)) Instantiate(particle, transform.position, transform.rotation); } } } }
// Instantiates 10 copies of prefab each 2 units apart from each other
using UnityEngine; using System.Collections;
public class ExampleClass : MonoBehaviour { public Transform prefab; void Start() { for (int i = 0; i < 10; i++) { Instantiate(prefab, new Vector3(i * 2.0F, 0, 0), Quaternion.identity); } } }
using UnityEngine; using System.Collections;
public class ExampleClass : MonoBehaviour { public Rigidbody projectile; void Update() { if (Input.GetButtonDown("Fire1")) { Rigidbody clone; clone = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody; clone.velocity = transform.TransformDirection(Vector3.forward * 10); } } }
using UnityEngine; using System.Collections;
public class Missile : MonoBehaviour { public int timeoutDestructor;
// ...other code... }
public class ExampleClass : MonoBehaviour { // Instantiate a prefab with an attached Missile script public Missile projectile;
void Update() { // Ctrl was pressed, launch a projectile if (Input.GetButtonDown("Fire1")) { // Instantiate the projectile at the position and rotation of this transform Missile clone = (Missile)Instantiate(projectile, transform.position, transform.rotation);
// Set the missiles timeout destructor to 5 clone.timeoutDestructor = 5; } } }