using UnityEngine; using UnityEditor; using UnityEditor.Build; using UnityEditor.Build.Reporting; using System.Collections.Generic; using System.Linq; public class BillboardBuildProcessor : IPreprocessBuildWithReport { // 构建设置的配置选项 public class BuildSettings { public static bool EnableBillboardProcessing { get { return EditorPrefs.GetBool("BillboardProcessor_Enable", true); } set { EditorPrefs.SetBool("BillboardProcessor_Enable", value); } } } // 添加菜单项来控制处理器 [MenuItem("Tools/Billboard/Toggle Build Processing")] static void ToggleBuildProcessing() { BuildSettings.EnableBillboardProcessing = !BuildSettings.EnableBillboardProcessing; Debug.Log($"Billboard Build Processing is now {(BuildSettings.EnableBillboardProcessing ? "enabled" : "disabled")}"); } // 添加禁用所有 BillboardTarget 的菜单项 [MenuItem("Tools/Billboard/Disable All BillboardTargets")] static void DisableAllBillboardTargets() { GameObject[] taggedObjects = GameObject.FindGameObjectsWithTag("BillboardTarget"); int count = 0; foreach (GameObject obj in taggedObjects) { if (obj != null && obj.activeSelf) { obj.SetActive(false); count++; } } Debug.Log($"Disabled {count} BillboardTarget objects"); } [MenuItem("Tools/Billboard/Enable All BillboardTargets")] static void EnableAllBillboardTargets() { // 获取所有场景 int sceneCount = UnityEngine.SceneManagement.SceneManager.sceneCount; HashSet taggedObjects = new HashSet(); for (int i = 0; i < sceneCount; i++) { var scene = UnityEngine.SceneManagement.SceneManager.GetSceneAt(i); if (!scene.isLoaded) continue; // 获取场景中的所有根物体 GameObject[] rootObjects = scene.GetRootGameObjects(); foreach (GameObject root in rootObjects) { // 获取包括未激活物体在内的所有子物体 Transform[] transforms = root.GetComponentsInChildren(true); foreach (Transform transform in transforms) { if (transform.gameObject.CompareTag("BillboardTarget")) { taggedObjects.Add(transform.gameObject); } } } } int count = 0; foreach (GameObject obj in taggedObjects) { if (obj != null && !obj.activeSelf) { obj.SetActive(true); count++; } } Debug.Log($"Found {taggedObjects.Count} BillboardTarget objects, Enabled {count} inactive objects"); } // 添加选择性启用/禁用的窗口 [MenuItem("Tools/Billboard/Show BillboardTarget Manager")] static void ShowBillboardManager() { BillboardManagerWindow.ShowWindow(); } #if UNITY_EDITOR [MenuItem("Tools/Billboard/Test Load Billboards")] static void TestLoadBillboards() { // 测试是否能找到所有预制体 var prefabs = Resources.LoadAll("Billboards"); Debug.Log($"Found {prefabs.Length} billboard prefabs"); foreach (var prefab in prefabs) { Debug.Log($"Found prefab: {prefab.name}"); } } #endif public int callbackOrder { get { return 0; } } public void OnPreprocessBuild(BuildReport report) { // if (!BuildSettings.EnableBillboardProcessing) // { // Debug.Log("Billboard Build Processing is disabled"); // return; // } // Debug.Log("Billboard Build Processor: Starting..."); // GameObject[] taggedObjects = GameObject.FindGameObjectsWithTag("BillboardTarget"); // foreach (GameObject obj in taggedObjects) // { // if (obj != null) // { // Debug.Log($"Disabling original object: {obj.name}"); // obj.SetActive(false); // } // } // Debug.Log("Billboard Build Processor: Completed"); } } // 添加一个管理窗口来单独控制每个物体 public class BillboardManagerWindow : EditorWindow { private Vector2 scrollPosition; private GameObject[] taggedObjects; [MenuItem("Tools/Billboard/Manager")] public static void ShowWindow() { GetWindow("Billboard Manager"); } void OnEnable() { RefreshObjectsList(); } void RefreshObjectsList() { HashSet foundObjects = new HashSet(); // 获取所有场景中的物体 int sceneCount = UnityEngine.SceneManagement.SceneManager.sceneCount; for (int i = 0; i < sceneCount; i++) { var scene = UnityEngine.SceneManagement.SceneManager.GetSceneAt(i); if (!scene.isLoaded) continue; GameObject[] rootObjects = scene.GetRootGameObjects(); foreach (GameObject root in rootObjects) { Transform[] transforms = root.GetComponentsInChildren(true); foreach (Transform transform in transforms) { if (transform.gameObject.CompareTag("BillboardTarget")) { foundObjects.Add(transform.gameObject); } } } } taggedObjects = foundObjects.ToArray(); Debug.Log($"Found {taggedObjects.Length} BillboardTarget objects (including inactive)"); } void OnGUI() { if (GUILayout.Button("刷新列表")) { RefreshObjectsList(); } EditorGUILayout.Space(); if (taggedObjects == null || taggedObjects.Length == 0) { EditorGUILayout.HelpBox("没有找到带有 BillboardTarget 标签的物体", MessageType.Info); return; } scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition); foreach (GameObject obj in taggedObjects) { if (obj != null) { EditorGUILayout.BeginHorizontal(); // 显示物体名称和启用状态 EditorGUI.BeginChangeCheck(); bool isEnabled = EditorGUILayout.Toggle(obj.activeSelf, GUILayout.Width(20)); if (EditorGUI.EndChangeCheck()) { obj.SetActive(isEnabled); } // 物体名称,点击可以选中物体 if (GUILayout.Button(obj.name, EditorStyles.label)) { Selection.activeGameObject = obj; } EditorGUILayout.EndHorizontal(); } } EditorGUILayout.EndScrollView(); } }