UNITY 手动定制inspector

本文介绍如何使用Unity自定义Inspector来实现对特定组件属性的编辑界面。通过编写自定义编辑器脚本,可以为GameObject上的组件提供更直观的编辑体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

UNITY 手动定制inspector

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

[ExecuteInEditMode]
[CustomEditor(typeof(testxx))]
public class testxxEditor : Editor {

    [SerializeField]
    public float slider_value = 0.3f;

    public bool chk = false;
    SerializedProperty check;

    // Use this for initialization
    void Start () {
        
    }
    void OnEnable()
    {
        check = serializedObject.FindProperty("x");
        Debug.Log("============" + check.floatValue);
    }
    // Update is called once per frame
    void Update () {

    }

    public override void OnInspectorGUI()
    {
        if (GUILayout.Button("hello"))
        {
            //testxx otx = (testxx)target;
//             otx.x = 5.93f;
//             otx.check = false;
            Debug.Log("hello------------");
        }
        slider_value = EditorGUILayout.Slider(slider_value, 0, 1.0f);
        chk = GUILayout.Toggle(chk, new GUIContent("chk"));
        EditorGUILayout.PropertyField(check);
        serializedObject.ApplyModifiedProperties();
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

//[ExecuteInEditMode]
public class testxx : MonoBehaviour {

    [Range(1,10)]
    public float x = 0;

    public string text = "hello";

    public bool check = false;

    public bool[] chks = new bool[10];
    // Use this for initialization
    void Start () {
        
    }
    
    // Update is called once per frame
    void Update () {
        if (check)
        {
            Debug.Log("==================" + x);
        }
    }


}

 将testxx.cs挂到一个物体上,点击该GO,即可看到inspector上定制出来的相关UI

posted on 2017-03-28 17:46 时空观察者9号 阅读(...) 评论(...) 编辑 收藏

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值