1. 程式人生 > >Unity用代碼實現Remove Missing Script

Unity用代碼實現Remove Missing Script

null rop serialize ctu ssi etc ati href upm

 1 [MenuItem("Edit/Cleanup Missing Scripts")]
 2  static void CleanupMissingScripts ()
 3  {
 4      for(int i = 0; i < Selection.gameObjects.Length; i++)
 5      {
 6          var gameObject = Selection.gameObjects[i];
 7 
 8          // We must use the GetComponents array to actually detect missing components
9 var components = gameObject.GetComponents<Component>(); 10 11 // Create a serialized object so that we can edit the component list 12 var serializedObject = new SerializedObject(gameObject); 13 // Find the component list property 14 var prop = serializedObject.FindProperty("
m_Component"); 15 16 // Track how many components we‘ve removed 17 int r = 0; 18 19 // Iterate over all components 20 for(int j = 0; j < components.Length; j++) 21 { 22 // Check if the ref is null 23 if(components[j] == null) 24 {
25 // If so, remove from the serialized component array 26 prop.DeleteArrayElementAtIndex(j-r); 27 // Increment removed count 28 r++; 29 } 30 } 31 32 // Apply our changes to the game object 33 serializedObject.ApplyModifiedProperties(); 34 } 35 }

原文鏈接:http://blog.csdn.net/zzmkljd/article/details/52840724

Unity用代碼實現Remove Missing Script