DontDestroyOnLoad









How do I use Dont Destroy On Load
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: ChatGui.cs Copy
58     public void Start()
59     {
60         DontDestroyOnLoad(this.gameObject);
61         Application.runInBackground = true; // this must run in background or it will drop connection if not focussed.
62
63         if (string.IsNullOrEmpty(this.UserName))
64         {
65             this.UserName = "user" + Environment.TickCount%99; //made-up username
66         }
67
68         chatClient = new ChatClient(this);
69         chatClient.Connect(ChatAppId, "1.0", this.UserName, null);
70
71         if (this.AlignBottom)
72         {
73             this.GuiRect.y = Screen.height - this.GuiRect.height;
74         }
75         if (this.FullScreen)
76         {
77             this.GuiRect.x = 0;
78             this.GuiRect.y = 0;
79             this.GuiRect.width = Screen.width;
80             this.GuiRect.height = Screen.height;
81         }
82
83         Debug.Log(this.UserName);
84     }
File name: ToHubButton.cs Copy
10  void Start ()
11     {
12         if (ButtonTexture == null)
13         {
14             this.gameObject.SetActive(false);
15             return;
16         }
17      DontDestroyOnLoad(this.gameObject);
18
19  }
File name: NetworkingPeer.cs Copy
2688     public void RegisterPhotonView(PhotonView netView)
2689     {
2690         if (!Application.isPlaying)
2691         {
2692             this.photonViewList = new Dictionary();
2693             return;
2694         }
2695
2696         if (netView.viewID == 0)
2697         {
2698             // don't register views with ID 0 (not initialized). they register when a ID is assigned later on
2699             Debug.Log("PhotonView register is ignored, because viewID is 0. No id assigned yet to: " + netView);
2700             return;
2701         }
2702
2703         if (this.photonViewList.ContainsKey(netView.viewID))
2704         {
2705             // if some other view is in the list already, we got a problem. it might be undestructible. print out error
2706             if (netView != photonViewList[netView.viewID])
2707             {
2708                 Debug.LogError(string.Format("PhotonView ID duplicate found: {0}. New: {1} old: {2}. Maybe one wasn't destroyed on scene load?! Check for 'DontDestroyOnLoad'. Destroying old entry, adding new.", netView.viewID, netView, photonViewList[netView.viewID]));
2709             }
2710
2711             //this.photonViewList.Remove(netView.viewID); // TODO check if we chould Destroy the GO of this view?!
2712             this.RemoveInstantiatedGO(photonViewList[netView.viewID].gameObject, true);
2713         }
2714
2715         // Debug.Log("adding view to known list: " + netView);
2716         this.photonViewList.Add(netView.viewID, netView);
2717         //Debug.LogError("view being added. " + netView); // Exit Games internal log
2718
2719         if (PhotonNetwork.logLevel >= PhotonLogLevel.Full)
2720             Debug.Log("Registered PhotonView: " + netView.viewID);
2721     }
File name: PhotonHandler.cs Copy
24     public int updateIntervalOnSerialize; // time [ms] between consecutive RunViewUpdate calls (sending syncs, etc)
35     protected void Awake()
36     {
37         if (SP != null && SP != this && SP.gameObject != null)
38         {
39             GameObject.DestroyImmediate(SP.gameObject);
40         }
41
42         SP = this;
43         DontDestroyOnLoad(this.gameObject);
44
45         this.updateInterval = 1000 / PhotonNetwork.sendRate;
46         this.updateIntervalOnSerialize = 1000 / PhotonNetwork.sendRateOnSerialize;
47
48         PhotonHandler.StartFallbackSendAckThread();
49     }
File name: SupportLogger.cs Copy
9     public void Start()
10     {
11         GameObject go = GameObject.Find("PunSupportLogger");
12         if (go == null)
13         {
14             go = new GameObject("PunSupportLogger");
15             DontDestroyOnLoad(go);
16             SupportLogging sl = go.AddComponent();
17             sl.LogTrafficStats = this.LogTrafficStats;
18         }
19     }
File name: Singleton.cs Copy
12  public static T Instance {
13   get {
14     T foundObject = FindObjectOfType();
15
16     if (instance == null) {
17      instance = foundObject;
18     } else if (instance != foundObject) {
19      Destroy(foundObject);
20     }
21
22     if (!_destroyOnLoad) DontDestroyOnLoad(foundObject);
23     return instance;
24    }
25  }
File name: Scores.cs Copy
25  void MakeSingleton () {
26   if (instance != null) {
27    Destroy (gameObject);
28   } else {
29    instance = this;
30    DontDestroyOnLoad (gameObject);
31   }
32  }
File name: GameController.cs Copy
33  void CreateInstance(){
34   if (instance != null) {
35    Destroy (gameObject);
36   } else {
37    instance = this;
38    DontDestroyOnLoad (gameObject);
39   }
40  }
File name: MusicController.cs Copy
23  void CreateInstance(){
24   if (instance != null) {
25    Destroy (gameObject);
26   } else {
27    instance = this;
28    DontDestroyOnLoad (gameObject);
29   }
30  }
File name: GameController.cs Copy
29  void CreateInstance(){
30   if (instance != null) {
31    Destroy (gameObject);
32   } else {
33    instance = this;
34    DontDestroyOnLoad (gameObject);
35   }
36  }

DontDestroyOnLoad 90 lượt xem

Gõ tìm kiếm nhanh...