Unity 編輯器擴展

 

自定義檢視面板的使用:

 

先是定義一個腳本文件,咱們來修飾它的檢視面板:html

[HelpURL("http://www.baidu.com")]
public class Atr : MonoBehaviour
{
    public int id;
    public string Name;
    [Multiline(5)]
    public string BackStory;

    public float health;
    public float damage;

    public float weaponDamagel, weaponDamage2;
    public string shoeName;
    public int shoeSize;
    public string shoeType;

    [Space(100)]
    [Range(-2,5)]
    public int time;
    void Start ()
    {
        health = 50;
    }
}

而後在根目錄的Editor文件夾下定義一個用來修飾上面腳本檢視面板的類文件:api

using UnityEngine;
using System.Collections;
using UnityEditor;

[CustomEditor(typeof(Atr))]
//須要繼承自editor,而且引入UnityEditor程序集
public class LearnInspector : Editor
{
    private Atr atr;
    private bool showWeapons;

    void OnEnable()
    {
        //獲取當前自定義的Inspector對象
        atr = (Atr) target;
    }

    //執行該函數來自定義檢視面板
    public override void OnInspectorGUI()
    {
        //不寫默認是垂直佈局
        EditorGUILayout.BeginVertical();

        //空格
        EditorGUILayout.Space();
        EditorGUILayout.Space();

        EditorGUILayout.LabelField("Base Info");
        atr.id = EditorGUILayout.IntField("Atr ID", atr.id);
        atr.Name = EditorGUILayout.TextField("Atr Name", atr.Name);

        EditorGUILayout.Space();
        EditorGUILayout.Space();
        EditorGUILayout.Space();

        EditorGUILayout.LabelField("Back Story");
        atr.BackStory = EditorGUILayout.TextArea(atr.BackStory, GUILayout.MinHeight(100));

        EditorGUILayout.Space();
        EditorGUILayout.Space();
        EditorGUILayout.Space();

        atr.health = EditorGUILayout.Slider("Health", atr.health, 0, 100);

        if (atr.health < 20)
        {
            GUI.color = Color.red;
        }
        else if (atr.health>80)
        {
            GUI.color = Color.green;
        }
        else
        {
            GUI.color = Color.grey;
        }


        Rect progressRect = GUILayoutUtility.GetRect(50, 50);

        EditorGUI.ProgressBar(progressRect,atr.health/100.0f,"Health");

        GUI.color = Color.white;

        EditorGUILayout.Space();
        EditorGUILayout.Space();
        EditorGUILayout.Space();


        atr.damage = EditorGUILayout.Slider("Damage", atr.damage, 0, 20);

        if(atr.damage<10)
        {
            EditorGUILayout.HelpBox("傷害太低",MessageType.Error);
        }
        else if (atr.damage > 15)
        {
            EditorGUILayout.HelpBox("傷害太高",MessageType.Warning);
        }
        else
        {
            EditorGUILayout.HelpBox("傷害適中",MessageType.Info);
        }

        EditorGUILayout.Space();
        EditorGUILayout.Space();
        EditorGUILayout.Space();

        EditorGUILayout.LabelField("Shoe");
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("Name", GUILayout.MaxWidth(50));
        atr.shoeName = EditorGUILayout.TextField(atr.shoeName);
        EditorGUILayout.LabelField("Size", GUILayout.MaxWidth(50));
        atr.shoeSize = EditorGUILayout.IntField(atr.shoeSize); EditorGUILayout.LabelField("Type", GUILayout.MaxWidth(50));
        atr.shoeType = EditorGUILayout.TextField(atr.shoeType);

        EditorGUILayout.EndHorizontal();

        EditorGUILayout.EndVertical();
    }
}

//繪製字段用到的方法

//EditorGUILayout.LabelField()標籤字段
//EditorGUILayout.IntField() 整數字段
//EditorGUILayout.FloatField() 浮點數字段
//EditorGUILayout.TextField() 文本字段
//EditorGUILayout.Vector2Field() 二維向量字段
//EditorGUILayout.Vector3Field() 三維向量字段
//EditorGUILayout.Vector4Field() 四維向量字段

能夠看出該修飾類和效果圖對應的關係。咱們能夠方便的定義檢視面板來協助遊戲的開發調試,讓它直觀的顯示出幫助消息。編輯器

更多的信息能夠查看幫助文檔:http://www.ceeger.com/Script/Editor/Editor.htmlide

 


 

