Unity中的Mono管理器

在Unity中我们通常会遇到这样的场景,在一个不继承Monobehaviour类的类中,想使用Mono behaviour类中的方法,这时候我们应该怎么办呢?

这时候就需要我们在游戏中创建一个Mono管理器了,它的作用就是让我们可以在不继承Mono behaviour的类中使用Monobehaviour中的方法,那Mono管理器应该怎样写呢?接下来让我们来看一下。

首先我们先实现使用Mono管理器,开启协程,关闭协程的方法。

我们先创建一个Monocontroll类继承Monobehaviour

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MonoControll : MonoBehaviour
{
   
}

然后我们创建一个MonoManger类用来管理,当然MonoManger类需要继承单例模式基类,因为整个游戏中我们只需要一个MonoManger类即可。在MonoManger类中我们调用Monocontroll类中开启协程的方法,关闭协程的方法即可。当然我们还需要判断Monocontroll是否存在,如果不存在,我们需要使用代码创建一个游戏对象,并把Monocontroll脚本挂载上去。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
Title:
Description:
*/

public class MonoManger : Singleton<MonoManger>
{
    private MonoManger() { }
    private MonoControll monocontroll;
    public MonoControll MonoControll
    {
        get
        {
            if(monocontroll==null)
            {
                GameObject go = new GameObject(typeof(MonoControll).Name);
                monocontroll=go.AddComponent<MonoControll>();
            }
            return monocontroll;
        }
    } 
    public Coroutine Startcoroutine(IEnumerator routine)
    {
        return MonoControll.StartCoroutine(routine);
    }
    public void Stopcoroutine(Coroutine routine)
    {
        MonoControll.StopCoroutine(routine);
    }
    public void  StopAllCoroutine()
    {
        MonoControll.StopAllCoroutines();
    }
  
}

这样我们的MonoManger类中的协程部分就写完了。接下来我们可以实验一下是否成功

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
Title:
Description:
*/

public class Player 
{
    Coroutine coroutine;

    public void Show()
    {
       coroutine= MonoManger.Instance.Startcoroutine(Mycoroutine());
    }
    public void Hide()
    {
        MonoManger.Instance.Stopcoroutine(coroutine);
    }
    public void HideAll()
    {
        MonoManger.Instance.StopAllCoroutine();
    }


   IEnumerator Mycoroutine()
    {
        while(true)
        {
            Debug.Log("11111111");
            yield return null;
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class Test :MonoBehaviour
{
    Player Player;
    private void Awake()
    {
        Player = new Player();

    }
    private void OnGUI()
    {
        if(GUI.Button(new Rect(0,0,150f,50f),"开启协程"))
        {
            Player.Show();
        }
        if(GUI.Button(new Rect(0,100f,150f,50f),"关闭协程"))
        {
            Player.Hide();
        }
        if (GUI.Button(new Rect(0, 200f, 150f, 50f), "关闭所有协程"))
        {
            Player.HideAll();
           
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值