Last active
August 29, 2015 13:55
-
-
Save tsubaki/8703610 to your computer and use it in GitHub Desktop.
オブジェクト単位でのシングルトン。名前で判断
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
/// <summary> | |
/// このコンポーネントがアタッチされているオブジェクトは、 単一のオブジェクトに含まれる | |
/// 既にある場は削除される | |
/// </summary> | |
public class SingletonObject : MonoBehaviour | |
{ | |
/// <summary> | |
/// 親オブジェクトの情報 | |
/// </summary> | |
private readonly static string objectName = "gameController"; | |
/// <summary> | |
/// The base object. | |
/// </summary> | |
private static GameObject baseObject; | |
private static GameObject BaseObject { | |
get { | |
if (baseObject == null) { | |
baseObject = GameObject.Find (objectName); | |
if (baseObject == null) { | |
baseObject = new GameObject (objectName); | |
} | |
} | |
return baseObject; | |
} | |
} | |
/// <summary> | |
/// 登録済みオブジェクトの取得 | |
/// </summary> | |
/// <returns> 名称に一致するオブジェクト </returns> | |
/// <param name='objName'> | |
/// オブジェクト名 | |
/// </param> | |
public static GameObject GetGameObject (string objName) | |
{ | |
return BaseObject.transform.FindChild (objName).gameObject; | |
} | |
/// <summary> | |
/// 登録済みオブジェクトの取得 | |
/// </summary> | |
/// <returns>最初に一致したオブジェクト</returns> | |
/// <param name="type">探索する型情報</param> | |
public static GameObject GetGameObject (System.Type type) | |
{ | |
return BaseObject.transform.GetComponentInChildren (type).gameObject; | |
} | |
/// <summary> | |
/// 既に同名のオブジェクトがある場合、自身を削除する | |
/// </summary> | |
bool Marge () | |
{ | |
Transform target = BaseObject.transform.FindChild (name); | |
if (target != null && target != transform) { | |
Destroy (gameObject); | |
return true; | |
} else { | |
transform.parent = BaseObject.transform; | |
return false; | |
} | |
} | |
#region UnityDelegate | |
void Awake () | |
{ | |
Marge (); | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
このコンポーネントを付与したオブジェクトは全て** gameController**オブジェクトに入る。
既に同名のオブジェクトが会った場合、自身を破棄する(Awakeで)
gameControllerオブジェクト以下のオブジェクトを取得する場合、以下のコードで取得する。