Unity自定义工具之引用查找

目录

前言:

准备

知识点

完整代码

效果


前言:

我们在游戏开发过程中,有时会需要查找一个资源的所有引用的地方,比如查找一个小prefab在那些大prefabs中使用,或者某个建筑物在那些场景中使用,这时就需要一个查找工具供我们快速高效的找到这些引用关系。


准备

using System;
using System.Runtime.InteropServices;//调用外部库,需要引用命名空间

/// <summary>
/// 为了调用外部库脚本
/// </summary>
public class EditorMessage
{
    [DllImport("User32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
    public static extern int MessageBox(IntPtr handle, String message, String title, int type);//具体方法
}

一个提示窗口

知识点

1.Selection.assetGUIDs 所有选中物体的GUID
2.AssetDatabase.GUIDToAssetPath 利用GUID得到路径
3.EditorSettings.serializationMode = SerializationMode.ForceText
此选项可将所有资源转换为文本模式,并且 Unity 对新资源使用文本模式。这是默认选项
拓展:
        Mixed  :二进制模式下的资源保持二进制模式,文本模式下的资源保持文本模式。默认情况下,Unity 对新资源使用二进制模式。
        Force Binary : 此选项可将所有资源转换为二进制模式,并且 Unity 对新资源使用二进制模式。

4.Directory.GetFiles("Assets", "*.*", SearchOption.AllDirectories).Where(s => s.EndsWith(".prefab") || s.EndsWith(".unity"))
筛选Assets路径下所有的prefab和场景

5.EditorApplication.update 在编辑器模式下每帧执行

6.Regex.IsMatch 正则匹配

7.EditorUtility.DisplayProgressBar("Loading", "打印中......", slider) 进度条

完整代码

public class FindObjectReference {
    [MenuItem("Assets/FindAllReference", priority = 12)]
    public static void FindObjectAllReference() {
        string[] selectUid = Selection.assetGUIDs;
        if(selectUid.Length > 1) {
            EditorMessage.MessageBox(IntPtr.Zero, "目前只支持单次查找一个物体的所有引用", "确认", 0);
            return;
        }
        string path = AssetDatabase.GUIDToAssetPath(selectUid[0]);
        if(Directory.Exists(path)) {
            EditorMessage.MessageBox(IntPtr.Zero, "目前不支持文件夹引用的查找", "确认", 0);
            return;
        } else if(File.Exists(path)) {
            if(EditorSettings.serializationMode != SerializationMode.ForceText)
                EditorSettings.serializationMode = SerializationMode.ForceText;
            string[] allPath =  Directory.GetFiles("Assets", "*.*", SearchOption.AllDirectories).Where(s => s.EndsWith(".prefab") || s.EndsWith(".unity")).ToArray();

            int start = 0;
            int max = allPath.Length;
            EditorApplication.update = () => {
                string content =  File.ReadAllText(allPath[start]);
                if(Regex.IsMatch(content, selectUid[0])) {
                    Debug.Log(allPath[start]);
                }
                start++;
                float slider = (float)start / max;
                //参数1 为标题,参数2为提示,参数3为 进度百分比 0~1 之间
                EditorUtility.DisplayProgressBar("Loading", "打印中......", slider);
                if(start >= max) {
                    EditorMessage.MessageBox(IntPtr.Zero, "打印完成", "确认", 0);
                    EditorUtility.ClearProgressBar();
                    EditorApplication.update = null;
                }
            };
        }
    }
}

效果

 

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Unity中的自定义弹框可以使用Unity3D Custom Popup插件来实现。该插件可以推迟构造弹出窗口的显示,对于构造弹出窗口相对昂贵的情况非常有用。你可以通过执行`yarn add rotorz/unity3d-custom-popup`命令来安装该软件包,并将其与Unity工具兼容。然后按照插件的说明进行操作,将自定义弹出控件添加到Unity编辑器界面中。 具体而言,你可以在代码中调用`MyPermissionDialog.Builder`来创建一个自定义弹框。可以使用`setPositiveButton`方法设置弹框的确认按钮的点击事件,并在点击事件中执行相应的操作。通过`setTitleAndContext`方法可以设置弹框的标题和内容。最后,通过调用`create`方法创建弹框,并使用`show`方法显示出来。 请注意,这只是一种实现自定义弹框的方式,根据你的具体需求,可能还有其他的方法可供选择。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Android自定义弹框以及弹框资源加载](https://blog.csdn.net/u014146238/article/details/104824842)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [unity3d-custom-popup:Unity编辑器界面的自定义弹出控件,将弹出菜单的构造推迟到显示为止。 这对于弹出...](https://download.csdn.net/download/weixin_42131443/18312293)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

一梭键盘任平生

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

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

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

打赏作者

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

抵扣说明:

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

余额充值