OnDestroy









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

Featured Snippets


File name: PhotonView.cs Copy
295     protected internal void OnDestroy()
296     {
297         if (!this.destroyedByPhotonNetworkOrQuit)
298         {
299             PhotonNetwork.networkingPeer.LocalCleanPhotonView(this);
300         }
301
302         if (!this.destroyedByPhotonNetworkOrQuit && !Application.isLoadingLevel)
303         {
304             if (this.instantiationId > 0)
305             {
306                 // if this viewID was not manually assigned (and we're not shutting down or loading a level), you should use PhotonNetwork.Destroy() to get rid of GOs with PhotonViews
307                 Debug.LogError("OnDestroy() seems to be called without PhotonNetwork.Destroy()?! GameObject: " + this.gameObject + " Application.isLoadingLevel: " + Application.isLoadingLevel);
308             }
309             else
310             {
311                 // this seems to be a manually instantiated PV. if it's local, we could warn if the ID is not in the allocated-list
312                 if (this.viewID <= 0)
313                 {
314                     Debug.LogWarning(string.Format("OnDestroy manually allocated PhotonView {0}. The viewID is 0. Was it ever (manually) set?", this));
315                 }
316                 else if (this.isMine && !PhotonNetwork.manuallyAllocatedViewIds.Contains(this.viewID))
317                 {
318                     Debug.LogWarning(string.Format("OnDestroy manually allocated PhotonView {0}. The viewID is local (isMine) but not in manuallyAllocatedViewIds list. Use UnAllocateViewID() after you destroyed the PV.", this));
319                 }
320             }
321         }
322     }
File name: OnClickDestroy.cs Copy
39     public IEnumerator DestroyRpc()
40     {
41         GameObject.Destroy(this.gameObject);
42         yield return 0; // if you allow 1 frame to pass, the object's OnDestroy() method gets called and cleans up references.
43         PhotonNetwork.UnAllocateViewID(this.photonView.viewID);
44     }
File name: NetworkAdapter.cs Copy
152         private void OnDestroy()
153         {
154             ServiceLocator.RemoveService();
155         }
File name: Connecting.cs Copy
37         protected override void OnDestroy()
38         {
39             base.OnDestroy();
40
41             NetworkService.OnBeginConnectingSignal.RemoveListener(Show);
42             NetworkService.OnConnectedToMasterSignal.RemoveListener(OnConnectedToMaster);
43             NetworkService.OnConnectionFailSignal.RemoveListener(OnConnectionFail);
44         }
File name: Hud.cs Copy
38         protected override void OnDestroy()
39         {
40             base.OnDestroy();
41
42             GameService.OnGameStartSignal.RemoveListener(OnGameStart);
43             GameService.OnGameResultSignal.RemoveListener(UpdateGameScore);
44             GameService.OnGameQuitSignal.RemoveListener(Hide);
45         }
File name: Lobby.cs Copy
58         protected override void OnDestroy()
59         {
60             base.OnDestroy();
61
62             NetworkService.OnConnectedToMasterSignal.RemoveListener(Show);
63             NetworkService.OnJoinedRoomSignal.RemoveListener(Hide);
64             NetworkService.OnDisconnectedFromMasterSignal.RemoveListener(Hide);
65         }
File name: MainMenu.cs Copy
26         protected override void OnDestroy()
27         {
28             base.OnDestroy();
29
30             GameService.OnGameQuitSignal.RemoveListener(Show);
31         }
File name: Result.cs Copy
32         protected override void OnDestroy()
33         {
34             base.OnDestroy();
35
36             GameService.OnGameResultSignal.RemoveListener(OnGameResult);
37             NetworkService.OnNewGameStartedSignal.RemoveListener(OnNewGame);
38             NetworkService.OnDisconnectedFromMasterSignal.RemoveListener(Hide);
39         }
File name: Waiting.cs Copy
22         protected override void OnDestroy()
23         {
24             base.OnDestroy();
25
26             NetworkService.OnJoinedRoomSignal.RemoveListener(Show);
27             NetworkService.OnAllPlayersConnectedSignal.RemoveListener(Hide);
28             NetworkService.OnDisconnectedFromMasterSignal.RemoveListener(Hide);
29         }
File name: CoreBehaviour.cs Copy
32         protected virtual void OnDestroy()
33         {
34         }

OnDestroy 128 lượt xem

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