From e9a8e02a40c09a9bfa070fe235dc8b22d468e9bd Mon Sep 17 00:00:00 2001 From: MrGadget <9826063+MrGadget1024@users.noreply.github.com> Date: Sat, 2 Nov 2024 18:29:05 -0400 Subject: [PATCH] fix(PlayerCamera): Improved cleanup - avoids error in editor --- .../Examples/_Common/Scripts/PlayerCamera.cs | 37 ++++++++++++++++--- 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/Assets/Mirror/Examples/_Common/Scripts/PlayerCamera.cs b/Assets/Mirror/Examples/_Common/Scripts/PlayerCamera.cs index 8d12c02bd..28f327cae 100644 --- a/Assets/Mirror/Examples/_Common/Scripts/PlayerCamera.cs +++ b/Assets/Mirror/Examples/_Common/Scripts/PlayerCamera.cs @@ -19,11 +19,6 @@ void Awake() mainCam = Camera.main; } - void OnDisable() - { - //Debug.Log("PlayerCamera.OnDisable"); - } - public override void OnStartLocalPlayer() { if (mainCam != null) @@ -38,16 +33,46 @@ public override void OnStartLocalPlayer() Debug.LogWarning("PlayerCamera: Could not find a camera in scene with 'MainCamera' tag."); } + void OnApplicationQuit() + { + //Debug.Log("PlayerCamera.OnApplicationQuit"); + ReleaseCamera(); + } + public override void OnStopLocalPlayer() + { + //Debug.Log("PlayerCamera.OnStopLocalPlayer"); + ReleaseCamera(); + } + + void OnDisable() + { + //Debug.Log("PlayerCamera.OnDisable"); + ReleaseCamera(); + } + + void OnDestroy() + { + //Debug.Log("PlayerCamera.OnDestroy"); + ReleaseCamera(); + } + + void ReleaseCamera() { if (mainCam != null && mainCam.transform.parent == transform) { + //Debug.Log("PlayerCamera.ReleaseCamera"); + mainCam.transform.SetParent(null); - SceneManager.MoveGameObjectToScene(mainCam.gameObject, SceneManager.GetActiveScene()); mainCam.orthographic = true; mainCam.orthographicSize = 15f; mainCam.transform.localPosition = new Vector3(0f, 70f, 0f); mainCam.transform.localEulerAngles = new Vector3(90f, 0f, 0f); + + if (mainCam.gameObject.scene != SceneManager.GetActiveScene()) + SceneManager.MoveGameObjectToScene(mainCam.gameObject, SceneManager.GetActiveScene()); + + mainCam = null; } } }