popcorn/Scripts/Utilities/Swapper/PrefabInPrefabSwapper.cs

53 lines
1.6 KiB
C#

using UnityEngine;
#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using UnityEditor.Experimental.SceneManagement;
#endif
public sealed class PrefabInPrefabSwapper : MonoBehaviour {
#if UNITY_EDITOR
[SerializableAttribute]
public sealed class SwapInfo {
[SerializeField]
private string swapTargetName = default;
public string SwapTargetName {
get{ return swapTargetName; }
}
[SerializeField]
private GameObject swapPrefab = default;
public GameObject SwapPrefab {
get{ return swapPrefab; }
}
}
[SerializeField]
private SwapInfo[] swapInfoArray = new SwapInfo[1];
[ContextMenu("SwapPrefab")]
private void SwapPrefab(){
foreach(var info in swapInfoArray){
if(info.SwapTargetName.Length <= 0) continue;
if(info.SwapPrefab == null) continue;
var swapList = new List<Transform>();
transform.AllChildrens(t => {
if(t.name.Contains(info.SwapTargetName)){
swapList.Add(t);
}
});
foreach(var from in swapList){
var to = (UnityEditor.PrefabUtility.InstantiatePrefab(info.SwapPrefab, PrefabStageUtility.GetCurrentPrefabStage().scene) as GameObject).transform;
to.parent = from.parent;
to.localPosition = from.localPosition;
to.localRotation = from.localRotation;
to.localScale = from.localScale;
DestroyImmediate(from.gameObject);
}
}
}
#endif
}