【Unity之IMGUI】—位置信息类和控件基类的封装

在这里插入图片描述


‍个人主页:@元宇宙-秩沅

‍ hallo 欢迎 点赞 收藏⭐ 留言 加关注✅!

本文由 秩沅 原创

‍ 收录于专栏

️IMGUI封装实践



文章目录

    • ️IMGUI封装实践
    • 前言
    • (==A==) UI中的九宫格原理
    • (==B==) 位置信息类UML
    • (==C==) 控件基类的封装创建


前言

【Unity之IMGUI】—位置信息类和控件基类的封装_第1张图片

缺点1:无法在编译过程进行可视化调整
缺点2:无法分辨率自适应

【Unity之IMGUI】—位置信息类和控件基类的封装_第2张图片


A UI中的九宫格原理


【Unity之IMGUI】—位置信息类和控件基类的封装_第3张图片

在这里插入图片描述


B 位置信息类UML


作用:让控件根据调整对齐
【Unity之IMGUI】—位置信息类和控件基类的封装_第4张图片
【Unity之IMGUI】—位置信息类和控件基类的封装_第5张图片

  • 最终代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
//___________项目:    
//___________功能:位置信息类 的封装   
//___________创建者:秩沅_______________
//_____________________________________
//-------------------------------------

/// 
/// 选择对齐方式的枚举
/// 
public enum E_Alignment
{
    Up,
    Down,
    Left,
    Right,
    Center,
    Left_Up,
    Left_Down,
    Right_Up,
    Right_Down
}

[System.Serializable] //让
/// 
/// 作用:为完成分辨率的相关计算
/// 
public class PosInformation
{   
    //-----------
    //可视化部分
    //-----------
    public Vector2 posOffset;   //控件的偏移量
    public float width = 100;
    public float height = 50;   //控件的宽高
    //-----------
    //私有部分
    //-----------
    private E_Alignment screen_Alignemnt;   //屏幕的对齐类型
    private E_Alignment contorl_Alignment;  //控件的对齐类型
    private Vector2 centerPos;              //控件的基点(中心点)
    private Rect CorPos = new Rect(0, 0, 100, 100);  //控件的最终坐标
    //属性
    public Rect LastPos
    {
        get
        {
            MutiCenterPos();
            MutiLastPos();   //进行坐标位置的最终计算

            CorPos.width = width;
            CorPos.height = height;
            //宽高进行赋值        
            return CorPos;
        }                    //返回经过计算完毕的控件位置信息Rcet类型
    }

    /// 
    /// 控件中心点偏移的方法计算
    /// 
    private  void MutiCenterPos()
    {
        switch (contorl_Alignment)
        {
            case E_Alignment.Up:
                centerPos.x = width / 2;
                centerPos.y = 0;
                break;
            case E_Alignment.Down:
                centerPos.x = width / 2;
                centerPos.y = height;
                break;
            case E_Alignment.Left:
                centerPos.x = 0;
                centerPos.y = height / 2;
                break;
            case E_Alignment.Right:
                centerPos.x = width;
                centerPos.y = height / 2;
                break;
            case E_Alignment.Center:
                centerPos.x = width / 2;
                centerPos.y = height / 2;
                break;
            case E_Alignment.Left_Up:
                centerPos.x = 0;
                centerPos.y = 0;
                break;
            case E_Alignment.Left_Down:
                centerPos.x = 0;
                centerPos.y = height;
                break;
            case E_Alignment.Right_Up:
                centerPos.x = width;
                centerPos.y = 0;
                break;
            case E_Alignment.Right_Down:
                centerPos.x = width;
                centerPos.y = height;
                break;
            default:
                break;
        }
    }

    /// 
    /// 控件的最终位置计算
    /// 
    private void MutiLastPos()
    {
        switch (screen_Alignemnt )
        {
            case E_Alignment.Up:
                CorPos.x = Screen.width / 2 + centerPos.x + posOffset.x;
                CorPos.y = 0 + centerPos.y + posOffset.y;
                break;
            case E_Alignment.Down:
                CorPos.x = Screen.width / 2 + centerPos.x + posOffset.x;
                CorPos.y = Screen.height + centerPos.y - posOffset.y;
                break;
            case E_Alignment.Left:
                CorPos .x = centerPos.x + posOffset.x;
                CorPos.y = Screen.height / 2 + centerPos.y + posOffset.y;
                break;
            case E_Alignment.Right:
                CorPos.x = Screen.width + centerPos.x - posOffset.x ;
                CorPos.y = Screen.height / 2 + centerPos.y + posOffset.y ;
                break;
            case E_Alignment.Center:
                CorPos.x = Screen.width / 2 + centerPos.x + posOffset.x;
                CorPos.y = Screen.height / 2 + centerPos.y + posOffset.y;
                break;
            case E_Alignment.Left_Up:
                CorPos.x = centerPos.x + posOffset.x;
                CorPos.y = centerPos.y + posOffset.y;
                break;
            case E_Alignment.Left_Down:
                CorPos.x = centerPos.x + posOffset.x;
                CorPos.y = Screen.height + centerPos.y - posOffset.y; 
                break;
            case E_Alignment.Right_Up:
                CorPos.x = Screen.width + centerPos.x - posOffset.x;
                CorPos.y =  centerPos.y + posOffset.y;
                break;
            case E_Alignment.Right_Down:
                CorPos.x = Screen.width + centerPos.x - posOffset.x;
                CorPos.y = Screen.height + centerPos.y - posOffset.y;
                break;
            default:
                break;
        }
    }
}


C 控件基类的封装创建


【Unity之IMGUI】—位置信息类和控件基类的封装_第6张图片

特点:
类是抽象类:原因基类不需修改子类需要修改
两个抽象方法:原因不同控件的自定义类型不同,所以需要被重写

【Unity之IMGUI】—位置信息类和控件基类的封装_第7张图片

  • 最终代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
//___________项目:       ______________
//___________功能: 控件基类的创建
//___________创建者:秩沅_______________
//_____________________________________
//-------------------------------------

/// 
    /// 自定义样式枚举选择(开关)
    /// 
public enum switchStyle
{
    on,
    off
}
public abstract class ControlFather : MonoBehaviour
{
    //---------
    //可视化部分
    //---------
    public PosInformation ContorlPosition; //位置信息
    public GUIContent ContorlContent;      //内容信息
    public GUIStyle ContorlStyle;          //自定义信息
    public switchStyle onOrOff = switchStyle.off; //默认是关闭

    /// 
    /// 判断自定义开关
    /// 
    public void Judge()
    {
        switch (onOrOff)
        {
            case switchStyle.on:
                OnDrawstyle(); 
                break;
            case switchStyle.off:
                OffDrawStyle();
                break;
            default:
                break;
        }
    }

    protected abstract void OffDrawStyle(); //关闭时执行的行为
    protected abstract void OnDrawstyle();  //开启时执行的行为

}


⭐【Unityc#专题篇】之c#进阶篇】

⭐【Unityc#专题篇】之c#核心篇】

⭐【Unityc#专题篇】之c#基础篇】

⭐【Unity-c#专题篇】之c#入门篇】

【Unityc#专题篇】—进阶章题单实践练习

⭐【Unityc#专题篇】—基础章题单实践练习

【Unityc#专题篇】—核心章题单实践练习


你们的点赞 收藏⭐ 留言 关注✅是我持续创作,输出优质内容的最大动力!


在这里插入图片描述


你可能感兴趣的:(#,UnityGUI篇,unity,游戏引擎,c#,移动开发)