はじめに
今回はUnityEditor.SceneManagement.PrefabStageUtilityを用いて、Prefab Modeでアセットを開いたり、Prefab stageを取得したりする記事になります。

概要
PrefabStageUtilityはPrefabStageに関するUtility群が定義されており、具体的には以下の3つのメソッドがあります。
- PrefabStageUtility.GetCurrentPrefabStage : Prefabモードで開いている
PrefabStageを取得する - PrefabStageUtility.GetPrefabStage : 指定した
GameObjectを含むPrefabStageを取得する - PrefabStageUtility.OpenPrefab : 指定した
Prefab AssetをPrefabモードで開く
またPrefabStageってなんやねんとなる方もいらっしゃると思うますので、説明をのっけておきます。
The PrefabStage class represents an editing context for Prefab Assets.
A stage is an editing context which includes a collection of Scenes. The main stage contains all the currently open regular Scenes, while a Prefab stage contains a preview Scene used solely for editing the Prefab in.
The breadcrumbs which are shown in the Scene view when in Prefab Mode each represent a stage. Those with a Prefab icon represent Prefab stages.
PrefabStageクラスはPrefab Assetsの編集コンテキストを表します。
ステージはSceneのコレクションを含む編集コンテキストです。メインステージは現在開いている全ての通常シーンを含み、プレファブステージはプレファブの編集にのみ使用されるプレビューシーンを含みます。
プレハブモードの時にシーンビューに表示されるパンくずは、それぞれステージを表しています。プレハブアイコンがあるものはプレハブステージです。
私の認識だとPrefabモードで開いているPrefabの情報がPrefabStageに格納されているイメージ(Prefabと一対一で紐づいている感じ?要調査)です。
PrefabStageUtility.GetCurrentPrefabStage
現在Prefabモードで開いているPrefab Stageを取得します。またPrefabモードで何も開かれていなければnullが返ってきます。
public static SceneManagement.PrefabStage GetCurrentPrefabStage ();
SceneManagement.PrefabStageUtility-GetCurrentPrefabStage - Unity スクリプトリファレンス
// サンプル [MenuItem("Sample/Execute")] private static void Execute() { // Prefabモードで開いているPrefab Stageを取得(開いてなければnull) var stage = PrefabStageUtility.GetCurrentPrefabStage(); // Assetのパスを表示する // 例. Assets/Prefabs/Sphere.prefab Debug.Log(stage.assetPath); }

PrefabStageUtility.GetPrefabStage
引数で渡したGameObjectを含むPrefab Stageを取得します。
public static SceneManagement.PrefabStage GetPrefabStage (GameObject gameObject);
SceneManagement.PrefabStageUtility-GetPrefabStage - Unity スクリプトリファレンス
PrefabStageUtility.OpenPrefab
指定したPrefab AssetをPrefabモードで開くことができます。これは今まで紹介してきた中で一番使えるかもしれません。
Opens a Prefab Asset in Prefab Mode.
After opening Prefab Mode you can return to the main scenes by using StageUtility.GoToMainStage. See also: Editing in Prefab Mode.
// DeepL翻訳
プリファブアセットをプリファブモードで開きます。Prefab Modeを開いた後、StageUtility.GoToMainStageを使ってメインシーンに戻ることができます。こちらも参照してください:プレハブモードでの編集
public static SceneManagement.PrefabStage OpenPrefab (string prefabAssetPath); public static SceneManagement.PrefabStage OpenPrefab (string prefabAssetPath, GameObject openedFromInstance); public static SceneManagement.PrefabStage OpenPrefab (string prefabAssetPath, GameObject openedFromInstance, SceneManagement.PrefabStage.Mode prefabStageMode);
// サンプル [MenuItem("Sample/Execute")] private static void Execute() { const string prefabAssetPath = "Assets/Prefabs/Cube.prefab"; // 指定したPrefab AssetをPrefabモードで開く var stage = PrefabStageUtility.OpenPrefab(prefabAssetPath); }

