Unity程序开发框架——单例模式基类模块

  • 简单了解单例模式

    • 在有些系统中,为了节省内存资源、保证数据内容的一致性,对某些类要求只能创建一个实例,这就是所谓的单例模式。
    • 单例(Singleton)模式的定义:指一个类只有一个实例,且该类能自行创建这个实例的一种模式。
  • 单例模式有 3 个特点:
    • 单例类只有一个实例对象;
    • 该单例对象必须由单例类自行创建;
    • 单例类对外提供一个访问该单例的全局访问点。
  • 单例模式基类模块

在Unity程序中我们利用单例模式的类保证内存里只有一个实例,减少了内存的开销,可以避免对资源的多重占用,单例模式设置全局访问点,可以优化和共享资源的访问。

  • BaseManager单例模式泛型基类,针对的脚本不是U3D对象。
//1.C#中 泛型的知识
//2.设计模式中 单例模式的知识
public class BaseManager<T> where T:new()
{
    private static T instance;
    public static T GetInstance()
    {
        if (instance == null)
            instance = new T();
        return instance;
    }
}
  • 继承MonoBehaviour的单例模式基类
//C#中 泛型知识点
//设计模式 单例模式的知识点
//继承了 MonoBehaviour 的 单例模式对象 需要我们自己保证它的唯一性
public class SingletonMono<T> : MonoBehaviour where T: MonoBehaviour
{
    private static T instance;
    public static T GetInstance()
    {
        //继承了Mono的脚本 不能够直接new
        //只能通过拖动到对象上 或者 通过 加脚本的api AddComponent去加脚本
        //U3D内部帮助我们实例化它
        return instance;
    }
    protected virtual void Awake()
    {
        instance = this as T;
    }
}

继承此类的脚本,若在多处游戏对象中使用时,挂载此对象的最后一个起作用!

  • 继承MonoBehaviour的存在整个程序生命周期中的单例模式基类
public class SingletonAutoMono<T> : MonoBehaviour where T : MonoBehaviour
{
    private static T instance;

    public static T GetInstance()
    {
        if( instance == null )
        {
            GameObject obj = new GameObject();
            //设置对象的名字为脚本名
            obj.name = typeof(T).ToString();
            //让这个单例模式对象 过场景 不移除
            //因为 单例模式对象 往往 是存在整个程序生命周期中的
            DontDestroyOnLoad(obj);
            instance = obj.AddComponent<T>();
        }
        return instance;
    }
}

继承此类的脚本,过场景不移除,这个基类不需要手动添加或者在脚本里进行添加,想用它直接 GetInstance就行。

  • 应用测试

1. 测试使用BaseManager基类。
  • 新建一个NewBehaviourScript增加一个测试方法。
public class NewBehaviourScript : BaseManager<NewBehaviourScript>
{
    public void testOut() {
        Debug.Log("测试输出!");
    }
}
  • 新建一个BaseTest脚本。通过NewBehaviourScript提供的GetInstance来调用测试方法。

public class BaseTest : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
      NewBehaviourScript.GetInstance().testOut();
    }
}

  • 挂载BaseTest脚本到Main Camera运行结果。在这里插入图片描述
2. 测试使用SingletonMono基类。
  • 改写BaseTest脚本继承SingletonMono基类。
public class BaseTest : SingletonMono<BaseTest>
{
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log(BaseTest.GetInstance().name);
    }
}
  • 挂载BaseTestMain Camera运行结果。
    在这里插入图片描述
3. 测试使用SingletonAutoMono基类。
public class BaseTest : SingletonAutoMono<BaseTest>
{
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log(BaseTest.GetInstance().name);
    }
}

