• Unity 从Inspector界面打开资源管理器选择并记录文件路径


    在AvProVideo插件中有个功能,点击视频组件中的按钮可以打开资源管理器窗口,找到目标视频资源并记录下资源路径。
    查了一下实现起来并不难,主要有两点
    一个是unity 自带的用于打开资源管理器的方法

    EditorUtility.OpenFilePanel();
    
    • 1

    第二个就是自定义编辑器的操作

    实现过程
    脚本1:定义窗口、打开资源管理器

    using UnityEngine;
    using UnityEditor;
    
    public class Example : EditorWindow
    {
        static GameObject _objectBuilderScript;
        //[MenuItem("SKFramework/Example")]
        public static void Open(GameObject objectBuilderScript)
        {
            GetWindow<Example>().Show();
            _objectBuilderScript = objectBuilderScript;
        }
    
        
        private string path;
    
        private void OnGUI()
        {
            //水平布局
            GUILayout.BeginHorizontal();
            {
                GUILayout.Label("路径", GUILayout.Width(50f));
                path = GUILayout.TextField(path);
                if (GUILayout.Button("浏览", GUILayout.Width(50f)))
                {
                	//不用上边这个弹窗也可以
                    path = EditorUtility.OpenFilePanel("成功啦 ୧☉□☉୨", Application.dataPath, "png");
                    _objectBuilderScript.GetComponent<ObjectBuilderScript>().path = path;
                }
            }
            GUILayout.EndHorizontal();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    脚本2:操作类

    using UnityEditor;
    using UnityEngine;
    public class ObjectBuilderScript : MonoBehaviour
    {
        public  string path;
        
        public void BuildObject()
        {
            Example.Open(gameObject);
        }   
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    脚本3:重写操作类
    这个脚本要放在 Assets/Editor 文件夹下

    using UnityEngine;
    using UnityEditor;
    using System;
    [CustomEditor(typeof(ObjectBuilderScript))]
    public class ObjectBuilderEditor : Editor
    {
        public override void OnInspectorGUI()
        {
            string path;
            string FilePath = Environment.CurrentDirectory;
            DrawDefaultInspector();
            ObjectBuilderScript myScript = (ObjectBuilderScript)target;
            if (GUILayout.Button("选择图片资源"))
            {            
    
                myScript.BuildObject();
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    可以看到我们定义了一个按钮
    在这里插入图片描述
    弹窗
    在这里插入图片描述
    打开资源管理器
    在这里插入图片描述

    选择文件后可以看到路径被保存下来了
    在这里插入图片描述

    在这里插入图片描述
    参考文章:https://blog.csdn.net/qq_42139931/article/details/123206376
    参考文章:https://wenku.baidu.com/view/1b41bac9561810a6f524ccbff121dd36a32dc422.html

  • 相关阅读:
    2022年武汉市两化融合贯标补贴政策详情!
    22071.11.20作业
    npm/yarm常用命令
    python日期计算器 青少年编程电子学会python编程等级考试二级真题解析2021年12月
    pollLast() 和poll啥区别
    java计算机毕业设计线上文具销售系统源码+数据库+系统+lw文档+mybatis+运行部署
    【Detectron2】代码库学习-2. 环境搭建和demo
    你不知道的Spring的依赖的查找和注入的来源
    怎么把录音转文字?这些方法值得收藏
    交互式电子表格Baserow
  • 原文地址:https://blog.csdn.net/weixin_45023328/article/details/125545673