Files
bansonic_beta_main/Assets/Editor/MoveMusicPicToPrefab.cs
T

78 lines
2.6 KiB
C#

using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;
public static class MoveMusicPicToPrefab
{
const string ScenePath = "Assets/Scenes/UI_UI.unity";
const string PrefabPath = "Assets/Scripts/UI/Panel/dontdestroyonload/btm and top.prefab";
const string MusicPicName = "MusicPic";
[MenuItem("Tools/MusicPic/Move To btm and top Prefab")]
public static void Execute()
{
var scene = EditorSceneManager.OpenScene(ScenePath, OpenSceneMode.Single);
if (!scene.IsValid())
{
Debug.LogError("[MoveMusicPicToPrefab] Failed to open UI_UI scene.");
return;
}
var musicPic = FindInScene(scene, MusicPicName);
if (musicPic == null)
{
Debug.LogError("[MoveMusicPicToPrefab] MusicPic not found in UI_UI scene.");
return;
}
var prefabRoot = PrefabUtility.LoadPrefabContents(PrefabPath);
if (prefabRoot == null)
{
Debug.LogError("[MoveMusicPicToPrefab] Failed to load prefab.");
return;
}
var existing = prefabRoot.transform.Find(MusicPicName);
if (existing != null)
{
Object.DestroyImmediate(existing.gameObject);
}
var copy = Object.Instantiate(musicPic);
copy.name = MusicPicName;
copy.transform.SetParent(prefabRoot.transform, false);
copy.transform.localPosition = Vector3.zero;
copy.transform.localRotation = Quaternion.identity;
copy.transform.localScale = Vector3.one;
PrefabUtility.SaveAsPrefabAsset(prefabRoot, PrefabPath);
PrefabUtility.UnloadPrefabContents(prefabRoot);
Object.DestroyImmediate(musicPic);
EditorSceneManager.MarkSceneDirty(scene);
EditorSceneManager.SaveScene(scene);
Debug.Log("[MoveMusicPicToPrefab] Moved MusicPic into btm and top prefab and removed from UI_UI scene.");
}
static GameObject FindInScene(Scene scene, string name)
{
var roots = scene.GetRootGameObjects();
foreach (var root in roots)
{
if (root == null) continue;
var t = root.transform.Find(name);
if (t != null) return t.gameObject;
}
var all = Object.FindObjectsByType<Transform>(FindObjectsInactive.Include, FindObjectsSortMode.None);
foreach (var t in all)
{
if (t == null) continue;
if (t.gameObject.scene != scene) continue;
if (t.name == name) return t.gameObject;
}
return null;
}
}