自定義窗口:

 

 1 using System;
 2 using UnityEngine;
 3 using System.Collections;
 4 using System.IO;
 5 using UnityEditor;
 6 using UnityEditor.SceneManagement;
 7 
 8 public class MyFirstWindow : EditorWindow
 9 {
10 
11     public Texture TxTexture;
12     string bugReporterName = "";
13     string description = "";
14     GameObject buggyGameObject;
15 
16     MyFirstWindow()
17     {
18         this.titleContent=new GUIContent("Bug Rt");
19     }
20 
21 
22     [MenuItem("Tool/Bug Reporter")]
23     static void showWindow()
24     {
25         EditorWindow.GetWindow(typeof (MyFirstWindow));
26     }
27 
28     //繪製窗口界面
29     void OnGUI()
30     {
31         GUILayout.BeginVertical();
32 
33         GUILayout.Space(10);
34         GUI.skin.label.fontSize = 24;
35         GUI.skin.label.alignment = TextAnchor.MiddleCenter;
36         GUILayout.Label("Bug Report");
37 
38         GUILayout.Space(10);
39         bugReporterName = EditorGUILayout.TextField("Bug Name", bugReporterName);
40 
41         GUILayout.Space(10);
42         GUI.skin.label.fontSize = 12;
43         GUI.skin.label.alignment = TextAnchor.UpperLeft;
44         GUILayout.Label("Currently Scene:"+EditorSceneManager.GetActiveScene().name);
45 
46         GUILayout.Space(10);
47         GUILayout.Label("Time:"+System.DateTime.Now);
48 
49 
50         GUILayout.Space(10);
51         buggyGameObject =
52             (GameObject) EditorGUILayout.ObjectField("Buggy Go", buggyGameObject, typeof (GameObject), true);
53 
54         GUILayout.Space(10);
55         GUILayout.BeginHorizontal();
56         GUILayout.Label("Description", GUILayout.MaxWidth(80));
57         description = EditorGUILayout.TextArea(description, GUILayout.MaxHeight(75));
58         GUILayout.EndHorizontal();
59 
60         EditorGUILayout.Space();
61 
62         if (GUILayout.Button("Save Bug"))
63         {
64             SaveBug();
65 
66         }
67 
68         if (GUILayout.Button("Save Bug With Screenshoot"))
69         {
70             SaveBugWithScreeshot();
71         }
72 
73         EditorGUILayout.EndVertical();//佈局開始和結束相對應,缺乏時可能出現窗口中的元素沒法自適應的狀況
74     }
75 
76     private void SaveBugWithScreeshot()
77     {
78         Writer();
79         Application.CaptureScreenshot("Assets/BugReports/" + bugReporterName + "/" + bugReporterName + ".png");
80     }
81 
82     private void SaveBug()
83     {
84         Writer();
85     }
86 
87     //IO類,用來寫入保存信息
88     void Writer()
89     {
90         Directory.CreateDirectory("Assets/BugReports/" + bugReporterName);
91         StreamWriter sw = new StreamWriter("Assets/BugReports/" + bugReporterName + "/" + bugReporterName + ".txt");
92         sw.WriteLine(bugReporterName);
93         sw.WriteLine(DateTime.Now.ToString());
94         sw.WriteLine(EditorSceneManager.GetActiveScene().name);
95         sw.WriteLine(description);
96         sw.Close();
97     }
98 }

 


