当前位置:   article > 正文

Unity EditorWindow---扩展自定义编辑器窗口_unity editorwindow rect

unity editorwindow rect

一、EditorWindow类属性及函数

首先提供官方参考文档:API-Reference-EditorWindow

概述: EditorWindow类继承自ScriptableObject类;我们可以通过继承这个类并重写来创建一个自定义的编辑器窗口,它可以随意停靠或放置在编辑器内任意位置,就像unity界面原生窗口一样;

static 属性

  •  

static 方法

  • CreateWindow():unity新提供的创建窗口函数;
  • GetWindow() / GetWindowWithRect():获取窗口,可以将窗口停靠在其它类型的窗口上;如下,该函数会获取第一次创建的窗口,如果没有就创建一个;它会自动显示窗口,无需调用open()函数;
EditorWindow.GetWindow<FirstEditorWindow>();

public 属性

  • position (Rect):窗口位置
  • maxSize/minSize (Vector2):最大最小尺寸

public 方法

  • Show() / Close():

消息回调

  • OnGUI():

Inherited:

static 属性

  •  

static 方法

  •  

public 属性

  •  

public 方法

  •  

消息回调

  • Awake():
  • OnEnable() / OnDisable():当窗口被创建时调用,可以在OnEnable()设置窗口的一些属性;

二、相关的简单GUI类

GUIContent:一种GUI元素,构造函数共包含三个参数,有多种构造函数类型;这是一个非常重要的类,几乎所有的控件都会用到它作为显示文字的参数

GUIContent(string text,Texture image,string tooltip);

三、应用

1.创建自定义EditorWindow的三步骤

  • 创建一个继承自EditorWindow的脚本;
  • 使用代码来触发这个窗口的显示;
  • 在OnGUI回调函数中绘制GUI;
  1. class MyWindow : EditorWindow {
  2. [MenuItem ("Window/My Window")]
  3. public static void ShowWindow () {
  4. EditorWindow.GetWindow(typeof(MyWindow));
  5. }
  6. void OnGUI () {
  7. // The actual window code goes here
  8. }
  9. }

2.窗口的停靠

有时候,我们希望窗口能够和其它已有窗口绑定在一起,就需要在GetWindow()中传入参数;

窗口停靠时一般就两种方式,停靠在场景视图中,停靠在属性面板中,下面分别给出示例:

  1. //停靠在监视窗口
  2. FirstEditorWindow window = EditorWindow.GetWindow<FirstEditorWindow>(typeof(UnityEditor.InspectorWindow));
  3. //停靠在场景窗口
  4. FirstEditorWindow window = EditorWindow.GetWindow<FirstEditorWindow>(typeof(UnityEditor.SceneView));

3.EditorWindow&ScriptableObject

如果需要保存一些配置文件,可以将EditorWindow和ScriptableObject配合使用;

首先定义一个ScriptableObject的配置类,然后在打开窗口时加载和保存数据即可;

  1. public class GameSettingsData : ScriptableObject
  2. {
  3. public bool isFPSOpen = true;
  4. }
  5. private void Awake()
  6. {
  7. gameSettings = AssetDatabase.LoadAssetAtPath<GameSettingsData>(dataRelativePth);
  8. //判断有没有配置表数据,如果没有,则创建;有,则加载
  9. //if (!File.Exists(Application.dataPath+dataRelativePth))
  10. if (!gameSettings)
  11. {
  12. gameSettings = ScriptableObject.CreateInstance<GameSettingsData>();
  13. AssetDatabase.CreateAsset(gameSettings,dataRelativePth);
  14. }
  15. Init();
  16. }
  17. private void Init() {
  18. isFPSOpen = gameSettings.isFPSOpen;
  19. }

4.注意GUI控件的排列布局以及锚点设置

通过获取当前窗口的Rect属性来设置手动设置控件的位置,当然:控件的Rect是相对于当前Window的

Rect windowRect = this.position;

5.小案例

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. using System.Text.RegularExpressions;
  6. using System.IO;
  7. public class TextureSpitWindow : EditorWindow
  8. {
  9. // 设置窗口参数
  10. public Texture2D sourceTexture;
  11. public Texture2D maskTexture;
  12. public string sourcePath;
  13. public string sourceFolerPath; // 分割结果的存储路径
  14. public string retPath = ""; // 分割结果的存储路径
  15. // public List<Texture2D> allTextureList = new List<Texture2D>(); // 放置所有生成的texture
  16. Dictionary<byte, Texture2D> allTextureList = new Dictionary<byte, Texture2D>();
  17. [MenuItem ("Tools/Split Texture")]
  18. public static void ShowWindow () {
  19. EditorWindow thisWindow = EditorWindow.GetWindow(typeof(TextureSpitWindow));
  20. thisWindow.titleContent = new GUIContent("切割图片");
  21. thisWindow.position = new Rect(Screen.width/2, Screen.height/2, 600, 800);
  22. }
  23. void OnGUI()
  24. {
  25. EditorGUILayout.BeginHorizontal();
  26. EditorGUILayout.LabelField("选择切割图片");
  27. sourceTexture = EditorGUILayout.ObjectField(sourceTexture, typeof(Texture2D), true) as Texture2D;
  28. EditorGUILayout.EndHorizontal();
  29. EditorGUILayout.BeginHorizontal();
  30. EditorGUILayout.LabelField("选择遮罩图片");
  31. maskTexture = EditorGUILayout.ObjectField(maskTexture, typeof(Texture2D), true) as Texture2D;
  32. EditorGUILayout.EndHorizontal();
  33. sourcePath = AssetDatabase.GetAssetPath(sourceTexture);
  34. if(sourceTexture != null){
  35. string pattern = "/" + sourceTexture.name + "\\.\\w+";
  36. Debug.Log(sourcePath);
  37. Debug.Log(pattern);
  38. Regex rgx = new Regex(pattern);
  39. sourceFolerPath = rgx.Replace(sourcePath, "");
  40. string sourceTexName = sourceTexture.name;
  41. retPath = sourceFolerPath + "/" +sourceTexName;
  42. }
  43. EditorGUILayout.BeginHorizontal();
  44. EditorGUILayout.LabelField("存储路径");
  45. EditorGUILayout.TextField(retPath);
  46. if (GUILayout.Button("选择存储路径"))
  47. {
  48. retPath = EditorUtility.OpenFolderPanel("选择存储路径", retPath, "");
  49. }
  50. EditorGUILayout.EndHorizontal();
  51. if (GUILayout.Button("生成"))
  52. {
  53. Split();
  54. }
  55. }
  56. void Split(){
  57. SplitTexture();
  58. SaveSplitTexture();
  59. }
  60. void SplitTexture(){
  61. int width = maskTexture.width;
  62. int height = maskTexture.height;
  63. for(int i = 0; i < width; i++){
  64. for(int j = 0; j < height; j++){
  65. Color pixelCol = maskTexture.GetPixel(i, j);
  66. Color32 maskCol = pixelCol;
  67. if(allTextureList.ContainsKey(maskCol.a) == false){
  68. Texture2D tex = new Texture2D(width, height, TextureFormat.RGBA32, false);
  69. allTextureList.Add(maskCol.a, tex);
  70. tex.SetPixel(i, j, pixelCol);
  71. }
  72. }
  73. }
  74. foreach(KeyValuePair<byte, Texture2D> tex in allTextureList){
  75. tex.Value.Apply();
  76. }
  77. }
  78. void SaveSplitTexture(){
  79. if(AssetDatabase.IsValidFolder(retPath) == false){
  80. AssetDatabase.CreateFolder(sourceFolerPath, sourceTexture.name);
  81. }
  82. if(AssetDatabase.IsValidFolder(retPath) == false){
  83. EditorUtility.DisplayDialog("文件路径错误", "错误的文件夹:" + retPath, "确定", null);
  84. return;
  85. }
  86. for(int i = 10; i < sourceTexture.width; i++){
  87. for(int j = 10; j < sourceTexture.height; j++){
  88. sourceTexture.SetPixel(i, j, new Color(1, 0, 0, 1));
  89. }
  90. }
  91. sourceTexture.Apply();
  92. // int splitTexCount = 0;
  93. // foreach(KeyValuePair<byte, Texture2D> tex in allTextureList){
  94. // splitTexCount++;
  95. // AssetDatabase.CreateAsset(tex.Value, retPath + "/" + splitTexCount + ".png");
  96. // }
  97. AssetDatabase.SaveAssets();
  98. }
  99. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/100093
推荐阅读
相关标签
  

闽ICP备14008679号