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