在这里插入图片描述
为什么会有两次输出,思考一下!
链接:
Unity程序开发框架——单例模式基类模块.
Unity程序开发框架——缓存池模块.
Unity程序开发框架——事件中心模块.
Unity程序开发框架——公共Mono模块.
Unity程序开发框架——场景切换模块.
Unity程序开发框架——资源加载模块.
Unity程序开发框架——输入控制模块.
Unity程序开发框架——UI管理模块.

  • 6
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
### 回答1: 在Unity中实现一个单例模式非常简单,首先需要创建一个脚本,在脚本中声明一个静态变量,在 Awake() 函数中实例化该变量,并将其设为 DontDestroyOnLoad(),这样就可以保证在场景加载时只会实例化一次。 ### 回答2: 在Unity中实现一个单例模式可以通过使用C#的静态变量和方法来实现。 首先,创建一个单例类,我们将其命名为SingletonClass。在该类中需要有一个私有的静态变量instance,用于存储实例化后的SingletonClass对象。如下所示: ```csharp public class SingletonClass { private static SingletonClass instance; // 私有构造函数,防止通过实例化创建多个对象 private SingletonClass() { } // 公有静态方法,通过该方法获取SingletonClass的唯一实例 public static SingletonClass GetInstance() { if (instance == null) { instance = new SingletonClass(); } return instance; } } ``` 上述代码中,构造函数被私有化,以防止在类外部通过实例化创建多个对象。GetInstace()方法是一个公有的静态方法,用于返回SingletonClass的唯一实例。在该方法内部,会判断instance变量是否为空,如果为空则实例化一个SingletonClass对象并赋值给instance。 接下来,在Unity场景中使用该单例类可以按照以下步骤进行。 1. 创建一个空的GameObject,将其命名为SingletonManager。 2. 给SingletonManager游戏对象添加一个脚本,我们将其命名为SingletonManagerScript。 3. 在SingletonManagerScript脚本中,定义一个私有的SingletonClass类型的变量,用于存储SingletonClass的实例。 4. 在Awake()方法中,通过调用SingletonClass的GetInstance()方法获取SingletonClass的唯一实例,并将其赋值给变量。 5. 在其他脚本中,可以通过调用SingletonManagerScript的GetInstance()方法来获取SingletonClass的唯一实例。 示例代码如下: ```csharp public class SingletonManagerScript : MonoBehaviour { private SingletonClass singletonInstance; private void Awake() { singletonInstance = SingletonClass.GetInstance(); } public SingletonClass GetInstance() { return singletonInstance; } } ``` 在其他脚本中,可以通过以下代码来获取SingletonClass的实例: ```csharp SingletonManagerScript singletonManager = FindObjectOfType<SingletonManagerScript>(); SingletonClass singletonInstance = singletonManager.GetInstance(); ``` 通过以上步骤和代码,我们就可以在Unity中实现一个单例模式。 ### 回答3: 在Unity中实现单例模式,可以通过以下步骤进行: 首先,创建一个脚本类,例如SingletonManager,用于管理单例对象的创建和访问。 在SingletonManager脚本中定义一个私有静态变量_instance,用于保存单例对象的实例。 然后,定义一个公有静态属性Instance,用于返回_instance的值。在该属性的get方法中,判断_instance是否为空,如果为空,则实例化一个单例对象并赋值给_instance,如果不为空,则直接返回_instance。 接下来,将SingletonManager脚本挂载到一个空的游戏对象上,将这个游戏对象设置为不销毁,并将它命名为Singleton。 在其他需要使用单例对象的脚本中,可以通过SingletonManager.Instance来访问单例对象。例如,如果有一个GameManager的脚本需要使用单例对象,可以使用SingletonManager.Instance来获取GameManager的实例。 最后,在需要销毁单例对象的时候,可以通过Destroy(SingletonManager.Instance.gameObject)方法来销毁整个SingletonManager对象,从而销毁单例对象。 通过以上步骤,在Unity中就可以实现一个简单的单例模式。这种方式保证了在整个游戏运行期间只存在一个实例化对象,方便其他脚本访问和使用。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yxlalm

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值