tags: Unity
Thanks to Wu Yunze for part of the code.Unity batch modification prefab
Thanks to Lin Xinfa’s part of the code.Unity3D editor: delete the MissingScirpt script
Problem: I used prefabs from other places, but I dropped their scripts (their scripts are not suitable for myself). When instantiating this Prefab, I will print a lot of warning messages under the Editor. This message will affect the loading efficiency. Slow loading speed, and even cause a crash (in the packaged out to load Bundle through UnityWebRequest no problem, instant crash when instantiation), some students will say, I manually deleted it, but the prefab of productization is often relatively large, The structure is relatively complex, and there may be hundreds or thousands of nodes that lose scripts.
Requirement: Clean up all missing scripts in the prefab in the specified directory (recursive cleanup, including hidden nodes, clean up and write back to prefab)
Establish a test environment:
The first step is to create an empty scene, add the root node G1, add two child nodes Child1 Child2 and attach a new C# script NewBehaviourScript.cs, then hide Child1 and store G1 as Prefab
The second step is to delete NewBehaviourScript.cs Apply Prefab: G1
Ok, here is the environment, the following is the Editor code.
[MenuItem("Tools/clean up missing scripts in prefab")]
public static void RMLosedComponentInPrefab()
{
EditorUtility.DisplayProgressBar("Modify Prefab", "Please wait...", 0);
string[] ids = AssetDatabase.FindAssets("t:Prefab", new string[] { "Assets/data/testLoseScript" });
for (int i = 0; i < ids.Length; i++)
{
string path = AssetDatabase.GUIDToAssetPath(ids[i]);
GameObject prefab = AssetDatabase.LoadAssetAtPath(path, typeof(GameObject)) as GameObject;
GameObject instance = PrefabUtility.InstantiatePrefab(prefab) as GameObject;
int delCount = 0;
RecersiceModifyGameObject(instance, ref delCount);
if (delCount > 0)
{
Debug.Log(instance.name + " remove losed:---- " + delCount);
// save
PrefabUtility.ReplacePrefab(instance, prefab, ReplacePrefabOptions.ConnectToPrefab);
}
UnityEngine.Object.DestroyImmediate(instance);
EditorUtility.DisplayProgressBar("Modify Prefab", "Please wait...", i / (float)ids.Length);
}
AssetDatabase.SaveAssets();
EditorUtility.ClearProgressBar();
}
private static void RecersiceModifyGameObject(GameObject obj, ref int delCount)
{
//find child
if (obj.transform.childCount > 0)
{
for (int i = 0; i < obj.transform.childCount; i++)
{
var _childObj = obj.transform.GetChild(i).gameObject;
// modify this GameObject
RecersiceModifyGameObject(_childObj, ref delCount);
}
}
// change instance
SerializedObject so = new SerializedObject(obj);
var soProperties = so.FindProperty("m_Component");
var components = obj.GetComponents<Component>();
int propertyIndex = 0;
int innerDelCount = 0;
foreach (var c in components)
{
if (c == null)
{
++delCount;
++innerDelCount;
soProperties.DeleteArrayElementAtIndex(propertyIndex);
}
++propertyIndex;
}
if (innerDelCount > 0)
so.ApplyModifiedProperties();
}
result:
After the cleanup is done like this
Join crontab to check the logs 3 months ago and delete them Command to delete the December index...
Some time ago the system was doing stress testing, resulting in a huge amount of log, almost every two hours hard disk (co 140G) explode log server, it is necessary to clean up the data write timing o...
Wang Po sells melons and brags about herself. The more the code is written, the more standard it is. Used to clean up elasticsearch data, take a look at your own records. Yield for the first time...
0 1 * * * /home/osoper/clean_caches_data.sh I forgot who copied it, there is no source for the time being! Just for recording, so that it can be quickly turned out later...
If you want to write to your hard drive in the Linux system, sometimes it's a memory space. This buffer is a memory space. If you want to write to the hard disk, you have saved the system, and the sys...
Generate all frameworks in the Products folder under the project directory, fully automated, freeing productivity...
Unity converts C# scripts to DLLs, Prefab and other files without missing reference methods The scripts that will be used in the Prefab and Scene files in unity are assigned a guiid and fileID to stor...
Problem: After unity manually deletes the prefab, it packs Assetbundle for the prefab with the deleted prefab, and unity does not report an error Crash. To solve the crash check code is as follows: &n...