Unity Editor下 修改 Prefab

本文介绍如何在Unity编辑器中批量为所有Prefab添加PrefabInfoHelper组件。通过MenuItem函数`AddPrefabInfoHelper`,遍历选择的深资产对象,检查是否为Prefab,然后实例化Prefab,添加PrefabInfoHelper组件,设置相关信息,并使用PrefabUtility替换Prefab,最后销毁临时实例。
摘要由CSDN通过智能技术生成
[MenuItem("所有Prefab添加PrefabInfoHerlper组件")]
    static void AddPrefabInfoHelper()
    {
        UnityEngine.Object[] selObjs = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.DeepAssets);
        for (int i = 0; i < selObjs.Length; i++)
        {
            Object selObj = selObjs[i];
            string selfPath = AssetDatabase.GetAssetPath(selObj);
            if (selfPath.EndsWith(".prefab"))
            {
                string[] dependPaths = AssetDatabase.GetDependencies(selfPath);
                GameObject go = GameObject.Instantiate(selObj) as GameObject;
                
                PrefabInfoHelper pih = go.GetComponent<PrefabInfoHelper>();
                if (pih == null)
                {
                    pih = go.AddComponent<PrefabInfoHelper>();
                }
                pih.isInstanced = false;
                pih.selfPath 
以下是一段Unity Editor代码,可以找到所有带有指定脚本的预制体。您可以将此脚本附加到Editor文件夹中的任何脚本中,然后在Unity编辑器中单击该脚本的“查找预制体”按钮,以查找所有带有该脚本的预制体: ```c# using UnityEngine; using UnityEditor; public class FindPrefabsWithScript : EditorWindow { private MonoScript targetScript; private GameObject[] prefabList; private Vector2 scrollPosition; [MenuItem("Window/Find Prefabs With Script")] public static void ShowWindow() { GetWindow<FindPrefabsWithScript>("Find Prefabs With Script"); } private void OnGUI() { EditorGUILayout.HelpBox("Select a script to find all prefabs that contain it.", MessageType.Info); targetScript = EditorGUILayout.ObjectField("Target Script", targetScript, typeof(MonoScript), false) as MonoScript; GUI.enabled = targetScript != null; if (GUILayout.Button("Find Prefabs")) { FindPrefabs(); } GUI.enabled = true; if (prefabList != null) { EditorGUILayout.LabelField("Prefabs Found: " + prefabList.Length); if (prefabList.Length > 0) { scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition); foreach (GameObject prefab in prefabList) { EditorGUILayout.ObjectField(prefab, typeof(GameObject), false); } EditorGUILayout.EndScrollView(); } } } private void FindPrefabs() { string[] guids = AssetDatabase.FindAssets("t:Prefab"); prefabList = new GameObject[0]; foreach (string guid in guids) { string assetPath = AssetDatabase.GUIDToAssetPath(guid); GameObject asset = AssetDatabase.LoadAssetAtPath(assetPath, typeof(GameObject)) as GameObject; if (asset != null && PrefabUtility.GetPrefabAssetType(asset) == PrefabAssetType.Regular) { Component[] components = asset.GetComponentsInChildren<Component>(true); foreach (Component component in components) { if (component.GetType() == targetScript.GetClass()) { ArrayUtility.Add(ref prefabList, asset); break; } } } } Repaint(); } } ``` 该脚本将创建一个新的窗口,您可以在其中选择要查找的脚本。单击“Find Prefabs”按钮后,它将搜索所有预制体,找到带有指定脚本的所有预制体,并将它们列出在窗口中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值