Browse Source

销毁时清除索引

lvjincheng 3 năm trước cách đây
mục cha
commit
ccd07d91bd

+ 5 - 0
Assets/BowArrow/Scripts/Game/ArmBow.cs

@@ -122,6 +122,11 @@ public class ArmBow : MonoBehaviour
         this.readyShoot();
     }
 
+    void OnDestroy()
+    {
+        if (_ins == this) _ins = null;
+    }
+
     void OnEnable() 
     {
         AudioMgr.GetAudioSource(this.gameObject).clip = null;

+ 1 - 1
Assets/BowArrow/Scripts/Game/Arrow.cs

@@ -50,7 +50,7 @@ public class Arrow : MonoBehaviour
 
     void OnDestroy() {
         arrowSet.Remove(this);
-        GameMgr.ins.gameMode.ResumeTimeCounting(this);
+        if (GameMgr.ins) GameMgr.ins.gameMode.ResumeTimeCounting(this);
     }
 
     void Start()

+ 5 - 0
Assets/BowArrow/Scripts/Game/BowCamera.cs

@@ -90,6 +90,11 @@ public class BowCamera : MonoBehaviour
         };
     }
 
+    void OnDestroy()
+    {
+        if (_ins == this) _ins = null;
+    }
+
     void Update()
     {
         //更新相机视野

+ 4 - 0
Assets/BowArrow/Scripts/Game/CameraToLook.cs

@@ -12,4 +12,8 @@ public class CameraToLook : MonoBehaviour
         ins = this;
         point = transform.Find("Point");
     }
+    void OnDestroy()
+    {
+        if (ins == this) ins = null;
+    }
 }

+ 5 - 0
Assets/BowArrow/Scripts/Game/CrossHair.cs

@@ -16,6 +16,11 @@ public class CrossHair : MonoBehaviour
         ins = this;
     }
 
+    void OnDestroy()
+    {
+        if (ins == this) ins = null;
+    }
+
     void Start()
     {
         this.image = this.GetComponent<Image>();

+ 1 - 0
Assets/BowArrow/Scripts/Game/GameAssistUI.cs

@@ -79,6 +79,7 @@ public class GameAssistUI : MonoBehaviour
 
     void OnDestroy() {
         SaveMyRecord();
+        if (ins == null) ins = null;
     }
 
     void Update() {

+ 5 - 0
Assets/BowArrow/Scripts/Game/TargetBody.cs

@@ -12,6 +12,11 @@ public class TargetBody : MonoBehaviour
         SetDistance(distance);
     }
 
+    void OnDestroy()
+    {
+        if (ins == this) ins = null;
+    }
+
     void Update()
     {
         TargetView targetView = TargetView.ins;

+ 1 - 1
Assets/BowArrow/Scripts/Game/TargetView.cs

@@ -49,7 +49,7 @@ public class TargetView : MonoBehaviour
 
     void OnDestroy()
     {
-        _ins = null;
+        if (_ins == null) _ins = null;
     }
     
     private static TargetView _ins = null;

+ 3 - 4
Assets/BowArrow/Scripts/Manager/AudioMgr.cs

@@ -9,15 +9,14 @@ public class AudioMgr : MonoBehaviour
 
     public static void Init() 
     {
-        if (GameObject.Find("AudioMgr") == null) {
+        if (!ins) {
             GameObject audioMgr = new GameObject("AudioMgr");
-            audioMgr.AddComponent<AudioMgr>();
+            ins = audioMgr.AddComponent<AudioMgr>();
             DontDestroyOnLoad(audioMgr);
         }
     }
 
-    void Start() {
-        ins = this;
+    void Awake() {
         this.audioSource = this.gameObject.AddComponent<AudioSource>();
     }
 

+ 1 - 0
Assets/BowArrow/Scripts/Manager/GameMgr.cs

@@ -53,6 +53,7 @@ public class GameMgr : MonoBehaviour
         } catch (System.Exception e) { Debug.LogError(e.Message); }
         if (AimHandler.ins) AimHandler.ins.Ban9AxisCalculate(false);
         clearLockerForGamePause();
+        if (ins == this) ins = null;
     }
 
     void FixedUpdate()

+ 1 - 1
Assets/BowArrow/Scripts/Manager/RoleMgr.cs

@@ -2,7 +2,7 @@ using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 /* 内置角色信息管理者,(PK模式用到的角色选择) */
-public class RoleMgr : MonoBehaviour
+public class RoleMgr
 {
     static string[] roleNames = {"海绵宝宝", "方狗狗", "五六七", "CHANEL", "Mimi", "喵了个咪"};
     public static int roleCount {

+ 1 - 0
Assets/BowArrow/Scripts/View/DeviceReconnectView.cs

@@ -46,6 +46,7 @@ public class DeviceReconnectView : MonoBehaviour
     void OnDestroy()
     {
         GameMgr.ins.removeLockerForGamePause(this);
+        if (ins == this) ins = null;
     }
 
     void Update()

+ 5 - 0
Assets/BowArrow/Scripts/View/HomeView.cs

@@ -40,6 +40,11 @@ public class HomeView : MonoBehaviour
         }
     }
 
+    void OnDestroy()
+    {
+        if (ins == this) ins = null;
+    }
+
     void FixedUpdate()
     {
         UpdateBtnForConnect();

+ 5 - 0
Assets/BowArrow/Scripts/View/TimeLimitGameView.cs

@@ -27,6 +27,11 @@ public class TimeLimitGameView : MonoBehaviour
         }
     }
 
+    void OnDestroy()
+    {
+        if (ins == this) ins = null;
+    }
+
     void Update()
     {
         timeTxt.text = gameMode.GetTimeStr();