From 91671bf2432717d8309f31cb263925a899b11c5a Mon Sep 17 00:00:00 2001 From: Sinai Date: Mon, 7 Jun 2021 19:27:09 +1000 Subject: [PATCH] Failsafe in case Component is null from GetComponents? --- src/UI/Inspectors/GameObjectInspector.cs | 48 ++++++++++++++---------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/src/UI/Inspectors/GameObjectInspector.cs b/src/UI/Inspectors/GameObjectInspector.cs index c89d918..f28b4fc 100644 --- a/src/UI/Inspectors/GameObjectInspector.cs +++ b/src/UI/Inspectors/GameObjectInspector.cs @@ -147,32 +147,39 @@ namespace UnityExplorer.UI.Inspectors var behaviours = GOTarget.GetComponents(); bool needRefresh = false; - if (comps.Length != componentEntries.Count || behaviours.Length != behaviourEntries.Count) - { - needRefresh = true; - } - else - { - foreach (var comp in comps) - { - if (!compInstanceIDs.Contains(comp.GetInstanceID())) - { - needRefresh = true; - break; - } - } - if (!needRefresh) + int count = 0; + foreach (var comp in comps) + { + if (!comp) + continue; + count++; + if (!compInstanceIDs.Contains(comp.GetInstanceID())) { - for (int i = 0; i < behaviours.Length; i++) + needRefresh = true; + break; + } + } + if (!needRefresh) + { + if (count != componentEntries.Count) + needRefresh = true; + else + { + count = 0; + foreach (var behaviour in behaviours) { - var behaviour = behaviours[i]; - if (behaviour.enabled != behaviourEnabledStates[i]) + if (!behaviour) + continue; + if (count >= behaviourEnabledStates.Count || behaviour.enabled != behaviourEnabledStates[count]) { needRefresh = true; break; } + count++; } + if (!needRefresh && count != behaviourEntries.Count) + needRefresh = true; } } @@ -181,9 +188,9 @@ namespace UnityExplorer.UI.Inspectors componentEntries.Clear(); compInstanceIDs.Clear(); - foreach (var comp in comps) { + if (!comp) continue; componentEntries.Add(comp); compInstanceIDs.Add(comp.GetInstanceID()); } @@ -192,6 +199,7 @@ namespace UnityExplorer.UI.Inspectors behaviourEnabledStates.Clear(); foreach (var behaviour in behaviours) { + if (!behaviour) continue; behaviourEntries.Add(behaviour); behaviourEnabledStates.Add(behaviour.enabled); } @@ -211,7 +219,7 @@ namespace UnityExplorer.UI.Inspectors private void OnAddComponentClicked(string input) { - if (ReflectionUtility.AllTypes.TryGetValue(input, out Type type)) + if (ReflectionUtility.GetTypeByName(input) is Type type) { try {