/// /// MIT License - Copyright(c) 2019 Ugo Belfiore /// using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; /// /// Remove Components marked as EditorOnly, and execute actions when you building your game /// ExecuteBuildCleaning is called thanks to BuildCallBack(). /// This function will also get called when entering Playmode, when SceneManager.LoadScene is called. /// if you want to desactivate this behavior, set PlayModeCleaning to false /// public class EditorOnlyCleaning : MonoBehaviour { public bool PlayModeCleaning = true; //active the cleaning when entering in play mode public bool BuildCleaning = true; //active the cleaning when building //all action you want to do public List AllActions = new List(); //all Component you want to delete public List AllBehaviorToDeleteFromBuild = new List(); /// /// - Call all action /// - Delete all behavior /// - Delete itself /// public void ExecuteBuildCleaning() { if (!BuildCleaning || (Application.isPlaying && !PlayModeCleaning)) { return; } ExecuteUnityEventAction(); RemoveAllComponents(); RemoveItself(); } /// /// execute all UnityEvent action /// We theoricly could have only one UnityEvent with multiple action, /// but then we can't prediect the order of actions. /// Here with a list of UnityEvent, we manage the order ourself. /// private void ExecuteUnityEventAction() { for (int i = 0; i < AllActions.Count; i++) { AllActions[i].Invoke(); } } /// /// Delete every components in the list /// private void RemoveAllComponents() { for (int i = 0; i < AllBehaviorToDeleteFromBuild.Count; i++) { if (AllBehaviorToDeleteFromBuild[i] == null) { continue; } if (Application.isPlaying) { Destroy(AllBehaviorToDeleteFromBuild[i]); } else { DestroyImmediate(AllBehaviorToDeleteFromBuild[i]); } } } /// /// Remove the script component this /// private void RemoveItself() { if (Application.isPlaying) { Destroy(this); } else { DestroyImmediate(this); } } }