daily pastebin goal
0%
SHARE
TWEET

Find and remove missing monobehaviour

a guest Jun 19th, 2017 897 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEditor;
  2. using UnityEngine;
  3.  
  4. namespace FLGCoreEditor.Utilities
  5. {
  6.     public class FindMissingScriptsRecursivelyAndRemove : EditorWindow
  7.     {
  8.         private static int _goCount;
  9.         private static int _componentsCount;
  10.         private static int _missingCount;
  11.  
  12.         private static bool _bHaveRun;
  13.  
  14.         [MenuItem("FLGCore/Editor/Utility/FindMissingScriptsRecursivelyAndRemove")]
  15.         public static void ShowWindow()
  16.         {
  17.             GetWindow(typeof(FindMissingScriptsRecursivelyAndRemove));
  18.         }
  19.  
  20.         public void OnGUI()
  21.         {
  22.             if (GUILayout.Button("Find Missing Scripts in selected GameObjects"))
  23.             {
  24.                 FindInSelected();
  25.             }
  26.  
  27.             if (!_bHaveRun) return;
  28.            
  29.             EditorGUILayout.TextField($"{_goCount} GameObjects Selected");
  30.             if(_goCount>0) EditorGUILayout.TextField($"{_componentsCount} Components");
  31.             if(_goCount>0) EditorGUILayout.TextField($"{_missingCount} Deleted");
  32.         }
  33.        
  34.         private static void FindInSelected()
  35.         {
  36.             var go = Selection.gameObjects;
  37.             _goCount = 0;
  38.             _componentsCount = 0;
  39.             _missingCount = 0;
  40.             foreach (var g in go)
  41.             {
  42.                 FindInGo(g);
  43.             }
  44.  
  45.             _bHaveRun = true;
  46.             Debug.Log($"Searched {_goCount} GameObjects, {_componentsCount} components, found {_missingCount} missing");
  47.            
  48.             AssetDatabase.SaveAssets();
  49.         }
  50.  
  51.         private static void FindInGo(GameObject g)
  52.         {
  53.             _goCount++;
  54.             var components = g.GetComponents<Component>();
  55.          
  56.             var r = 0;
  57.            
  58.             for (var i = 0; i < components.Length; i++)
  59.             {
  60.                 _componentsCount++;
  61.                 if (components[i] != null) continue;
  62.                 _missingCount++;
  63.                 var s = g.name;
  64.                 var t = g.transform;
  65.                 while (t.parent != null)
  66.                 {
  67.                     s = t.parent.name +"/"+s;
  68.                     t = t.parent;
  69.                 }
  70.                
  71.                 Debug.Log ($"{s} has a missing script at {i}", g);
  72.                
  73.                 var serializedObject = new SerializedObject(g);
  74.                
  75.                 var prop = serializedObject.FindProperty("m_Component");
  76.                
  77.                 prop.DeleteArrayElementAtIndex(i-r);
  78.                 r++;
  79.          
  80.                 serializedObject.ApplyModifiedProperties();
  81.             }
  82.            
  83.             foreach (Transform childT in g.transform)
  84.             {
  85.                 FindInGo(childT.gameObject);
  86.             }
  87.         }
  88.     }
  89. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top