大家好,又见面了,我是你们的朋友全栈君。
示例展示
前置准备
- 创建脚本,例 SimpleCharacterPanel.cs
- 添加Odin库using指令
using Sirenix.OdinInspector;
需求分析
我们可以将面版主要分为如下四个部分:
- 角色基础信息
- 名称
- 等级
- 经验值
- 角色模型
- 角色属性
- 经验上限(升级所需经验)
- 攻击力
- 血量上限
- 蓝量上限
- 血条
- 蓝条
- 模拟操作(金手指)
- 经验值增加
- 其他
- 初始化
- 等级提升
#region 属性
public string m_CharacterName = "银色大蜥蜴";
public int m_Level;
public int m_Exp;
public Object m_CharacterModel;
public int m_ExpLimit;
public int m_ATK;
public int m_MaxHP;
public int m_MaxSP;
public int m_HP;
public int m_SP;
private void _Get100Exp()
{
int ExpCount = m_Exp + 100;
while (ExpCount >= m_ExpLimit)
{
ExpCount -= m_ExpLimit;
_LevelUp();
}
m_Exp = ExpCount;
}
private void _LevelUp()
{
m_Level++;
m_ExpLimit = m_Level * m_Level + 100;
m_ATK = m_Level * m_Level * 3;
m_MaxHP = m_Level * m_Level * 20;
m_MaxSP = m_Level * m_Level * 5;
m_HP = m_MaxHP;
m_SP = m_MaxSP;
}
private void _Init()
{
m_Level = 1;
m_ExpLimit = 100;
m_Exp = 0;
m_ATK = 1;
m_MaxHP = 50;
m_MaxSP = 10;
m_HP = m_MaxHP;
m_SP = m_MaxSP;
m_CharacterModel = this.gameObject;
}
#endregion
面版改造
基础信息部分
-
显示改造
- 使用[DisplayAsString]去除文本框,只显示文本
- 使用[ProgressBar]以进度条形式显示经验值
- 第一个参数为进度最小值,此处为0
- 第二个参数为最大值,此处依赖 m_ExpLimit 字段的值
- 后三位分别为进度条颜色的RGB值
- 使用[PreviewField]显示模型预览
[DisplayAsString] public string m_CharacterName = "银色大蜥蜴"; [DisplayAsString] public int m_Level; [ProgressBar(0, "m_ExpLimit", 0, 0.8f, 0)] public int m_Exp; [PreviewField(ObjectFieldAlignment.Left)] public Object m_CharacterModel;
-
Label改造
- 使用[HideLabel]隐藏标签
- 使用[LabelWidth]设置标签宽度
-
布局改造
-
使用[HorizontalGroup]使名称和等级设置在同一水平区域
[HorizontalGroup("名称等级")] [HideLabel] [DisplayAsString] public string m_CharacterName = "银色大蜥蜴"; [HorizontalGroup("名称等级")] [LabelWidth(40)] [DisplayAsString] public int m_Level; [LabelWidth(50)] [ProgressBar(0, "m_ExpLimit", 0, 0.8f, 0)] public int m_Exp; [HideLabel] [PreviewField(ObjectFieldAlignment.Left)] public Object m_CharacterModel;
-
使用[VerticalGroup]使上一步的组合与经验值设置在同一垂直区域
[HorizontalGroup("文本信息/名称等级")] [HideLabel] [DisplayAsString] public string m_CharacterName = "银色大蜥蜴"; [HorizontalGroup("文本信息/名称等级")] [LabelWidth(40)] [DisplayAsString] public int m_Level; [VerticalGroup("文本信息")] [LabelWidth(50)] [ProgressBar(0, "m_ExpLimit", 0, 0.8f, 0)] public int m_Exp; [HideLabel] [PreviewField(ObjectFieldAlignment.Left)] public Object m_CharacterModel;
-
再次使用[HorizontalGroup]使上步组合和模型预览处在同一水平区域
[HorizontalGroup("基础信息/文本信息/名称等级")] [HideLabel] [DisplayAsString] public string m_CharacterName = "银色大蜥蜴"; [HorizontalGroup("基础信息/文本信息/名称等级")] [LabelWidth(40)] [DisplayAsString] public int m_Level; [VerticalGroup("基础信息/文本信息")] [LabelWidth(50)] [ProgressBar(0, "m_ExpLimit", 0, 0.8f, 0)] public int m_Exp; [HorizontalGroup("基础信息")] [HideLabel] [PreviewField(ObjectFieldAlignment.Right)] public Object m_CharacterModel;
属性信息部分
-
显示改造
-
使用[DisplayAsString]去除文本框,只显示文本
-
-
布局改造
-
使用[FoldoutGroup]将其收录在一个可折叠的框中
[FoldoutGroup("角色属性"), DisplayAsString] public int m_ExpLimit; [FoldoutGroup("角色属性"), DisplayAsString] public int m_ATK; [FoldoutGroup("角色属性"), DisplayAsString] public int m_MaxHP; [FoldoutGroup("角色属性"), DisplayAsString] public int m_MaxSP;
-
血条蓝条
[ProgressBar(0, "m_MaxHP", 0.8f, 0, 0)] public int m_HP; [ProgressBar(0, "m_MaxSP", 0.1f, 1, 0.9f)] public int m_SP;
模拟操作的按钮
-
在函数前使用[Button]在Inspector界面绘制一个调用按钮
-
使用[Title]绘制一个标题
[Title("模拟操作"), Button("加100经验")] private void _Get100Exp() { int ExpCount = m_Exp + 100; while (ExpCount >= m_ExpLimit) { ExpCount -= m_ExpLimit; _LevelUp(); } m_Exp = ExpCount; }
在Inspector初始化时初始化字段
[OnInspectorInit("_Init")] private void _Init() { m_Level = 1; m_ExpLimit = 100; m_Exp = 0; m_ATK = 1; m_MaxHP = 50; m_MaxSP = 10; m_HP = m_MaxHP; m_SP = m_MaxSP; m_CharacterModel = this.gameObject; }
完整代码
using Sirenix.OdinInspector; using System.Collections; using System.Collections.Generic; using UnityEngine; public class SimpleCharacterPanel : MonoBehaviour { #region 完整Demo [OnInspectorInit("_Init")] [HorizontalGroup("CharacterBaseInfo/Info/Base", Width = 80), HideLabel, DisplayAsString] public string m_CharacterName = "银色大蜥蜴"; [HorizontalGroup("CharacterBaseInfo/Info/Base", Width = 200, MinWidth = 100), LabelWidth(40), DisplayAsString] public int m_Level; [VerticalGroup("CharacterBaseInfo/Info"), LabelWidth(50), ProgressBar(0, "m_ExpLimit", 0, 0.8f, 0)] public int m_Exp; [HorizontalGroup("CharacterBaseInfo", Width = 50), HideLabel, PreviewField(ObjectFieldAlignment.Left)] public Object m_CharacterModel; [FoldoutGroup("角色属性"), DisplayAsString] public int m_ExpLimit; [FoldoutGroup("角色属性"), DisplayAsString] public int m_ATK; [FoldoutGroup("角色属性"), DisplayAsString] public int m_MaxHP; [FoldoutGroup("角色属性"), DisplayAsString] public int m_MaxSP; [ProgressBar(0, "m_MaxHP", 0.8f, 0, 0)] public int m_HP; [ProgressBar(0, "m_MaxSP", 0.1f, 1, 0.9f)] public int m_SP; [Title("模拟操作"), PropertySpace(30), Button("加100经验")] private void _Get100Exp() { int ExpCount = m_Exp + 100; while (ExpCount >= m_ExpLimit) { ExpCount -= m_ExpLimit; _LevelUp(); } m_Exp = ExpCount; } private void _LevelUp() { m_Level++; m_ExpLimit = m_Level * m_Level + 100; m_ATK = m_Level * m_Level * 3; m_MaxHP = m_Level * m_Level * 20; m_MaxSP = m_Level * m_Level * 5; m_HP = m_MaxHP; m_SP = m_MaxSP; } private void _Init() { m_Level = 1; m_ExpLimit = 100; m_Exp = 0; m_ATK = 1; m_MaxHP = 50; m_MaxSP = 10; m_HP = m_MaxHP; m_SP = m_MaxSP; m_CharacterModel = this.gameObject; } #endregion }
-
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/162825.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...