概要
プレハブモードで表示した Prefab の Prefab名をボタンで表示し、クリックするとことで再度プレハブモードで Prefab を表示する Editor 拡張です。
詳細
プレハブモード関連のコールバックに、PrefabStage.prefabStageOpened というものがあり、このコールバックに履歴の更新、window の更新を設定します。
コード全文
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
public class PrefabHistoryWindow : EditorWindow
{
private static PrefabHistoryWindow window;
[MenuItem( "Window/Prefab History" )]
private static void Open()
{
window = GetWindow<PrefabHistoryWindow>();
}
private static void AddHistory( PrefabStage prefabStage )
{
if( window == null )
{
Open();
}
var guid = AssetDatabase.AssetPathToGUID( prefabStage.assetPath );
var history = window._histories.FirstOrDefault( h => h.guid == guid );
if( history != default )
{
window._histories.Remove( history );
}
window._histories.Add( new( Path.GetFileName( prefabStage.assetPath ), guid ) );
window.Repaint();
}
[SerializeField]
private List<(string name, string guid)> _histories = new();
private Vector2 _scrollPosition;
private void OnEnable()
{
PrefabStage.prefabStageOpened += AddHistory;
}
private void OnGUI()
{
_scrollPosition = EditorGUILayout.BeginScrollView( _scrollPosition );
foreach( var history in _histories.AsEnumerable().Reverse() )
{
using( new EditorGUILayout.HorizontalScope() )
{
var asset = AssetDatabase.LoadAssetAtPath( AssetDatabase.GUIDToAssetPath( history.guid ), typeof( GameObject ) );
if( asset == null )
{
continue;
}
if( GUILayout.Button( history.name, GUILayout.ExpandWidth( true ) ) )
{
AssetDatabase.OpenAsset( asset );
}
}
}
EditorGUILayout.EndScrollView();
}
private void OnDestroy()
{
_histories.Clear();
PrefabStage.prefabStageOpened -= AddHistory;
window = null;
}
}
リファレンス