首页 > 编程语言 > 详细

unity gamemanager 单例示例

时间:2020-03-15 22:40:33      阅读:257      评论:0      收藏:0      [点我收藏+]
 1 using UnityEngine;
 2 using System.Collections;
 3 //-----------------------------------------
 4 //Sample Game Manager class - Singleton Object
 5 public class GameManager : MonoBehaviour 
 6 {
 7     //-----------------------------------------
 8     //C# Property to get access to singleton instance
 9     //Read only - only has get accessor
10     public static GameManager Instance
11     {
12         //return reference to private instance
13         get
14         {
15             return instance;
16         }
17     }
18 
19     //-----------------------------------------
20     private static GameManager instance = null;
21     //-----------------------------------------
22     //High score
23     public int HighScore = 0;
24 
25     //Is game paused
26     public bool IsPaused = false;
27     
28     //Is player input allowed
29     public bool InputAllowed = true;
30     //-----------------------------------------
31     // Use this for initialization
32     void Awake ()
33     {
34         //Check if any existing instance of the class exists in the scene
35         //If so, then destroy this instance
36         if(instance)
37         {
38             DestroyImmediate(gameObject);
39             return;
40         }
41 
42         //Make this active and only instance
43         instance = this;
44 
45         //Make game manager persistent
46         DontDestroyOnLoad(gameObject);
47     }
48     //-----------------------------------------
49 }
50 //-----------------------------------------

 

unity gamemanager 单例示例

原文:https://www.cnblogs.com/lucater/p/12500548.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!