👨💻个人主页:@元宇宙-秩沅
👨💻 hallo 欢迎 点赞👍 收藏⭐ 留言📝 加关注✅!
👨💻 本文由 秩沅 原创
👨💻 收录于专栏:UnityUI篇实战
⭐🅰️IMGUI封装实践【三】⭐
⭐前言⭐
🎶(A) 控件创建及其封装——标签
-
UML类图
-
封装代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
//-------------------------------------
//—————————————————————————————————————
//___________项目: ______________
//___________功能: 标签的创建
//___________创建者:秩沅_______________
//_____________________________________
//-------------------------------------
public class Label : ControlFather
{
protected override void OffDrawStyle()
{
GUI.Label(ContorlPosition.LastPos ,ContorlContent );
}
protected override void OnDrawstyle()
{
GUI.Label(ContorlPosition.LastPos, ContorlContent ,ContorlStyle);
}
}
🎶(B) 控件创建及其封装——按钮
- 按钮封装代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
//-------------------------------------
//—————————————————————————————————————
//___________项目: ______________
//___________功能: 创建按钮
//___________创建者:秩沅_______________
//_____________________________________
//-------------------------------------
public class Button : ControlFather
{
public event UnityAction triggerEvent;
//事件的添加在此起到妙用
//只要在外部给予响应函数
protected override void OffDrawStyle()
{
if(GUI.Button(ContorlPosition.LastPos ,ContorlContent))
{
triggerEvent?.Invoke();
}
}
protected override void OnDrawstyle()
{
if(GUI.Button(ContorlPosition.LastPos, ContorlContent,ContorlStyle ) )
{
triggerEvent?.Invoke();
}
}
}
🎶(C) 控件创建及其封装——多选框
- 封装代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
//-------------------------------------
//—————————————————————————————————————
//___________项目: ______________
//___________功能: 多选框功能的封装
//___________创建者:秩沅_______________
//_____________________________________
//-------------------------------------
public class ToggleM : ControlFather
{
public bool IsSwitch;
private bool previousSwitch;
public event UnityAction<bool> triggerEvent;
protected override void OffDrawStyle()
{
IsSwitch = GUI.Toggle(ContorlPosition.LastPos, IsSwitch, ContorlContent);
if(IsSwitch != previousSwitch ) //当上次开关状态 和 本次开关状态不一样时
{
triggerEvent?.Invoke( IsSwitch );
previousSwitch = IsSwitch; //更新上一次的开关状态
}
}
protected override void OnDrawstyle()
{
IsSwitch = GUI.Toggle(ContorlPosition.LastPos, IsSwitch, ContorlContent,ContorlStyle );
if (IsSwitch != previousSwitch)
{
triggerEvent?.Invoke(IsSwitch);
previousSwitch = IsSwitch;
}
}
}
🎶(D) 控件创建及其封装——单选框
-
UML类图
-
封装代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
//___________项目:
//___________功能: 单选框功能的创建
//___________创建者:秩沅_______________
//_____________________________________
//-------------------------------------
[ExecuteAlways ]
public class ToggleS : MonoBehaviour
{
public ToggleM[] toggles;
private ToggleM previousToggle;
//此时只需放在start里面,而不需要放在持续帧的生命周期里的原因是,将方法放进事件中只需一次
void Start()
{
if (toggles.Length == 0) return;
foreach (ToggleM item1 in toggles )
{
item1.triggerEvent += (swtich) =>
{
//如果传入的开关是开的,那么其他全部都设为关
if(swtich)
{
foreach (ToggleM item2 in toggles)
{
item2.IsSwitch = item2 != item1 ? false : true ;
}
previousToggle = item1;
}
//如果当前是开的其他全部是关的,就不允许它变成关(保证至少有一个开启)
else if(item1 == previousToggle )
{
item1.IsSwitch = true;
}
};
}
}
// Update is called once per frame
void Update()
{
}
}
🎶(E) 控件创建及其封装——输入框
- 封装代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
//-------------------------------------
//—————————————————————————————————————
//___________项目: ______________
//___________功能:输入框控件的封装创建
//___________创建者:秩沅_______________
//_____________________________________
//-------------------------------------
public class InputFram : ControlFather
{
public event UnityAction<string> triggerEvent; //和单选框控件一样
private string previousText = "";
protected override void OffDrawStyle()
{
ContorlContent.text = GUI.TextField(ContorlPosition .LastPos ,ContorlContent.text);
if(previousText != ContorlContent.text )
{
triggerEvent?.Invoke(ContorlContent.text);
previousText = ContorlContent.text;
}
}
protected override void OnDrawstyle()
{
ContorlContent.text = GUI.TextField(ContorlPosition.LastPos, ContorlContent.text,ContorlStyle );
if (previousText != ContorlContent.text)
{
triggerEvent?.Invoke(ContorlContent.text);
previousText = ContorlContent.text;
}
}
}
🎶(F) 控件创建及其封装——拖动条
- 封装代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
//-------------------------------------
//—————————————————————————————————————
//___________项目: ______________
//___________功能: 滚动条控件的封装创建
//___________创建者:秩沅_______________
//_____________________________________
//-------------------------------------
public enum E_Diretioon //拖动条的方向
{
Horizontal,
Vertical
}
public class Slider : ControlFather
{
public float MaxValue = 1 ; //拖动条的最大值
public float MinValue = 0 ; //拖动条的最小值
public float NowValue = 0.5f; //拖动条的当前值
public GUIStyle smallButton; //小按钮自定义样式,当自定义样式打开后,拖动条要和小按钮的样式一同重载
public event UnityAction<float> triggerEvent;
public E_Diretioon sliderDirecion = E_Diretioon.Vertical;
private float previousValue;
protected override void OffDrawStyle()
{
switch (sliderDirecion)
{
case E_Diretioon.Horizontal:
NowValue = GUI.HorizontalSlider(ContorlPosition.LastPos,NowValue,MinValue ,MaxValue );
break;
case E_Diretioon.Vertical:
NowValue = GUI.VerticalSlider(ContorlPosition.LastPos, NowValue, MinValue, MaxValue);
break;
default:
break;
}
if(previousValue != NowValue )
{
triggerEvent?.Invoke(NowValue);
}
}
protected override void OnDrawstyle()
{
switch (sliderDirecion)
{
case E_Diretioon.Horizontal:
NowValue = GUI.HorizontalSlider(ContorlPosition.LastPos, NowValue, MinValue, MaxValue,ContorlStyle,smallButton );
break;
case E_Diretioon.Vertical: //没有单个拖动条自定义样式的重载,要和小按钮的一起重载
NowValue = GUI.VerticalSlider(ContorlPosition.LastPos, NowValue, MinValue, MaxValue,ContorlStyle,smallButton );
break;
default:
break;
}
if (previousValue != NowValue)
{
triggerEvent?.Invoke(NowValue);
}
}
}
🎶(G) 控件创建及其封装——图片
-
GUI.DrawTextrrue 没有自定义样式的重载
-
封装代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
//___________项目: ______________
//___________功能: 图片空间的封装创建
//___________创建者:秩沅_______________
//_____________________________________
//-------------------------------------
public class TextureIMGUI : ControlFather
{
public ScaleMode scaleMode = ScaleMode.StretchToFill; //保持比例
protected override void OffDrawStyle()
{
GUI.DrawTexture(ContorlPosition.LastPos,ContorlContent .image ,scaleMode);
}
protected override void OnDrawstyle()
{
GUI.DrawTexture(ContorlPosition.LastPos, ContorlContent.image, scaleMode);
}
}
⭐🅰️⭐
缺点1:无法在编译过程进行可视化调整
缺点2:无法分辨率自适应
⭐【Unityc#专题篇】之c#进阶篇】
⭐【Unityc#专题篇】之c#核心篇】
⭐【Unityc#专题篇】之c#基础篇】
⭐【Unity-c#专题篇】之c#入门篇】
⭐【Unityc#专题篇】—进阶章题单实践练习
⭐【Unityc#专题篇】—基础章题单实践练习
⭐【Unityc#专题篇】—核心章题单实践练习
你们的点赞👍 收藏⭐ 留言📝 关注✅是我持续创作,输出优质内容的最大动力!、文章来源:https://www.toymoban.com/news/detail-560860.html
文章来源地址https://www.toymoban.com/news/detail-560860.html
到了这里,关于【Unity之IMGUI脚本封装】—自定义常用控件的封装(即拿即用)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!