Skip to content

Instantly share code, notes, and snippets.

@todorok1
Last active April 17, 2018 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save todorok1/357c53ba6c9f877848ccda6377ea4297 to your computer and use it in GitHub Desktop.
Save todorok1/357c53ba6c9f877848ccda6377ea4297 to your computer and use it in GitHub Desktop.
Unityチュートリアル・ガイド用のPrefabをインスタンス化するスクリプト。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GuideManager : MonoBehaviour {
// 『Sphere』オブジェクトへの参照
[SerializeField]
GameObject player;
// 『Sphere』オブジェクトから見た相対的なガイドの位置
Vector3 relativePos = new Vector3(2.0f, 2.0f, 0f);
void Start(){
// Prefabをインスタンス化するメソッドを呼ぶ
InstantiateGuidePrefabs();
}
void Update(){
}
void InstantiateGuidePrefabs(){
// 『GuideParent』の位置を『Sphere』オブジェクトの位置へ移動
gameObject.transform.position = player.transform.position;
//『GuideParent』の位置にrelativePosを足してPrefabの位置を決定
Vector3 guidePos = gameObject.transform.position + relativePos;
// Prefabをロードする
GameObject guidePrefab = (GameObject)Resources.Load("Prefabs/Guide");
// Prefabをインスタンス化
GameObject guideObject = (GameObject)Instantiate(guidePrefab, guidePos, Quaternion.identity);
// インスタンス化したオブジェクトをGuideParentの子オブジェクトにする
guideObject.transform.SetParent(gameObject.transform);
// オブジェクト名を設定する
guideObject.name = "GuideFromScript";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment