Level script code
using UnityEngine;
namespace RunAndJump {
public partial class Level : MonoBehaviour {
[SerializeField]
public int _totalTime = 60;
[SerializeField]
private float _gravity = -30;
[SerializeField]
private AudioClip _bgm;
[SerializeField]
private Sprite _background;
[SerializeField]
private int _totalColumns = 25; //网格列数
[SerializeField]
private int _totalRows = 10; //网格行数
public const float GridCellSize = 1.28f; //网格单元大小
private readonly Color _normalColor = Color.grey; //正常网格颜色
private readonly Color _selectedlColor = Color.yellow; //选中之后网格颜色
[SerializeField]
private LevelPiece[] _pieces;
public int TotalTime {
get {
return _totalTime; }
set {
_totalTime = value; }
}
public float Gravity {
get {
return _gravity; }
set {
_gravity = value; }
}
public AudioClip Bgm {
get {
return _bgm;}
set {
_bgm = value; }
}
public Sprite Background {
get {
return _background; }
set {
_background = value; }
}
public int TotalColumns
{
get {
return _totalColumns; }
set {
_totalColumns = value; }
}
public int TotalRows
{
get {
return _totalRows; }
set {
_totalRows = value; }
}
public LevelPiece[] Pieces
{
get {
return _pieces; }
set {
_pieces = value; }
}
}
Custom Inspector Code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using RunAndJump;
namespace RunAndJump
{
[CustomEditor(typeof(Level))]
public class LevelInspector : Editor
{
private Level _myTarget;
//在我们对象被选中的时候调用的,常常用于一些初始化的代码
private void OnEnable()
{
Debug.Log("OnEnable");
//为哪个对象写查看器 target就是哪个对象
//target的类型 是Object,需要转换类型
_myTarget = (Level)target;
}
//在对象超出我们操作范围内调用的
//在对象被销毁的时候调用的
//可以用来做一些代码退出前的清理工作(数组清零,变量重置)
private void OnDisable()
{
Debug.Log("OnDisable");
}
//在对象被销毁的时候调用的
private void OnDestroy()
{
Debug.Log("OnDestroy");
}
//绘制查看器GUI的回调函数
//必须是public,为了使自定义的查看起生效 必须重载
public override void OnInspectorGUI()
{
//查看器输出文本
EditorGUILayout.LabelField("这个inspector的GUI被修改了");
//绘制查看器默认效果
//DrawDefaultInspector();
//绘制自定义查看器效果
DrawLevelDataGUI();
}
private void DrawLevelDataGUI()
{
//EditorGUI 和 EditorGUILayout 的主要区别是,EditorGUI需要指定绘制区域,EditorGUILayout会自适应
//绘制标题 样式为加粗
EditorGUILayout.LabelField("Data",EditorStyles.boldLabel);
//绘制整形域 并且关卡时间要大于0
//赋值的原因是 当整形域的里输入的值修改的时候 会使用该值的新的值做判断
_myTarget.TotalTime = EditorGUILayout.IntField("TotalTime",Mathf.Max(0,_myTarget.TotalTime));
//绘制浮点域
_myTarget.Gravity = EditorGUILayout.FloatField("Gravity", _myTarget.Gravity);
//绘制对象域
//true的含义是 是否允许场景中的对象添加进来
//ObjectField的返回值就是Object 需要进行类型转换
//背景音乐
_myTarget.Bgm = (AudioClip)EditorGUILayout.ObjectField("BGM", _myTarget.Bgm , typeof(AudioClip) , false);
//背景图片
_myTarget.Background = (Sprite)EditorGUILayout.ObjectField("Background", _myTarget.Background, typeof(Sprite), false);
}
}
}
After the code is executed, the effect is as follows