Created
July 22, 2019 09:53
-
-
Save bartwttewaall/7918859646b1ee90f221806b71221a23 to your computer and use it in GitHub Desktop.
Build script to strip out Colliders from a scene, used in a project where all static colliders we exported as a json file and were not needed within the scene at runtime which made it more performant.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if UNITY_EDITOR | |
/** | |
This script should be located in an Editor folder | |
No need to try assigning it to a GameObject | |
It will run automatically when playing from the editor or during a build | |
see: https://docs.unity3d.com/ScriptReference/Build.IProcessSceneWithReport.OnProcessScene.html | |
*/ | |
using System; | |
using System.Text; | |
using UnityEditor.Build; | |
using UnityEditor.Build.Reporting; | |
using UnityEngine; | |
public class CleanupSceneBuildProcessor : IProcessSceneWithReport { | |
// set to false to disable this script | |
bool processScenes = true; | |
bool VERBOSE = true; | |
StringBuilder logBuilder; | |
string[] includeSceneNames = new string[] { | |
"Level1", | |
"Level2", | |
"Level3" | |
}; | |
string[] excludeGameObjectNames = new string[] { | |
"Ground" | |
}; | |
public int callbackOrder { get { return 0; } } | |
public void OnProcessScene (UnityEngine.SceneManagement.Scene scene, BuildReport report) { | |
if (!processScenes) return; | |
// process whitelisted scenes | |
if (Array.IndexOf (includeSceneNames, scene.name) > -1) { | |
if (VERBOSE) logBuilder = new StringBuilder ("MapBuildProcessor.OnProcessScene " + scene.name).AppendLine (); | |
var rootObjects = scene.GetRootGameObjects (); | |
Array.ForEach (rootObjects, HandleCollidersInChildren); | |
} | |
// output log result, if available | |
if (logBuilder != null) { | |
Debug.Log (logBuilder.ToString ()); | |
logBuilder.Clear (); | |
} | |
} | |
void HandleCollidersInChildren (GameObject obj) { | |
var colliders = obj.GetComponentsInChildren<Collider> (); | |
Array.ForEach (colliders, (collider) => { | |
if (HasTransformAndColliderOnly (collider.gameObject)) { | |
if (VERBOSE) logBuilder.Append (" > Destroyed GameObject " + collider.gameObject.name + " from parent: " + collider.transform.parent.name).AppendLine (); | |
GameObject.DestroyImmediate (collider.gameObject); | |
} else if (Array.IndexOf (excludeGameObjectNames, collider.gameObject.name) == -1) { | |
if (VERBOSE) logBuilder.Append (" > Disabled Collider Component: " + collider.gameObject.name).AppendLine (); | |
collider.enabled = false; | |
} | |
}); | |
} | |
// custom rule: remove nested gameobjects that only contain a collider script | |
bool HasTransformAndColliderOnly (GameObject gameObject) { | |
return ( | |
gameObject.GetComponents<Component> ().Length == 2 && | |
gameObject.GetComponent<Transform> () != null && | |
gameObject.GetComponent<Collider> () != null | |
); | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment