一:前言

PrefabUtility是一个静态类,用于Prefab的相关处理


二:代码实现

——SavePrefabAsset
保存一个预制体,只能保存一个工程目录下的预制体

using UnityEditor;
using UnityEngine;

public class Test : MonoBehaviour
{
[MenuItem("Tools/PrefabTest")]
private static void PrefabTest()
{
GameObject go = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/Prefabs/test.prefab");
PrefabUtility.SavePrefabAsset(go);
}
}

——SaveAsPrefabAsset
 保存成一个预制体,只能保存一个场景中的游戏物体

using UnityEditor;
using UnityEngine;

public class Test : MonoBehaviour
{
[MenuItem("Tools/PrefabTest")]
private static void PrefabTest()
{
string savePath = "Assets/Prefabs/go.prefab";
PrefabUtility.SaveAsPrefabAsset(Selection.activeGameObject, savePath);
}
}

——SaveAsPrefabAssetAndConnect
 保存成一个预制体并关联,只能保存一个场景中的游戏物体

using UnityEditor;
using UnityEngine;

public class Test : MonoBehaviour
{
[MenuItem("Tools/PrefabTest")]
private static void PrefabTest()
{
string savePath = "Assets/Prefabs/go.prefab";
PrefabUtility.SaveAsPrefabAssetAndConnect(Selection.activeGameObject, savePath, InteractionMode.AutomatedAction);
}
}

——GetPrefabAssetPathOfNearestInstanceRoot
得到此预制体的根预制体所在路径

using UnityEditor;
using UnityEngine;

public class Test : MonoBehaviour
{
[MenuItem("Tools/PrefabTest")]
private static void PrefabTest()
{
string str = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(Selection.activeObject);
Debug.Log("path:" + str);
}
}

——GetPrefabAssetType
得到预制体类型, NotAPrefab、Regular、Model、Variant、MissingAsset

using UnityEditor;
using UnityEngine;

public class Test : MonoBehaviour
{
[MenuItem("Tools/PrefabTest")]
private static void PrefabTest()
{
PrefabAssetType type = PrefabUtility.GetPrefabAssetType(Selection.activeObject);
Debug.Log("type:" + type);
}
}

——GetNearestPrefabInstanceRoot
得到此预制体的根预制体游戏物体,只能传入一个场景中的游戏物体

using UnityEditor;
using UnityEngine;

public class Test : MonoBehaviour
{
[MenuItem("Tools/PrefabTest")]
private static void PrefabTest()
{
GameObject go = PrefabUtility.GetNearestPrefabInstanceRoot(Selection.activeObject);
Debug.Log("GameObject:" + go);
}
}

——UnpackPrefabInstance
打断预制体关联,只能打断一个场景中的游戏物体

using UnityEditor;
using UnityEngine;

public class Test : MonoBehaviour
{
[MenuItem("Tools/PrefabTest")]
private static void PrefabTest()
{
PrefabUtility.UnpackPrefabInstance(Selection.activeGameObject, PrefabUnpackMode.Completely, InteractionMode.AutomatedAction);
Debug.Log("GameObject:" + go);
}
}

——prefabInstanceUpdated
预制体更新时的回调(Apply时) 

using UnityEditor;
using UnityEngine;

public class Test : MonoBehaviour
{
public int a;

[InitializeOnLoadMethod]
static void PrefabTest()
{
PrefabUtility.prefabInstanceUpdated = OnPrefabUpdate;
}

static void OnPrefabUpdate(GameObject instance)
{
Debug.Log(instance);
}
}

三:常用功能

——一键保存场景中所有预制体

using UnityEditor;
using UnityEngine;

public class Test : MonoBehaviour
{
[MenuItem("Tools/SaveAll")]
private static void SaveAll()
{
GameObject[] gos = FindObjectsOfType<GameObject>();
foreach (var temp in gos)
{
PrefabAssetType type = PrefabUtility.GetPrefabAssetType(temp);
if (type == PrefabAssetType.Regular)
{
GameObject rootGo = PrefabUtility.GetNearestPrefabInstanceRoot(temp);
string path = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(temp);
PrefabUtility.SaveAsPrefabAssetAndConnect(rootGo, path, InteractionMode.AutomatedAction);
}
}
AssetDatabase.Refresh();
}
}