Files
WordConnect/Assets/WordConnectGameToolkit/Scripts/Editor/PopupPreview.cs
2025-10-17 09:14:59 +08:00

94 lines
2.5 KiB
C#

using UnityEditor;
using UnityEngine;
using System.IO;
using System.Linq;
public class PopupPreview : EditorWindow
{
private GameObject[] prefabs;
private int currentIndex = 0;
private GameObject currentInstance;
[MenuItem("Tools/Prefab Batch Viewer")]
public static void ShowWindow()
{
GetWindow<PopupPreview>("Prefab Batch Viewer");
}
private void OnEnable()
{
string path = "Assets/WordConnectGameToolkit/Resources/Popups";
string[] prefabPaths = Directory.GetFiles(path, "*.prefab", SearchOption.AllDirectories);
prefabs = prefabPaths
.Select(p => AssetDatabase.LoadAssetAtPath<GameObject>(p))
.Where(p => p != null)
.ToArray();
}
private void OnGUI()
{
if (prefabs == null || prefabs.Length == 0)
{
EditorGUILayout.LabelField("No prefabs found in Resources/Popups.");
return;
}
EditorGUILayout.LabelField($"Prefab {currentIndex + 1}/{prefabs.Length}", EditorStyles.boldLabel);
EditorGUILayout.ObjectField("Current Prefab", prefabs[currentIndex], typeof(GameObject), false);
GUILayout.Space(10);
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("Previous"))
{
ShowPreviousPrefab();
}
if (GUILayout.Button("Next"))
{
ShowNextPrefab();
}
EditorGUILayout.EndHorizontal();
GUILayout.Space(10);
if (currentInstance == null)
{
if (GUILayout.Button("Instantiate in Scene"))
{
InstantiateCurrentPrefab();
}
}
else
{
if (GUILayout.Button("Remove Instance"))
{
DestroyImmediate(currentInstance);
}
}
}
private void ShowPreviousPrefab()
{
currentIndex = (currentIndex - 1 + prefabs.Length) % prefabs.Length;
InstantiateCurrentPrefab();
}
private void ShowNextPrefab()
{
currentIndex = (currentIndex + 1) % prefabs.Length;
InstantiateCurrentPrefab();
}
private void InstantiateCurrentPrefab()
{
if (currentInstance != null)
{
DestroyImmediate(currentInstance);
}
currentInstance = (GameObject)PrefabUtility.InstantiatePrefab(prefabs[currentIndex]);
currentInstance.name = prefabs[currentIndex].name + "_Preview";
Selection.activeGameObject = currentInstance;
}
}