| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEditor; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | public class TestSceneIntegrityChecker |
| | 7 | | { |
| | 8 | | private const bool DEBUG_PAUSE_ON_INTEGRITY_FAIL = false; |
| 501 | 9 | | public bool enabled = true; |
| | 10 | |
|
| 501 | 11 | | protected List<Component> startingSceneComponents = new List<Component>(); |
| | 12 | |
|
| | 13 | | public IEnumerator SaveSceneSnapshot() |
| | 14 | | { |
| 501 | 15 | | if (!enabled) |
| 501 | 16 | | yield break; |
| | 17 | |
|
| | 18 | | //NOTE(Brian): to make it run faster in CI |
| 0 | 19 | | if (Application.isBatchMode) |
| 0 | 20 | | yield break; |
| | 21 | |
|
| 0 | 22 | | yield return null; |
| 0 | 23 | | startingSceneComponents = GetAllSceneComponents(); |
| 0 | 24 | | } |
| | 25 | |
|
| | 26 | | static List<Component> GetAllSceneComponents() |
| | 27 | | { |
| 0 | 28 | | if (!(Resources.FindObjectsOfTypeAll(typeof(Component)) is Component[] components)) |
| 0 | 29 | | return new List<Component>(); |
| | 30 | |
|
| 0 | 31 | | List<Component> result = new List<Component>(); |
| | 32 | |
|
| 0 | 33 | | foreach (Component go in components) |
| | 34 | | { |
| 0 | 35 | | bool isPersistent = EditorUtility.IsPersistent(go.transform.root.gameObject); |
| 0 | 36 | | bool isEditableOrVisible = go.hideFlags == HideFlags.NotEditable || go.hideFlags == HideFlags.HideAndDontSav |
| | 37 | |
|
| 0 | 38 | | if (!isPersistent && !isEditableOrVisible) |
| | 39 | | { |
| 0 | 40 | | result.Add(go); |
| | 41 | | } |
| | 42 | | } |
| | 43 | |
|
| 0 | 44 | | return result; |
| | 45 | | } |
| | 46 | |
|
| | 47 | | public IEnumerator TestSceneSnapshot() |
| | 48 | | { |
| 501 | 49 | | if (!enabled) |
| 501 | 50 | | yield break; |
| | 51 | |
|
| 0 | 52 | | if (startingSceneComponents == null) |
| | 53 | | { |
| 0 | 54 | | Debug.LogError("SceneIntegrityChecker fail. Check called without Saving snapshot?"); |
| 0 | 55 | | yield break; |
| | 56 | | } |
| | 57 | |
|
| | 58 | | //NOTE(Brian): If any Destroy() calls are pending, this will flush them. |
| 0 | 59 | | yield return null; |
| | 60 | |
|
| 0 | 61 | | List<Component> currentObjects = GetAllSceneComponents(); |
| 0 | 62 | | List<Component> newObjects = new List<Component>(); |
| | 63 | |
|
| 0 | 64 | | foreach (var o in currentObjects) |
| | 65 | | { |
| 0 | 66 | | if (o.ToString().Contains("MainCamera")) |
| | 67 | | continue; |
| | 68 | |
|
| 0 | 69 | | if (!startingSceneComponents.Contains(o)) |
| | 70 | | { |
| 0 | 71 | | newObjects.Add(o); |
| | 72 | | } |
| | 73 | | } |
| | 74 | |
|
| 0 | 75 | | if (newObjects.Count > 0) |
| | 76 | | { |
| 0 | 77 | | Debug.LogError("Dangling components detected!. Look your TearDown code, you missed to destroy objects after |
| | 78 | |
|
| | 79 | | //NOTE(Brian): Can't use asserts here because Unity Editor hangs for some reason. |
| 0 | 80 | | foreach (var o in newObjects) |
| | 81 | | { |
| | 82 | | if (DEBUG_PAUSE_ON_INTEGRITY_FAIL) |
| | 83 | | Debug.LogError($"Component - {o} (id: {o.GetInstanceID()}) (Click to highlight)", o.gameObject); |
| | 84 | | else |
| 0 | 85 | | Debug.LogError($"Component - {o}", o.gameObject); |
| | 86 | | } |
| | 87 | |
|
| | 88 | | if (DEBUG_PAUSE_ON_INTEGRITY_FAIL) |
| | 89 | | { |
| | 90 | | Debug.Break(); |
| | 91 | | yield return null; |
| | 92 | | } |
| | 93 | | } |
| 0 | 94 | | } |
| | 95 | | } |