自定義菜單項:

  1 using UnityEditor;
  2 using UnityEngine;
  3 using System.Collections;
  4 
  5 /// <summary>
  6 /// 工具類更名
  7 /// </summary>
  8 public class ChangeName : ScriptableWizard
  9 {
 10 
 11 
 12     public string PrefixStr = null;
 13     
 14     /// <summary>  
 15     /// 當沒有任何GameObject被選中的時候,將菜單disable(注意,這個函數名能夠隨意取)  
 16     /// </summary>  
 17     /// <returns></returns>  
 18     [MenuItem("ChangeName/AddPrefixAndEuipment", true)]
 19     static bool CreateWindowDisabled()
 20     {
 21         return Selection.activeTransform;
 22     }
 23 
 24 
 25     /// <summary>  
 26     /// 建立編輯窗口(注意,這個函數名能夠隨意取)  
 27     /// </summary>  
 28     [MenuItem("ChangeName/AddPrefixAndEuipment")]
 29     static void CreateWindow()
 30     {
 31         //第一個參數窗口標題
 32         // 定製窗口標題和按鈕,其中第二個參數是Create按鈕,第三個則屬於other按鈕  
 33         // 若是不想使用other按鈕,則可調用DisplayWizard的兩參數版本  
 34         DisplayWizard<ChangeName>(
 35             "AddPrefix",
 36             "Add", "Remove");
 37     }
 38 
 39     /// <summary>  
 40     /// 窗口建立或窗口內容更改時調用  
 41     /// </summary>  
 42     void OnWizardUpdate()
 43     {
 44         helpString = "Note: Prefix are not created";
 45 
 46         if (string.IsNullOrEmpty(PrefixStr))
 47         {
 48             errorString = "Please enter Prefix";
 49             isValid = false;
 50         }
 51         else
 52         {
 53             errorString = "";
 54             isValid = true;
 55         }
 56     }
 57 
 58 
 59     /// <summary>  
 60     /// 點擊Add按鈕(即Create按鈕)調用  
 61     /// </summary>  
 62     void OnWizardCreate()
 63     {
 64 
 65         Transform[] transforms = Selection.GetTransforms(SelectionMode.TopLevel | SelectionMode.OnlyUserModifiable);
 66 
 67         foreach (Transform transform in transforms)
 68         {
 69             transform.name = PrefixStr + transform.name; 71         }
 72 
 73         Debug.Log("AddPrefixAndEuipment " + PrefixStr+"successed!");
 74     }
 75 
 76     /// <summary>  
 77     /// 點擊Remove(即other按鈕)調用  
 78     /// </summary>  
 79     void OnWizardOtherButton()
 80     {
 81         Transform[] transforms = Selection.GetTransforms(SelectionMode.TopLevel | SelectionMode.OnlyUserModifiable);
 82 
 83         foreach (Transform transform in transforms)
 84         {
 85             if (transform.name.Contains(PrefixStr))
 86             {
 87                 transform.name=transform.name.Replace(PrefixStr, "");
 88             }
 89         }
 90 
 91         Debug.Log("ReMove prefix " + PrefixStr + "successed!");
 92     }
 93 
 94 
 95     
 96     [MenuItem("ChangeName/AddOrderNume2Name ")]// 爲選擇的對象名稱添加序列號,讓重名的物體相區別  97     static void AddOrder2Name()  98  {  99         int n = 0; 100         Transform[] transforms = Selection.GetTransforms(SelectionMode.TopLevel | SelectionMode.OnlyUserModifiable); 101 
102         foreach (Transform transform in transforms) 103  { 104             transform.name = transform.name + n.ToString(); 105             n++; 106  } 107  } 108 
109     [MenuItem("GameObject/Add Child %g")] 110     static void MenuAddChild() 111  { 112         Transform[] transforms = Selection.GetTransforms(SelectionMode.TopLevel | SelectionMode.OnlyUserModifiable); 113 
114         foreach (Transform transform in transforms) 115  { 116             GameObject newChild = new GameObject("_Child"); 117             newChild.transform.parent = transform; 118             
119  } 120  } 121 
122     [MenuItem("ChangeName/ReNameUILabelX")] 123     static void ReNameUILableX() 124  { 125         UIButton [] UIButtions = GameObject.Find("CenterMenu").transform.GetChild(0).GetComponentsInChildren<UIButton>(); 126 
127         foreach (UIButton uiButtion in UIButtions) 128  { 129             if (uiButtion.gameObject.activeSelf) 130  { 131                 uiButtion.GetComponentInChildren<EptBtnOnClick>().EptName="X_"+ uiButtion.GetComponentInChildren<UILabel>().text; 132  } 133  } 134  } 135 141 
142 }
ScriptableWizard:
繼承自EditorWindow,主要用來作嚮導。有2個按鈕,一個是Create,另外一個是Other。當咱們使用它的時候,他始終顯示在最上層,不會被unity其餘窗口遮擋。

   SelectionMode:函數

can be used to tweak the selection returned by Selection.GetTransforms.工具

Note: This is an editor class. To use it you have to place your script in Assets/Editor inside your project folder. Editor classes are in the UnityEditor namespace so for C# scripts you need to add "using UnityEditor;" at the beginning of the script.佈局

The default transform selection mode is: SelectionMode.TopLevel | SelectionMode.ExcludePrefab | SelectionMode.Editable.學習

Unfiltered

Return the whole selection.測試

TopLevel

Only return the topmost selected transform. A selected child of another selected transform will be filtered out.ui

Deep

Return the selection and all child transforms of the selection.

ExcludePrefab

Excludes any prefabs from the selection.

Editable

Excludes any objects which shall not be modified.

Assets

Only return objects that are assets in the Asset directory.

DeepAssets

If the selection contains folders, also include all assets and subfolders within that folder in the file hierarchy.

 

 

 

 

 

 

 

 

 

 

 

補充(190426):

一鍵導出包,很是方便遷移測試程序或者本身的代碼庫

 1 using System;
 2 #if UNITY_EDITOR
 3 using UnityEditor;
 4 using System.IO;
 5 #endif
 6 using UnityEngine;
 7 
 8 namespace WSFramework{
 9 
10 
11     public class ExportPakge{
12 
13 #if UNITY_EDITOR
14          [MenuItem("WSTool/ExportPac %e")]
15         private static void ExportPackageAndCopyName(){
16             var path="Assets/WSFramework";
17             var fileName="WS_"+DateTime.Now.ToString("yyyyMMdd_hh")+".unitypackage";
18 
19             //--剪切板API,導出後直接粘貼出當前包的名字
20             GUIUtility.systemCopyBuffer=fileName;
21 
22             
23             //--導出package api
24             AssetDatabase.ExportPackage(path,fileName, ExportPackageOptions.Recurse);
25 
26             //--打開asset目錄
27             Application.OpenURL("file://"+Application.dataPath);
28             
29             
30         }
31     }
32 
33 #endif
34 }

 

 

assetstore一個很優秀的編輯器擴展插件,只須要在類中添加 attribute標籤就能方便的實現不少功能,有興趣的能夠下載學習下

相關文章
相關標籤/搜索