Unity编辑器扩展——PrefabUtility

Posted Hello Bug.

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity编辑器扩展——PrefabUtility相关的知识,希望对你有一定的参考价值。

一:前言

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();
    

以上是关于Unity编辑器扩展——PrefabUtility的主要内容,如果未能解决你的问题,请参考以下文章

Unity在编辑器模式下修改预制体

Unity插件开发:PrefabUtility--Prefab实例引用断开和替换

Unity2018获取对应预制体并SetDirty

Unity3D编辑器扩展——扩展自己的组件

Unity 一键制作预制体,一键修改预制体

Unity编辑器扩展