转载:http://blog.csdn.net/liqiangeastsun/article/details/42173613
使用Unity编辑器类编辑 ToolBar
两种类型的 Toolbar
第一种只显示文字
第二种显示图片
在Editor文件夹下创建脚本EditorScript
using UnityEngine;
using System.Collections;
using UnityEditor;
[CanEditMultipleObjects]
[CustomEditor(typeof(TestScript))]
public class EditorScript : Editor {
private TestScript testScript;
private int select = 0;
private int selected = 0;
private string[] names = { "one", "two", "three", "four"};
private static GUIContent[] myGUIContent = new GUIContent[4];
private Texture2D[] myTexture = new Texture2D[4];
private void OnEnable()
{
testScript = (TestScript)target;
Init();
}
private void Init() //初始化获取图片
{
for (int i = 0; i < 4; i++)
{
//通过路径获取图片资源
myTexture[i] = (Texture2D)AssetDatabase.LoadAssetAtPath("Assets/MyTexture/1.png", typeof(Texture2D));
//将图片数组和字符串数组存进去
myGUIContent[i] = new GUIContent(myTexture[i], names[i]);
}
}
public override void OnInspectorGUI() //绘制在Inspector面板上
{
EditorGUILayout.BeginVertical();
//创建 Toolbar , 返回值为选中的项, 参数1 为 选中的项, 参数 2 每项上的文字
select = GUILayout.Toolbar(select, names);
EditorGUILayout.Space();
EditorGUILayout.Space();
//参数 1, 选中的项, 参数2 每项的图片, 参数3 整个Toolbar的宽度, 参数4 Toolbar的高度
selected = GUILayout.Toolbar(selected, myGUIContent, GUILayout.Width(300), GUILayout.Height(64));
EditorGUILayout.Space();
EditorGUILayout.EndVertical();
}
}
TestScript 脚本如下
using UnityEngine;
using System.Collections;
public class TestScript : MonoBehaviour {
public void MyTestA()
{
Debug.Log("Click");
}
}