使用GUIStyle
首先创建一个Level脚本,用于自定义Inspector Editor。
using UnityEngine;
public class Level : MonoBehaviour
{
}
首先使用默认的GUIStyle,Inspector Editor代码如下:
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(Level))]
public class LevelInspector :Editor
{
private Level _myTarget;
private void OnEnable()
{
_myTarget = (Level)target;
}
public override void OnInspectorGUI()
{
EditorGUILayout.LabelField("Data");
}
}
效果如图:
默认的GUIStyle
使用我们自定义的GUIStyle,代码如下:
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(Level))]
public class LevelInspector :Editor
{
public GUIStyle _titleStyle;
private Level _myTarget;
private void OnEnable()
{
_myTarget = (Level)target;
InitStyles();
}
public override void OnInspectorGUI()
{
EditorGUILayout.LabelField("Data",_titleStyle);
}
private void InitStyles()
{
_titleStyle = new GUIStyle();
//字体居中
_titleStyle.alignment = TextAnchor.MiddleCenter;
//字号16
_titleStyle.fontSize = 16;
Texture2D titleBg = Resources.Load<Texture2D>("Color_Bg");
Font titleFont = Resources.Load<Font>("Oswald-Regular");
//normal状态下背景
_titleStyle.normal.background = titleBg;
//normal状态下的字体颜色
_titleStyle.normal.textColor = Color.white;
//设置字体
_titleStyle.font = titleFont;
//设置九宫格
_titleStyle.border = new RectOffset(10, 10, 20, 20);
}
}
资源文件如下:
资源文件
图片格式设置为Editor GUI and legacy GUI,如图:
图片格式设置
最终效果如图:
自定义的GUIStyle
使用GUISkin
创建一个GUISkin资源,右键菜单点击GUI Skin。
创建一个GUISkin
将创建出来的GUI Skin做如下修改:
修改GUISkin
代码如下:
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(Level))]
public class LevelInspector :Editor
{
public GUIStyle _titleStyle;
private Level _myTarget;
private void OnEnable()
{
_myTarget = (Level)target;
GUISkin guiSkin = Resources.Load<GUISkin>("LevelCreatorSkin");
_titleStyle = guiSkin.label;
}
public override void OnInspectorGUI()
{
EditorGUILayout.LabelField("Data",_titleStyle);
}
}
效果如图:
GUISkin效果图
关于GUI Style与GUISkin的区别,可以看看这个https://blog.csdn.net/sunny__chen/article/details/51323265
网友评论