赞
踩
首先提供官方参考文档:API-Reference-EditorWindow
概述: EditorWindow类继承自ScriptableObject类;我们可以通过继承这个类并重写来创建一个自定义的编辑器窗口,它可以随意停靠或放置在编辑器内任意位置,就像unity界面原生窗口一样;
static 属性
static 方法
EditorWindow.GetWindow<FirstEditorWindow>();
public 属性
public 方法
消息回调
static 属性
static 方法
public 属性
public 方法
消息回调
GUIContent:一种GUI元素,构造函数共包含三个参数,有多种构造函数类型;这是一个非常重要的类,几乎所有的控件都会用到它作为显示文字的参数;
GUIContent(string text,Texture image,string tooltip);
- class MyWindow : EditorWindow {
- [MenuItem ("Window/My Window")]
-
- public static void ShowWindow () {
- EditorWindow.GetWindow(typeof(MyWindow));
- }
-
- void OnGUI () {
- // The actual window code goes here
- }
- }
有时候,我们希望窗口能够和其它已有窗口绑定在一起,就需要在GetWindow()中传入参数;
窗口停靠时一般就两种方式,停靠在场景视图中,停靠在属性面板中,下面分别给出示例:
- //停靠在监视窗口
- FirstEditorWindow window = EditorWindow.GetWindow<FirstEditorWindow>(typeof(UnityEditor.InspectorWindow));
- //停靠在场景窗口
- FirstEditorWindow window = EditorWindow.GetWindow<FirstEditorWindow>(typeof(UnityEditor.SceneView));
如果需要保存一些配置文件,可以将EditorWindow和ScriptableObject配合使用;
首先定义一个ScriptableObject的配置类,然后在打开窗口时加载和保存数据即可;
- public class GameSettingsData : ScriptableObject
- {
- public bool isFPSOpen = true;
- }
-
- private void Awake()
- {
- gameSettings = AssetDatabase.LoadAssetAtPath<GameSettingsData>(dataRelativePth);
- //判断有没有配置表数据,如果没有,则创建;有,则加载
- //if (!File.Exists(Application.dataPath+dataRelativePth))
- if (!gameSettings)
- {
- gameSettings = ScriptableObject.CreateInstance<GameSettingsData>();
- AssetDatabase.CreateAsset(gameSettings,dataRelativePth);
- }
- Init();
-
- }
-
- private void Init() {
- isFPSOpen = gameSettings.isFPSOpen;
- }
-
通过获取当前窗口的Rect属性来设置手动设置控件的位置,当然:控件的Rect是相对于当前Window的;
Rect windowRect = this.position;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEditor;
- using System.Text.RegularExpressions;
- using System.IO;
-
- public class TextureSpitWindow : EditorWindow
- {
- // 设置窗口参数
- public Texture2D sourceTexture;
- public Texture2D maskTexture;
- public string sourcePath;
- public string sourceFolerPath; // 分割结果的存储路径
- public string retPath = ""; // 分割结果的存储路径
- // public List<Texture2D> allTextureList = new List<Texture2D>(); // 放置所有生成的texture
- Dictionary<byte, Texture2D> allTextureList = new Dictionary<byte, Texture2D>();
-
- [MenuItem ("Tools/Split Texture")]
- public static void ShowWindow () {
- EditorWindow thisWindow = EditorWindow.GetWindow(typeof(TextureSpitWindow));
- thisWindow.titleContent = new GUIContent("切割图片");
- thisWindow.position = new Rect(Screen.width/2, Screen.height/2, 600, 800);
- }
-
- void OnGUI()
- {
- EditorGUILayout.BeginHorizontal();
- EditorGUILayout.LabelField("选择切割图片");
- sourceTexture = EditorGUILayout.ObjectField(sourceTexture, typeof(Texture2D), true) as Texture2D;
- EditorGUILayout.EndHorizontal();
-
- EditorGUILayout.BeginHorizontal();
- EditorGUILayout.LabelField("选择遮罩图片");
- maskTexture = EditorGUILayout.ObjectField(maskTexture, typeof(Texture2D), true) as Texture2D;
- EditorGUILayout.EndHorizontal();
-
- sourcePath = AssetDatabase.GetAssetPath(sourceTexture);
-
- if(sourceTexture != null){
- string pattern = "/" + sourceTexture.name + "\\.\\w+";
- Debug.Log(sourcePath);
- Debug.Log(pattern);
- Regex rgx = new Regex(pattern);
- sourceFolerPath = rgx.Replace(sourcePath, "");
-
- string sourceTexName = sourceTexture.name;
- retPath = sourceFolerPath + "/" +sourceTexName;
- }
-
- EditorGUILayout.BeginHorizontal();
-
- EditorGUILayout.LabelField("存储路径");
- EditorGUILayout.TextField(retPath);
- if (GUILayout.Button("选择存储路径"))
- {
- retPath = EditorUtility.OpenFolderPanel("选择存储路径", retPath, "");
- }
-
- EditorGUILayout.EndHorizontal();
-
- if (GUILayout.Button("生成"))
- {
- Split();
- }
- }
-
- void Split(){
- SplitTexture();
- SaveSplitTexture();
- }
-
- void SplitTexture(){
- int width = maskTexture.width;
- int height = maskTexture.height;
-
- for(int i = 0; i < width; i++){
- for(int j = 0; j < height; j++){
- Color pixelCol = maskTexture.GetPixel(i, j);
- Color32 maskCol = pixelCol;
- if(allTextureList.ContainsKey(maskCol.a) == false){
- Texture2D tex = new Texture2D(width, height, TextureFormat.RGBA32, false);
- allTextureList.Add(maskCol.a, tex);
- tex.SetPixel(i, j, pixelCol);
- }
- }
- }
- foreach(KeyValuePair<byte, Texture2D> tex in allTextureList){
- tex.Value.Apply();
- }
- }
-
- void SaveSplitTexture(){
- if(AssetDatabase.IsValidFolder(retPath) == false){
- AssetDatabase.CreateFolder(sourceFolerPath, sourceTexture.name);
- }
-
- if(AssetDatabase.IsValidFolder(retPath) == false){
- EditorUtility.DisplayDialog("文件路径错误", "错误的文件夹:" + retPath, "确定", null);
- return;
- }
-
- for(int i = 10; i < sourceTexture.width; i++){
- for(int j = 10; j < sourceTexture.height; j++){
- sourceTexture.SetPixel(i, j, new Color(1, 0, 0, 1));
- }
- }
- sourceTexture.Apply();
- // int splitTexCount = 0;
- // foreach(KeyValuePair<byte, Texture2D> tex in allTextureList){
- // splitTexCount++;
- // AssetDatabase.CreateAsset(tex.Value, retPath + "/" + splitTexCount + ".png");
- // }
-
- AssetDatabase.SaveAssets();
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。