10.自定义输入框和拖动条

10.自定义输入框和拖动条


10.1 知识点

创建自定义输入框类,添加输入框文本更改事件和记录老文本信息的字符串,实现Style关和开的绘制抽象方法

//自定义输入框
public class CustomGUIInput : CustomGUIControl
{
    //输入框文本更改事件
    public event UnityAction<string> textChange;

    //记录上一次文本信息
    private string oldStr = "";

    //实现Style关和开的绘制抽象方法

    protected override void StyleOffDraw()
    {
        content.text = GUI.TextField(guiPos.Pos, content.text);
        if(oldStr != content.text)
        {
            textChange?.Invoke(oldStr);
            oldStr = content.text;
        }
    }

    protected override void StyleOnDraw()
    {
        content.text = GUI.TextField(guiPos.Pos, content.text, style);
        if (oldStr != content.text)
        {
            textChange?.Invoke(oldStr);
            oldStr = content.text;
        }
    }
}

创建自定义拖动条,添加拖动条值和事件相关变量,实现Style关和开的绘制抽象方法

//拖动条类型枚举
public enum E_Slider_Type
{
    Horizontal,
    Vertical,
}

//自定义拖动条
public class CustomGUISlider : CustomGUIControl
{
    //最小值
    public float minValue = 0;

    //最大值
    public float maxValue = 1;

    //当前值
    public float nowValue = 0;

    //水平还是竖直样式
    public E_Slider_Type type = E_Slider_Type.Horizontal;

    //小按钮的style
    public GUIStyle styleThumb;

    //拖动条更改事件
    public event UnityAction<float> changeValue;

    //拖动条旧的值
    private float oldValue = 0;

    //实现Style关和开的绘制抽象方法

    protected override void StyleOffDraw()
    {
        switch (type)
        {
            case E_Slider_Type.Horizontal:
                nowValue = GUI.HorizontalSlider(guiPos.Pos, nowValue, minValue, maxValue);
                break;
            case E_Slider_Type.Vertical:
                nowValue = GUI.VerticalSlider(guiPos.Pos, nowValue, minValue, maxValue);
                break;
        }

        if (oldValue != nowValue)
        {
            changeValue?.Invoke(nowValue);
            oldValue = nowValue;
        }
        
    }

    protected override void StyleOnDraw()
    {
        switch (type)
        {
            case E_Slider_Type.Horizontal:
                nowValue = GUI.HorizontalSlider(guiPos.Pos, nowValue, minValue, maxValue, style, styleThumb);
                break;
            case E_Slider_Type.Vertical:
                nowValue = GUI.VerticalSlider(guiPos.Pos, nowValue, minValue, maxValue, style, styleThumb);
                break;
        }

        if (oldValue != nowValue)
        {
            changeValue?.Invoke(nowValue);
            oldValue = nowValue;
        }
    }
}

10.2 知识点代码

CustomGUIInput

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

//自定义输入框
public class CustomGUIInput : CustomGUIControl
{
    //输入框文本更改事件
    public event UnityAction<string> textChange;

    //记录上一次文本信息
    private string oldStr = "";

    //实现Style关和开的绘制抽象方法

    protected override void StyleOffDraw()
    {
        content.text = GUI.TextField(guiPos.Pos, content.text);
        if(oldStr != content.text)
        {
            textChange?.Invoke(oldStr);
            oldStr = content.text;
        }
    }

    protected override void StyleOnDraw()
    {
        content.text = GUI.TextField(guiPos.Pos, content.text, style);
        if (oldStr != content.text)
        {
            textChange?.Invoke(oldStr);
            oldStr = content.text;
        }
    }
}

CustomGUISlider

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

//拖动条类型枚举
public enum E_Slider_Type
{
    Horizontal,
    Vertical,
}

//自定义拖动条
public class CustomGUISlider : CustomGUIControl
{
    //最小值
    public float minValue = 0;

    //最大值
    public float maxValue = 1;

    //当前值
    public float nowValue = 0;

    //水平还是竖直样式
    public E_Slider_Type type = E_Slider_Type.Horizontal;

    //小按钮的style
    public GUIStyle styleThumb;

    //拖动条更改事件
    public event UnityAction<float> changeValue;

    //拖动条旧的值
    private float oldValue = 0;

    //实现Style关和开的绘制抽象方法

    protected override void StyleOffDraw()
    {
        switch (type)
        {
            case E_Slider_Type.Horizontal:
                nowValue = GUI.HorizontalSlider(guiPos.Pos, nowValue, minValue, maxValue);
                break;
            case E_Slider_Type.Vertical:
                nowValue = GUI.VerticalSlider(guiPos.Pos, nowValue, minValue, maxValue);
                break;
        }

        if(oldValue != nowValue)
        {
            changeValue?.Invoke(nowValue);
            oldValue = nowValue;
        }
        
    }

    protected override void StyleOnDraw()
    {
        switch (type)
        {
            case E_Slider_Type.Horizontal:
                nowValue = GUI.HorizontalSlider(guiPos.Pos, nowValue, minValue, maxValue, style, styleThumb);
                break;
            case E_Slider_Type.Vertical:
                nowValue = GUI.VerticalSlider(guiPos.Pos, nowValue, minValue, maxValue, style, styleThumb);
                break;
        }

        if (oldValue != nowValue)
        {
            changeValue?.Invoke(nowValue);
            oldValue = nowValue;
        }
    }
}


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 785293209@qq.com

×

喜欢就点赞,疼爱就打赏