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

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hello Bug.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值