unity添加对象实例_Unity里的单例对象创建方法 | 六阿哥博客
Unity里的单例对象不同场景有不同的写法,下面是最常见的几种。
普通类
不是继承MonoBehaviour类的普通类,不能被挂载到Unity游戏物体上。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test
{
private static Test _Instance;
public static Test Instance
{
get
{
if (_Instance == null)
{
_Instance = new Test();
}
return _Instance;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
usingSystem.Collections;
usingSystem.Collections.Generic;
usingUnityEngine;
publicclassTest
{
privatestaticTest_Instance;
publicstaticTestInstance
{
get
{
if(_Instance==null)
{
_Instance=newTest();
}
return_Instance;
}
}
}
多游戏物体
我们都知道,继承自MonoBehaviour并挂载到游戏物体上的脚本,是由Unity去创建对象的,挂载了多少次就会创建多少个这个脚本的实例对象。
根据脚本的生命周期,如果一个脚本被挂载到多个游戏物体上,Awake()方法会被执行多次,我们要写单例就不能在这个方法中直接赋值了。我们可以自己创建一个空游戏物体来实现,不过这种方式并不完全是脚本的单例对象,只是保证_Instance在内存中只存在一份而已。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
private static Test _Instance;
public static Test Instance
{
get
{
if (_Instance == null)
{
// 在场景中创建一个名字为Test的空游戏物体
GameObject obj = new GameObject("Test");
// 给这个空游戏物体添加一个Test组件
_Instance = obj.AddComponent();
// 加载新场景时不会销毁这个空物体
DontDestroyOnLoad(obj);
}
return _Instance;
}
}
}
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
usingSystem.Collections;
usingSystem.Collections.Generic;
usingUnityEngine;
publicclassTest:MonoBehaviour
{
privatestaticTest_Instance;
publicstaticTestInstance
{
get
{
if(_Instance==null)
{
// 在场景中创建一个名字为Test的空游戏物体
GameObjectobj=newGameObject("Test");
// 给这个空游戏物体添加一个Test组件
_Instance=obj.AddComponent();
// 加载新场景时不会销毁这个空物体
DontDestroyOnLoad(obj);
}
return_Instance;
}
}
}
单游戏物体
如果确定一个脚本只会被挂载到一个游戏物体上,可以直接在Awake()方法中赋值:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
private static Test _Instance;
public static Test Instance
{
get
{
return _Instance;
}
}
void Awake()
{
_Instance = this;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
usingSystem.Collections;
usingSystem.Collections.Generic;
usingUnityEngine;
publicclassTest:MonoBehaviour
{
privatestaticTest_Instance;
publicstaticTestInstance
{
get
{
return_Instance;
}
}
voidAwake()
{
_Instance=this;
}
}
封装类
我们将单例封装好,在使用的时候直接在需要使用单例的类里继承我们的封装类即可。
普通单例类:
using System;
///
/// 普通单例类
///
/// 子类类型
public class Singleton : IDisposable where T : new()
{
private static T _Instance;
public static T Instance
{
get
{
if (_Instance == null)
{
_Instance = new T();
}
return _Instance;
}
}
public virtual void Dispose() { }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
usingSystem;
///
/// 普通单例类
///
/// 子类类型
publicclassSingleton:IDisposablewhereT:new()
{
privatestaticT_Instance;
publicstaticTInstance
{
get
{
if(_Instance==null)
{
_Instance=newT();
}
return_Instance;
}
}
publicvirtualvoidDispose(){}
}
需要继承MonoBehaviour的单例类:
using UnityEngine;
///
/// 继承自MonoBehaviour的单例类
///
/// 子类类型
public class SingletonMono : MonoBehaviour where T : MonoBehaviour
{
private static T _Instance;
public static T Instance
{
get
{
if (_Instance == null)
{
GameObject obj = new GameObject(typeof(T).Name);
_Instance = obj.GetOrAddComponent();
DontDestroyOnLoad(obj);
}
return _Instance;
}
}
void Awake()
{
OnAwake();
}
void Start()
{
OnStart();
}
void Update()
{
OnUpdate();
}
void OnDestroy()
{
BeforeOnDestroy();
}
protected virtual void OnAwake() { }
protected virtual void OnStart() { }
protected virtual void OnUpdate() { }
protected virtual void BeforeOnDestroy() { }
}
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
47
48
49
usingUnityEngine;
///
/// 继承自MonoBehaviour的单例类
///
/// 子类类型
publicclassSingletonMono:MonoBehaviourwhereT:MonoBehaviour
{
privatestaticT_Instance;
publicstaticTInstance
{
get
{
if(_Instance==null)
{
GameObjectobj=newGameObject(typeof(T).Name);
_Instance=obj.GetOrAddComponent();
DontDestroyOnLoad(obj);
}
return_Instance;
}
}
voidAwake()
{
OnAwake();
}
voidStart()
{
OnStart();
}
voidUpdate()
{
OnUpdate();
}
voidOnDestroy()
{
BeforeOnDestroy();
}
protectedvirtualvoidOnAwake(){}
protectedvirtualvoidOnStart(){}
protectedvirtualvoidOnUpdate(){}
protectedvirtualvoidBeforeOnDestroy(){}
}
需要继承自MonoBehaviour的单例类的生命周期方法都最好在封装类中实现,并提供对应的虚方法给子类重写。
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
