[Unity] unity单例的用法《转老外》

查看:1518 |回复:22 | 2015-4-21 22:15:24

您需要 登录 才可以下载或查看,没有账号?注册

x
本帖最后由 大西几 于 2021-2-24 16:18 编辑

最简单的用法:下流的用法
public class MusicManager : MonoBehaviour
{
    //We make a static variable to our MusicManager instance
    public static MusicManager instance { get; private set; }

    //When the object awakens, we assign the static variable
    void Awake()
    {
        instance = this;
    }

    public void Play()
    {
        //Play some audio!
    }
}

标准用法
public class MusicManager : MonoBehaviour
{
    //Here is a private reference only this class can access
    private static MusicManager _instance;

    //This is the public reference that other classes will use
    public static MusicManager instance
    {
        get
        {
            //If _instance hasn't been set yet, we grab it from the scene!
            //This will only happen the first time this reference is used.
            if(_instance == null)
                _instance = GameObject.FindObjectOfType<MusicManager>();
            return _instance;
        }
    }

    public void Play()
    {
        //Play some audio!
    }
}
完美用法
public class MusicManager : MonoBehaviour
{
    private static MusicManager _instance;

    public static MusicManager instance
    {
        get
        {
            if(_instance == null)
            {
                _instance = GameObject.FindObjectOfType<MusicManager>();

                //Tell unity not to destroy this object when loading a new scene!
                DontDestroyOnLoad(_instance.gameObject);
            }

            return _instance;
        }
    }

    void Awake()
    {
        if(_instance == null)
        {
            //If I am the first instance, make me the Singleton
            _instance = this;
            DontDestroyOnLoad(this);
        }
        else
        {
            //If a Singleton already exists and you find
            //another reference in scene, destroy it!
            if(this != _instance)
                Destroy(this.gameObject);
        }
    }

    public void Play()
    {
        //Play some audio!
    }
} image.png
2015-4-21 22:15:24  
 赞 赞 1

使用道具 登录

22个回答,把该问题分享到群,邀请大神一起回答。
2#
给力!元素有你更精彩
回复 收起回复
2015-4-23 00:49:00   回复
 赞 赞 1

使用道具 登录

3#
元素那么大,我想来看看!
回复 收起回复
2015-8-20 14:35:11   回复
 赞 赞 1

使用道具 登录

4#
立刻提起了精神。
回复 收起回复
2015-8-21 11:30:22   回复
 赞 赞 1

使用道具 登录

5#
资源甚好,发帖艰辛,且阅且珍惜!
回复 收起回复
2015-8-21 17:15:46   回复
 赞 赞 1

使用道具 登录

6#
为了元素币,拼了!
回复 收起回复
2015-11-11 19:01:08   回复
 赞 赞 1

使用道具 登录

7#
资源发布哪家强?元素首发称大王!
回复 收起回复
2015-11-12 00:13:18   回复
 赞 赞 1

使用道具 登录

8#
为了元素币,拼了!
回复 收起回复
2015-11-14 18:14:34   回复
 赞 赞 1

使用道具 登录

9#
666666666666666666
回复 收起回复
2015-11-17 08:22:39   回复
 赞 赞 1

使用道具 登录

10#
纯代码。。没有注释好累
回复 收起回复
2015-11-20 11:43:21   回复
 赞 赞 1

使用道具 登录

11#
资源不错,很有帮助,谢谢您的分享。
回复 收起回复
2015-11-22 12:15:41   回复
 赞 赞 1

使用道具 登录

12#
肝脸大规模 在奇才
回复 收起回复
2016-3-31 14:13:37   回复
 赞 赞 1

使用道具 登录

13#
惺惺惜惺惺
回复 收起回复
2016-4-12 20:40:58   回复
 赞 赞 1

使用道具 登录

14#
事实上事实上
回复 收起回复
2016-4-12 20:42:12   回复
 赞 赞 1

使用道具 登录

15#
感谢分享{:1_144:}
回复 收起回复
2016-4-15 23:25:21   回复
 赞 赞 1

使用道具 登录

16#
dddddddd

评分

参与人数 1元素币 -50 展开 理由
成林 -50 翻阅了下层主的所有回复.毫无意义.哪怕是使用论坛的快捷回复也好

查看全部评分

回复 收起回复
2016-4-24 20:38:41   回复
 赞 赞 1

使用道具 登录

17#
给力!元素有你更精彩
回复 收起回复
2016-5-5 10:58:20   回复
 赞 赞 1

使用道具 登录

18#
给力!元素有你更精彩
回复 收起回复
2016-5-5 10:58:24   回复
 赞 赞 1

使用道具 登录

19#
给力!元素有你更精彩
回复 收起回复
2016-5-5 10:58:27   回复
 赞 赞 1

使用道具 登录

20#
恩 因为mono不能实例化 躲不掉的mono单例写法还挺多的~
回复 收起回复
2016-8-23 10:16:51   回复
 赞 赞 1

使用道具 登录

CG 游戏行业专业问题

Unity3D技术手机游戏引擎手游引擎
12下一页
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表