๋ฐ์ ๋๊ธ๋ก ๋ฌ์ ์ค๋ผ ๊ทธ๋ฌ๋๋ฐ ๊ธ์ ์ ํ์ผ๋ก ๋ค ์ ์จ์ง๊ธธ๋...
์๋ ๋ง๋ค๊ธฐ๋ ์์ฝ๋กฌ ๋งน๊ทธ๋๋๋ฐ
public class SingleTonใTใ : MonoBehaviour where T : MonoBehaviour
{
protected static T _instance = null;
public static T instance
{
get
{
if (_instance == null) _instance = Object.FindObjectOfTypeใTใ();
if (_instance == null)
{
var object_ = new GameObject(typeof(T).Name + " Object");
_instance = object_.AddComponentใTใ();
}
return _instance;
}
}
}
๊ทธ๋ฅ ์๋ก์ฝ๋กฌ ์
public class SingleTonใTใ : MonoBehaviour where T : MonoBehaviour
{
protected static T _instance = null;
public static T instance
{
get
{
if (_instance == null) _instance = Object.FindObjectOfTypeใTใ();
return _instance;
}
}
}
์ฌ์ฉ.
public class GameManager : SingleTonใGameManagerใ
{
[SerializeField]
private Text _scoreText = null;
P,s. OnDestroy์์ _instance = null; ํด์ค์ผ ๋ค์ ๋ฒ ์ฌ ๋ก๋ ๋ ์ฐธ์กฐ ์๋ฌ๊ฐ ์๋์.
์ฑ๊ธํค์ ์์ํด์ ์ธ ๋งํผ ๋ง์ด ๋ง๋ค ํ์๊ฐ ์๋
๊ฐ๋ ์ฑ ๋ค์ฌ์ฐ๊ธฐ์ข
์ด๊ฑธ ์ง๊ฐ ์ง ๊ฑฐ ์ฒ๋ผ ์จ๋จ๋ค;
http://wiki.unity3d.com/index.php/Singleton
์ฝ๋ฉ ๋ฐฉ์ ๋๊ฐ๋ค