UnityEssentials
Small but useful tools and features for Unity
PresetsTools.cs
Go to the documentation of this file.
1 #if UNITY_EDITOR
2 using UnityEditor;
3 using UnityEditor.Presets;
4 using UnityEngine;
5 
6 namespace Essentials.Presets
7 {
11  public static class PresetsTools
12  {
17  [MenuItem("CONTEXT/Preset/Validate all Game Objects in scene")]
18  public static void ValidateAllGameObjectsInScene(MenuCommand command)
19  {
20  // Get our current selected Preset.
21  Preset referencePreset = command.context as Preset;
22 
23  if (referencePreset == null)
24  return;
25 
26  bool foundAnyMissmatch = false;
27 
28  GameObject[] allObjects = UnityEngine.Object.FindObjectsOfType<GameObject>() ;
29  foreach (GameObject go in allObjects)
30  {
31  //if (!go.activeInHierarchy)
32  // continue;
33 
34  foreach (Component component in go.GetComponents(typeof(Component)))
35  {
36  if (!referencePreset.CanBeAppliedTo(component))
37  continue;
38 
39  if (!referencePreset.DataEquals(component))
40  {
41  Debug.LogWarning($"The '{referencePreset.GetTargetTypeName()}' in the Game Object '{go}' does not match the selected preset.", component);
42  foundAnyMissmatch = true;
43  }
44 
45  }
46 
47  }
48 
49  if (!foundAnyMissmatch)
50  Debug.Log("All GameObjects' components in the scene are configured according to the selected preset.");
51  }
52 
53 
57  [MenuItem("GameObject/Presets/Search mismatches between scene GameObjects and default Presets", false, -20)]
58  public static bool AreAllGameObjectsInSceneMatchingWithDefaultPresets()
59  {
60  bool foundAnyMissmatch = false;
61 
62  GameObject[] allObjects = UnityEngine.Object.FindObjectsOfType<GameObject>() ;
63  //Transform[] allObjectsTransforms = Selection.transforms;
64  foreach (GameObject go in allObjects)
65  {
66  foreach (Component component in go.GetComponents(typeof(Component)))
67  {
68  Preset[] defaults = Preset.GetDefaultPresetsForObject(component);
69 
70  foreach (Preset defaultPreset in defaults)
71  {
72  // Debug.Log($"Checking {defaultPreset.name} against '{defaultPreset.GetTargetTypeName()}' Component of '{go}' GameObject");
73  if (!defaultPreset.CanBeAppliedTo(component))
74  {
75  continue;
76  }
77  else
78  {
79  if (!defaultPreset.DataEquals(component))
80  {
81  Debug.LogWarning($"The '{defaultPreset.GetTargetTypeName()}' in the Game Object '{go}' does not match the default preset ({defaultPreset.name}).", component);
82  foundAnyMissmatch = true;
83  }
84  break;
85  }
86  }
87  }
88  }
89 
90  if (foundAnyMissmatch)
91  return false;
92 
93  Debug.Log("All GameObjects' components in the scene are configured according to the selected preset.");
94  return true;
95  }
96  }
97 }
98 #endif