当前位置:网站首页>Unity 从Inspector界面打开资源管理器选择并记录文件路径
Unity 从Inspector界面打开资源管理器选择并记录文件路径
2022-07-04 07:13:00 【unity工具人】
在AvProVideo插件中有个功能,点击视频组件中的按钮可以打开资源管理器窗口,找到目标视频资源并记录下资源路径。
查了一下实现起来并不难,主要有两点
一个是unity 自带的用于打开资源管理器的方法
EditorUtility.OpenFilePanel();
第二个就是自定义编辑器的操作
实现过程
脚本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();
}
}
脚本2:操作类
using UnityEditor;
using UnityEngine;
public class ObjectBuilderScript : MonoBehaviour
{
public string path;
public void BuildObject()
{
Example.Open(gameObject);
}
}
脚本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();
}
}
}
可以看到我们定义了一个按钮
弹窗
打开资源管理器
选择文件后可以看到路径被保存下来了

参考文章:https://blog.csdn.net/qq_42139931/article/details/123206376
参考文章:https://wenku.baidu.com/view/1b41bac9561810a6f524ccbff121dd36a32dc422.html
边栏推荐
- The important role of host reinforcement concept in medical industry
- MySQL storage engine
- Transition technology from IPv4 to IPv6
- 用于压缩视频感知增强的多目标网络自适应时空融合
- Mobile adaptation: vw/vh
- Centos8 install mysql 7 unable to start up
- 【FreeRTOS】FreeRTOS學習筆記(7)— 手寫FreeRTOS雙向鏈錶/源碼分析
- 高薪程序员&面试题精讲系列119之Redis如何实现分布式锁?
- The difference between synchronized and lock
- Zabbix agent主动模式的实现
猜你喜欢

Introduction to deep learning Ann neural network parameter optimization problem (SGD, momentum, adagrad, rmsprop, Adam)

The final week, I split

leetcode825. 适龄的朋友

The most effective futures trend strategy: futures reverse merchandising

【Kubernetes系列】Kubernetes 上安装 KubeSphere

Industrial computer anti-virus

Four sets of APIs for queues

Selenium ide plug-in download, installation and use tutorial

Bottom problem of figure

Deep understanding of redis -- a new type of bitmap / hyperloglgo / Geo
随机推荐
Splicing plain text into JSON strings - easy language method
The final week, I split
Finishing (III) - Exercise 2
果果带你写链表,小学生看了都说好
Tar source code analysis Part 10
Responsive - media query
Boast about Devops
Introduction to rce in attack and defense world
Tar source code analysis 4
BasicVSR++: Improving Video Super-Resolutionwith Enhanced Propagation and Alignment
Design of test cases
[Flink] temporal semantics and watermark
【FPGA教程案例8】基于verilog的分频器设计与实现
notepad++如何统计单词数量
What is the use of cloud redis? How to use cloud redis?
[GF (q) + LDPC] regular LDPC coding and decoding design and MATLAB simulation based on the GF (q) field of binary graph
Pangu open source: multi support and promotion, the wave of chip industry
MySQL 45 learning notes (XI) how to index string fields
win10微软拼音输入法输入文字时候下方不出现中文提示
[Mori city] random talk on GIS data (I)