-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSingletonBehaviour.cs
46 lines (43 loc) · 1.11 KB
/
SingletonBehaviour.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
//单例模式的实现
public class SingletonBehaviour<T> : MonoBehaviour where T : Component
{
protected static T _instance;
public static T Instance
{
get
{
if (_instance == null)
{
//如果这个组件存在,找到它
_instance = FindObjectOfType<T>();
if (_instance == null)
{
GameObject obj = new GameObject();
_instance = obj.AddComponent<T>();
}
}
return _instance;
}
}
/// <summary>
/// Awake只调用一次,在脚本实例加载时执行
/// </summary>
public virtual void Awake()
{
DontDestroyOnLoad(gameObject);
name = "_" + typeof(T).Name;
//可能会有问题,会和跨场景有关?
//if (_instance == null)
//{
// _instance = this as T;
//}
//else
//{
// Destroy(gameObject);
//}
}
}