unity gamemanager 单例示例


 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 //-----------------------------------------

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM