Unity Editor 基础篇(九):EditorUtility编辑器工具

EditorUtility 编辑器工具

转自:http://blog.csdn.net/liqiangeastsun/article/details/42174339,请查看原文,尊重楼主原创版权。

这是一个编辑器类,如果想使用它你需要把它放到工程目录下的Assets/Editor文件夹下。

编辑器类在UnityEditor命名空间下。所以当使用C#脚本时,你需要在脚本前面加上 "using UnityEditor"引用。

1.ProgressBar 进度条

在Editor文件夹中添加脚本:

 

 
  1. using UnityEngine;

  2. using System.Collections;

  3. using UnityEditor;

  4.  
  5. public class TestEditor : EditorWindow

  6. {

  7. float secs = 10.0f;

  8. double startVal = 0;

  9. double progress = 0;

  10. bool isShow = false;

  11.  
  12. [MenuItem("Examples/Cancelable Progress Bar Usage")]

  13. static void Init()

  14. {

  15. var window = GetWindow(typeof(TestEditor));

  16. window.Show();

  17. }

  18.  
  19. void OnGUI()

  20. {

  21. secs = EditorGUILayout.FloatField("Time to wait:", secs);

  22. if (GUILayout.Button("Display bar"))

  23. {

  24. startVal = EditorApplication.timeSinceStartup; //开始编译到现在的时间

  25. isShow = !isShow;

  26. }

  27.  
  28. if (GUILayout.Button("Clear bar"))

  29. {

  30. EditorUtility.ClearProgressBar(); //清空进度条的值, 基本没什么用

  31. }

  32.  
  33. if (progress < secs && isShow == true)

  34. {

  35. //使用这句代码,在进度条后边会有一个 关闭按钮,但是用这句话会直接卡死,切记不要用

  36. // EditorUtility.DisplayCancelableProgressBar("Simple Progress Bar", "Show a progress bar for the given seconds", (float)(progress / secs));

  37. //使用这句创建一个进度条, 参数1 为标题,参数2为提示,参数3为 进度百分比 0~1 之间

  38. EditorUtility.DisplayProgressBar("Simple Progress Bar", "Show a progress bar for the given seconds", (float)(progress / secs));

  39. }

  40. else {

  41. startVal = EditorApplication.timeSinceStartup;

  42. progress = 0.0f;

  43. return;

  44. }

  45. progress = EditorApplication.timeSinceStartup - startVal;

  46. }

  47.  
  48. void OnInspectorUpdate() //更新

  49. {

  50. Repaint(); //重新绘制

  51. }

  52. }

 

效果:

 

 

2.CollectDeepHierarchy收集深度层级

 

 

遍历对象以及子物体,以及子物体上绑定的所有组件

 

 
  1. using UnityEngine;

  2. using System.Collections;

  3. using UnityEditor;

  4.  
  5. public class Test : MonoBehaviour { //遍历对象以及子物体,以及子物体上绑定的所有组件

  6.  
  7. private GameObject parent;

  8.  
  9. // Use this for initialization

  10. void Start () {

  11. Init();

  12. }

  13.  
  14. void Init()

  15. {

  16. parent = gameObject; // 给parent赋值 为 gameObject ,在Hierarchy 中给该对象创建几个子物体

  17. Object[] obj = new Object[1];

  18. obj[0] = parent; //将parent添加至 数组

  19. Object[] result = EditorUtility.CollectDeepHierarchy(obj);

  20.  
  21. foreach (Object ob in result) //遍历 所有对象,得到对象本身包括子对象上绑定的所有组件

  22. {

  23. print(ob + " " + ob.name); //

  24. }

  25. }

  26. }


3.CompressTexture压缩一个纹理到指定的格式

 

 

 
  1. using UnityEngine;

  2. using System.Collections;

  3. using UnityEditor;

  4.  
  5. public class Test : AssetPostprocessor { //

  6.  
  7. void OnPostprocessTexture(Texture2D T) //使用该方法压缩一个纹理到指定的格式

  8. {

  9. //该方法需使用 Texture2D, 使用该方法比较快速但是会降低效果

  10. EditorUtility.CompressTexture(T, TextureFormat.RGB24, TextureCompressionQuality.Best);

  11. }

  12. }

 

 

4.CreateGameObjectWithHideFlags 创建带有标识的游戏物体

在Editor文件夹下创建脚本:

 

 
  1. using UnityEngine;

  2. using System.Collections;

  3. using UnityEditor;

  4.  
  5. public class TestEditor : EditorWindow

  6. {

  7. private string objName = "GameObject";

  8. private int instanceID = 0;

  9. private bool create = true;

  10. private GameObject go = null;

  11.  
  12. private bool hideHierarchy = true;

  13.  
  14. [MenuItem("Examples/GameObject flags")]

  15. static void Init()

  16. {

  17. TestEditor window = (TestEditor)GetWindow(typeof(TestEditor)); //初始化一个窗口

  18. window.Show();

  19. }

  20.  
  21. void OnGUI()

  22. {

  23. create = EditorGUILayout.Toggle("Create a GO:", create); //在窗口创建一个 Toggle

  24.  
  25. GUI.enabled = create; //GUI是否可以创建

  26. objName = EditorGUILayout.TextField("GameObject Name:", objName); //创建文本框

  27. if (GUILayout.Button("Create")) //创建按钮

  28. { //点击按钮,创建一个GameObject

  29. GameObject created = EditorUtility.CreateGameObjectWithHideFlags(

  30. objName,

  31. hideHierarchy ? HideFlags.HideInHierarchy : 0);

  32. //HideFlags.HideInHierarchy 对象在Hierarchy 窗口隐藏

  33. //HideFlags.HideInInspector 对象在Hierarchy窗口可见,点击该对象,在Inspector面板不显示任何属性

  34. GameObject ccc = EditorUtility.CreateGameObjectWithHideFlags("cccc", HideFlags.HideInInspector);

  35. Debug.Log("Created GameObject ID: " + created.GetInstanceID());

  36. }

  37.  
  38. GUI.enabled = !create;

  39. EditorGUILayout.BeginHorizontal(); //开始水平布局

  40. instanceID = EditorGUILayout.IntField("Instance ID:", instanceID); //创建一个 整数输入框

  41. if (GUILayout.Button("Search & Update flags")) //创建一个按钮,更新flags

  42. {

  43. go = null;

  44. go = (GameObject)EditorUtility.InstanceIDToObject(instanceID); //给该对象实例化一个ID

  45. if (go)

  46. go.hideFlags = hideHierarchy ? HideFlags.HideInHierarchy : 0;

  47. }

  48. EditorGUILayout.EndHorizontal(); //结束水平布局

  49.  
  50. if (!go)

  51. EditorGUILayout.LabelField("Object: ", "No object was found");

  52. else

  53. EditorGUILayout.LabelField("Object: ", go.name);

  54.  
  55. GUI.enabled = true;

  56. hideHierarchy = EditorGUILayout.Toggle("HideInHierarchy", hideHierarchy); //创建一个Toggle ,

  57.  
  58. }

  59. }

 

 

效果:

Unity在菜单栏创建按钮,点击按钮创建一个窗口,在窗口上创建 Toggle、TextField、button等, 在窗口创建了一个Create按钮,点击按钮创建对象

勾选 Create a Go: 的Toggle,显示Create按钮
点击Create按钮,在 Hierarchy 窗口创建 对象“aaa” “ccc”

选中 “aaa”,Inspector窗口如下所示

 

选中 “cccc” 在,Inspector窗口如下所示,”cccc“绑定的组件在Inspector面板隐藏


5.DisplayDialog显示对话框  DisplayDialogComplex 显示复杂对话框

用于在编辑器显示消息框。

1/ DisplayDialog显示对话框(返回true/false)

ok 和 cancel 是显示在对话框按钮上的标签,如果cancel为空(默认),然只有一个按钮被显示。如果ok按钮被按下,DisplayDialog返回true。

 

 

 
  1. //在地形的表面上放置选择的物体。

  2. using UnityEngine;

  3. using UnityEditor;

  4.  
  5. public class PlaceSelectionOnSurface : ScriptableObject {

  6. [MenuItem ("Example/Place Selection On Surface")]

  7. static void CreateWizard () {

  8. Transform[] transforms = Selection.GetTransforms(SelectionMode.Deep |

  9. SelectionMode.ExcludePrefab | SelectionMode.OnlyUserModifiable);

  10.  
  11. if (transforms.Length > 0 &&

  12. EditorUtility.DisplayDialog("Place Selection On Surface?",

  13. "Are you sure you want to place " + transforms.Length

  14. + " on the surface?", "Place", "Do Not Place")) {

  15. foreach (Transform transform in transforms) {

  16. RaycastHit hit;

  17. if (Physics.Raycast(transform.position, Vector3.down, out hit)) {

  18. transform.position = hit.point;

  19. Vector3 randomized = Random.onUnitSphere;

  20. randomized = new Vector3(randomized.x, 0F, randomized.z);

  21. transform.rotation = Quaternion.LookRotation(randomized, hit.normal);

  22. }

  23. }

  24. }

  25. }

  26. }

 

 

 

2/ DisplayDialogComplex 显示复杂对话框(返回0/1/2对应ok/cancel/alt)

 
  1. //让你保存,保存并退出或退出不保存

  2. class EditorUtilityDisplayDialogComplex extends MonoBehaviour {

  3.  
  4. @MenuItem("Examples/Enhanced Save")

  5. static function Init() {

  6. var option = EditorUtility.DisplayDialogComplex(

  7. "What do you want to do?",

  8. "Please choose one of the following options.",

  9. "Save Scene",

  10. "Save and Quit",

  11. "Quit without saving");

  12. switch (option) {

  13. // Save Scene //保存场景

  14. case 0:

  15. EditorApplication.SaveScene(EditorApplication.currentScene);

  16. break;

  17. // Save and Quit. //保存并退出

  18. case 1:

  19. EditorApplication.SaveScene(EditorApplication.currentScene);

  20. EditorApplication.Exit(0);

  21. break;

  22. // Quit Without saving. // 退出不保存

  23. case 2:

  24. EditorApplication.Exit(0);

  25. break;

  26. default:

  27. Debug.LogError("Unrecognized option.");

  28.  
  29. }

  30. }

  31. }


6.DisplayPopupMenu显示弹出菜单

 

static function DisplayPopupMenu (position : Rect, menuItemPath : string, command : MenuCommand) : void

 

菜单显示在position位置,从menuItemPath指定的子菜单生成,使用MenuCommand作为菜单上下文。

 

 
  1. 在Editor文件夹下创建脚本TestEditor

  2. using UnityEngine;

  3. using System.Collections;

  4. using UnityEditor;

  5.  
  6. public class TestEditor : EditorWindow

  7. {

  8. [MenuItem("Examples/Enhanced Save")]

  9. static void Init()

  10. {

  11.  
  12. Rect contextRect = new Rect(10, 10, 100, 100);

  13. EditorUtility.DisplayPopupMenu(contextRect, "Assets/", null);

  14. }

  15. }


在工具栏创建Button点击Button,在Asset下创建窗口

 

 

 

双击“Enbanced Save”显示如下窗口,即Asset下创建窗口

 

7.FocusProjectWindow焦点项目窗口

使项目窗口到前面并焦点它,这个通常在一个菜单项创建并选择一个资源之后被调用。

 

8.SaveFilePanel保存文件面板

SaveFolderPanel 保存文件夹面板

Unity编辑器之导入导出获取路径对话框:

 

选中一个图片,点击 “Save Texture to file”按钮:

 

 

 
  1. 在Editor文件夹下创建脚本

  2. using UnityEngine;

  3. using System.Collections;

  4. using UnityEditor;

  5. using System.IO;

  6.  
  7. public class TestEditor : EditorWindow

  8. {

  9. [MenuItem("Examples/Save Texture to file")]

  10. static void Apply()

  11. {

  12.  
  13. Texture2D texture = Selection.activeObject as Texture2D; //选中一个图片

  14. if (texture == null)

  15. { //如果没选图片,显示提示对话框

  16. EditorUtility.DisplayDialog(

  17. "Select Texture",

  18. "You Must Select a Texture first!",

  19. "Ok");

  20. return;

  21. }

  22. //获取路径

  23. string path = EditorUtility.SaveFilePanel(

  24. "Save texture as PNG",

  25. "",

  26. texture.name + ".png",

  27. "png");

  28.  
  29. if (path.Length != 0)

  30. {

  31. // Convert the texture to a format compatible with EncodeToPNG

  32. if (texture.format != TextureFormat.ARGB32 && texture.format != TextureFormat.RGB24)

  33. {

  34. Texture2D newTexture = new Texture2D(texture.width, texture.height);

  35. newTexture.SetPixels(texture.GetPixels(0), 0);

  36. texture = newTexture;

  37. }

  38. var pngData = texture.EncodeToPNG();

  39. if (pngData != null)

  40. File.WriteAllBytes(path, pngData);

  41. }

  42. }

  43. }

 

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Jack Yan

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

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

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

打赏作者

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

抵扣说明:

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

余额充值