0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Unity】プレハブモードで表示した Prefab の履歴を管理する Editor 拡張

Posted at

概要

プレハブモードで表示した Prefab の Prefab名をボタンで表示し、クリックするとことで再度プレハブモードで Prefab を表示する Editor 拡張です。
output.gif

詳細

プレハブモード関連のコールバックに、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;
    }
}

リファレンス

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?