| 前言:在unity编辑器中添加自己需要的工具栏目。差不多这个意思,本文介绍在tools下面添加一个条目“添加本地化组件”。
 创建一个类EditorWindow 
 UnityEditor.EditorWindow
  创建一个类PrefabLocalize,继承EditorWindow,其中AddLocalizeToGoWithText这个方法必须是静态的。 using UnityEditor;
using UnityEngine;
public class PrefabLocalize : EditorWindow
{
	void OnGUI()
	{
		
	}
	void OnEnable()
	{
		
	}
	[MenuItem("Tools/I2 Localization/添加本地化组件")]
    static void AddLocalizeToGoWithText()
    {
        GetWindow(typeof(PrefabLocalize));
    }
}
 创建完后就可以看到unity编辑器的工具栏目里面多出来一个条目了
  设计弹出框的UIGUIStyle 
 UnityEngine.GUIStyle
  先定义一些样式:
 private GUIStyle customGuiStyle;
private GUIStyle customLabStyle;
private GUIStyle customBtnStyle;
private GUIStyle customTogStyle;
private Vector2 _scroll;
 EditorStyles[UnityEditor基础]EditorStyles 编辑样式设置样式:
 private void OnEnable()
{
     customGuiStyle = new GUIStyle(EditorStyles.textField);
     customLabStyle = new GUIStyle(EditorStyles.label);
     customBtnStyle = new GUIStyle(EditorStyles.miniButton);
     customTogStyle = new GUIStyle(EditorStyles.toggle);
     customLabStyle.fontSize = 14;
     customGuiStyle.fontSize = 14;
     customBtnStyle.fontSize = 14;
     customTogStyle.alignment = TextAnchor.MiddleCenter;
     customTogStyle.fixedHeight = 20;
     customTogStyle.fixedWidth = 20;
 }
 EditorWindow.OnGUI()在这里设计具体的UI: private void OnGUI()
{
    EditorGUILayout.BeginHorizontal();
    GUILayout.Label("路径", customLabStyle, GUILayout.MaxWidth(40));
    EditorGUILayout.LabelField(txtPath, customGuiStyle, GUILayout.Height(20), GUILayout.MinWidth(300));
    if (GUILayout.Button("浏览", customBtnStyle, GUILayout.MaxWidth(50)))
    {
        txtPath = EditorUtility.OpenFolderPanel("选择文件夹", Application.dataPath + RelativePath, "");
    }
    var button = GUILayout.Button("提取", customBtnStyle, GUILayout.MaxWidth(50));
    if (button)
    {
        previewList = GetList(txtPath);
        isShowPreview = canAdd = true;
    }
    var button1 = GUILayout.Button("确定", customBtnStyle, GUILayout.MaxWidth(50));
    if (button1)
    {
        if (canAdd)
        {
            canAdd = false;
            Add();
        }
    }
    EditorGUILayout.EndHorizontal();
    
    if (isShowPreview)
    {
        _scroll = EditorGUILayout.BeginScrollView(_scroll, GUILayout.Height(800));
        foreach (var item in previewList)
        {
	        EditorGUILayout.BeginHorizontal(GUILayout.Height(20));
	        item.isSelect = EditorGUILayout.Toggle(line.isSelect, customTogStyle, GUILayout.Height(20), GUILayout.Width(20));
	        EditorGUILayout.TextField(item.name, customGuiStyle, GUILayout.Height(20), GUILayout.MinWidth(100));
	        EditorGUILayout.EndHorizontal();
        }
        EditorGUILayout.EndScrollView();
    }
}
 把内容放在一行内: EditorGUILayout.BeginHorizontal();
EditorGUILayout.EndHorizontal();
 成果点击上面“Tools-I2…-添加本地化组件”之后会弹出设置好的窗口,点击浏览会弹出文件选择框,然后巴拉巴拉。。。
  官方文档 |