unity中Inspector属性面板的扩展二

紧接着上一节

1.在属性面板中扩展进度值滑动条

   其中ChangeInspector脚本中:public float slideValue = 0.8f;

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ChangeInspector : MonoBehaviour {
    private Texture texture;
    // private私有的,受保护,不会在属性面板中出现
    private Rect rectValue;
    private string remark;//备注
    public float slideValue = 0.8f;
    public Texture Texture
    {
        get
        {
            return texture;
        }

        set
        {
            texture = value;
        }
    }

    public Rect RectValue
    {
        get
        {
            return rectValue;
        }

        set
        {
            rectValue = value;
        }
    }

    public string Remark
    {
        get
        {
            return remark;
        }

        set
        {
            remark = value;
        }
    }
}

InspectorEditor脚本中添加;

inspectorObj.slideValue = EditorGUILayout.Slider("进度值", inspectorObj.slideValue,0f,1f); //绘制滑动条

面板结果:

unity中Inspector属性面板的扩展二_第1张图片

2.添加复选框

ChangeInspector脚本中添加:

public bool isOpen = true;//开启

InspectorEditor脚本中添加;

 inspectorObj.isOpen= EditorGUILayout.Toggle("开启", inspectorObj.isOpen);

结果:

unity中Inspector属性面板的扩展二_第2张图片

3.如何在属性面板添加下拉列表

首先新建C#脚本,命名为ToolType

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Tool
{ 
    public enum EDirType
    {
        up,
        down,
        left,
        right
    }
public class ToolType : MonoBehaviour {

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}
}
}

ChangeInspector脚本内容:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Tool;


public class ChangeInspector : MonoBehaviour {
    private Texture texture;
    // private私有的,受保护,不会在属性面板中出现
    private Rect rectValue;
    private string remark;//备注
    public float slideValue = 0.8f;//进度值
    [HideInInspector]
    public bool isOpen = true;//开启
    public EDirType theType = EDirType.up; //下拉列表
    public Texture Texture
    {
        get
        {
            return texture;
        }

        set
        {
            texture = value;
        }
    }

    public Rect RectValue
    {
        get
        {
            return rectValue;
        }

        set
        {
            rectValue = value;
        }
    }

    public string Remark
    {
        get
        {
            return remark;
        }

        set
        {
            remark = value;
        }
    }
}

InspectorEditor脚本内容:

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using Tool;


[CustomEditor(typeof(ChangeInspector))]
public class InspectorEditor : Editor {
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        ChangeInspector inspectorObj = (ChangeInspector)target;
        //绘制贴图槽
        inspectorObj.Texture = EditorGUILayout.ObjectField("选择贴图",inspectorObj.Texture,typeof(Texture),true)as Texture;
        inspectorObj.RectValue = EditorGUILayout.RectField("窗口坐标",inspectorObj.RectValue);
        inspectorObj.Remark = EditorGUILayout.TextField("备注", inspectorObj.Remark);
        //绘制滑动条
        inspectorObj.slideValue = EditorGUILayout.Slider("进度值", inspectorObj.slideValue,0f,1f);
        //复选框
        inspectorObj.isOpen= EditorGUILayout.Toggle("开启", inspectorObj.isOpen);
        inspectorObj.theType= (EDirType)EditorGUILayout.EnumPopup("方向", inspectorObj.theType);


    }
}
结果:

unity中Inspector属性面板的扩展二_第3张图片

你可能感兴趣的:(Unity)