using UnityEditor; using UnityEngine; using System.Collections.Generic; public class PrefabManager : EditorWindow { private GameObject selectedPrefab; private GameObject prefabInstance; private Vector2 scrollPosition; private static Dictionary prefabReferences = new Dictionary(); [MenuItem("Tools/Prefab Manager")] public static void ShowWindow() { GetWindow("预制体管理器"); } private void OnGUI() { GUILayout.Label("预制体加载与管理", EditorStyles.boldLabel); // 选择预制体 EditorGUILayout.Space(); selectedPrefab = (GameObject)EditorGUILayout.ObjectField("选择预制体", selectedPrefab, typeof(GameObject), false); EditorGUILayout.Space(); if (GUILayout.Button("加载预制体")) { LoadPrefab(); } EditorGUILayout.Space(); if (prefabInstance != null) { GUILayout.Label("当前实例: " + prefabInstance.name); EditorGUILayout.Space(); if (GUILayout.Button("断开连接")) { BreakPrefabConnection(); } EditorGUILayout.Space(); if (GUILayout.Button("应用更改到预制体")) { ApplyChangesToPrefab(); } } // 显示已保存的预制体引用 EditorGUILayout.Space(); GUILayout.Label("已保存的预制体引用:", EditorStyles.boldLabel); scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition); foreach (var pair in prefabReferences) { EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField(pair.Key, GUILayout.Width(150)); if (pair.Value != null) { EditorGUILayout.ObjectField(pair.Value, typeof(GameObject), false); if (GUILayout.Button("选择", GUILayout.Width(60))) { Selection.activeObject = pair.Value; } } else { EditorGUILayout.LabelField("引用丢失"); if (GUILayout.Button("移除", GUILayout.Width(60))) { prefabReferences.Remove(pair.Key); } } EditorGUILayout.EndHorizontal(); } EditorGUILayout.EndScrollView(); } private void LoadPrefab() { if (selectedPrefab == null) { EditorUtility.DisplayDialog("错误", "请先选择一个预制体", "确定"); return; } // 检查是否已存在实例 if (prefabInstance != null) { if (!EditorUtility.DisplayDialog("确认", "是否替换当前实例?", "是", "否")) { return; } DestroyImmediate(prefabInstance); } // 实例化预制体并保持连接 prefabInstance = (GameObject)PrefabUtility.InstantiatePrefab(selectedPrefab); prefabInstance.name = selectedPrefab.name; // 保存预制体引用 string referenceKey = selectedPrefab.name; if (!prefabReferences.ContainsKey(referenceKey)) { prefabReferences.Add(referenceKey, selectedPrefab); } else { prefabReferences[referenceKey] = selectedPrefab; } // 记录操作 Debug.Log("已加载预制体: " + selectedPrefab.name + " 并保持连接"); // 选中新实例 Selection.activeObject = prefabInstance; } private void BreakPrefabConnection() { if (prefabInstance != null && PrefabUtility.IsPartOfPrefabInstance(prefabInstance)) { PrefabUtility.UnpackPrefabInstance(prefabInstance, PrefabUnpackMode.Completely, InteractionMode.UserAction); Debug.Log("已断开与预制体的连接"); } } private void ApplyChangesToPrefab() { if (prefabInstance != null && PrefabUtility.IsPartOfPrefabInstance(prefabInstance)) { GameObject prefab = PrefabUtility.GetCorrespondingObjectFromSource(prefabInstance) as GameObject; if (prefab != null) { PrefabUtility.ApplyPrefabInstance(prefabInstance, InteractionMode.UserAction); Debug.Log("已将更改应用到预制体: " + prefab.name); } } } // 用于在其他脚本中获取保存的预制体引用 public static GameObject GetPrefabReference(string prefabName) { if (prefabReferences.ContainsKey(prefabName)) { return prefabReferences[prefabName]; } return null; } }