您当前的位置: 首页 >  unity

Peter_Gao_

暂无认证

  • 0浏览

    0关注

    621博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Unity3D 中DontDestroyOnLoad单例模式的使用DDOL

Peter_Gao_ 发布时间:2020-04-09 14:58:49 ,浏览量:0

  1. 继承于MonoBehaviour(不随着场景切换而销毁)

  基类代码:

复制代码

 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 
 5 /// 
 6 /// 单例模式基类,继承于 MonoBehaviour,不随场景切换而销毁
 7 /// 在场景中新建空物体 DDOL,挂载 T 类继承 DDOLSingleton 即可
 8 /// 
 9 /// 
10 public abstract class DDOLSingleton : MonoBehaviour where T : DDOLSingleton
11 {
12     protected static T _instance = null;
13 
14     public static T Instance
15     {
16         get
17         {
18             if (null == _instance)
19             {
20                 GameObject go = GameObject.Find("DDOL");
21                 if (null == go)
22                 {
23                     go = new GameObject("DDOL");
24                     DontDestroyOnLoad(go);
25                 }
26                 _instance = go.GetComponent();
27                 if (null == _instance)
28                 {
29                     _instance = go.AddComponent();
30                 }
31             }
32             return _instance;
33         }
34     }
35 }

复制代码

  测试:

  新建空实体 DDOL,挂载脚本 GameManage。

  新建一个按钮,实现加载下一场景功能,测试是否随场景销毁而销毁。

  Hierarchy :

  GameManage 代码:

复制代码

 1 using System.Collections.Generic;
 2 using UnityEngine;
 3 
 4 /// 
 5 /// 单例
 6 /// 
 7 public class GameManage : DDOLSingleton {
 8     public void TestMethod()
 9     {
10         Debug.Log("GameManage");
11     }
12 }

复制代码

 

  测试代码: 

复制代码

 1 /// 
 2 /// 测试单例
 3 /// 
 4 public class Test : MonoBehaviour {
 5 
 6     // Use this for initialization
 7     void Start () {
 8         GameManage.Instance.TestMethod();
 9     }
10     
11     // Update is called once per frame
12     void Update () {
13         
14     }
15 
16     public void OnBtnClick()
17     {
18         SceneManager.LoadScene(1);
19     }
20 }

复制代码

  效果图:

 

 

2. 不继承于MonoBehaviour(随着场景切换而销毁)

  基类代码:

复制代码

 1 /// 
 2 /// 单例模式基类,不继承于 MonoBehaviour,随场景切换而销毁
 3 /// 挂载 T 类继承 DDOLSingleton,重载 Init 函数即可
 4 /// 
 5 /// 
 6 public abstract class Singleton where T : class, new()
 7 {
 8     protected static T _instance = null;
 9 
10     public static T Instance
11     {
12         get
13         {
14             if (null == _instance)
15             {
16                 _instance = new T();
17             }
18             return _instance;
19         }
20     }
21 
22     protected Singleton()
23     {
24         if (null != _instance)
25         {
26             Debug.LogError("This " + typeof(T).ToString() + " Singleton Instance is not null !!!");
27         }
28         Init();
29     }
30 
31     public virtual void Init()
32     {
33 
34     }
35 }

复制代码

 

 

  此时 GameManage 代码修改为:

复制代码

 1 /// 
 2 /// 测试 Singleton
 3 /// 
 4 public class GameManage : Singleton {
 5     public override void Init()
 6     {
 7         base.Init();
 8     }
 9 
10     public void TestMethod()
11     {
12         Debug.Log("GameManage");
13     }
14 }

复制代码

关注
打赏
1664521772
查看更多评论
立即登录/注册

微信扫码登录

0.0394s