1
This commit is contained in:
84
Src/Unity/Scripts/UX/Library/ProgressBlock.cs
Normal file
84
Src/Unity/Scripts/UX/Library/ProgressBlock.cs
Normal file
@@ -0,0 +1,84 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace BITKit.UX
|
||||
{
|
||||
public class ProgressBlock : VisualElement
|
||||
{
|
||||
public new class UxmlTraits : VisualElement.UxmlTraits
|
||||
{
|
||||
private readonly UxmlIntAttributeDescription m_ValueAttribute = new ()
|
||||
{
|
||||
name = "Value",defaultValue = 50
|
||||
};
|
||||
private readonly UxmlIntAttributeDescription m_SeparateAttribute = new ()
|
||||
{
|
||||
name = "Separate",defaultValue = 5
|
||||
};
|
||||
|
||||
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
|
||||
{
|
||||
base.Init(ve, bag, cc);
|
||||
var x = (ProgressBlock)ve;
|
||||
x.Value = m_ValueAttribute.GetValueFromBag(bag, cc);
|
||||
x.Separate = m_SeparateAttribute.GetValueFromBag(bag, cc);
|
||||
}
|
||||
}
|
||||
public ProgressBlock() : base()
|
||||
{
|
||||
|
||||
}
|
||||
public new class UxmlFactory : UxmlFactory<ProgressBlock, UxmlTraits> { }
|
||||
/// <summary>
|
||||
/// 值,默认50,范围0-100
|
||||
/// </summary>
|
||||
public int Value
|
||||
{
|
||||
get => value;
|
||||
set
|
||||
{
|
||||
this.value = value;
|
||||
Rebuild();
|
||||
}
|
||||
}
|
||||
private int value;
|
||||
/// <summary>
|
||||
/// 分割线,默认5
|
||||
/// </summary>
|
||||
public int Separate
|
||||
{
|
||||
get => separate;
|
||||
set
|
||||
{
|
||||
this.separate = value;
|
||||
Rebuild();
|
||||
}
|
||||
}
|
||||
private int separate;
|
||||
private void Rebuild()
|
||||
{
|
||||
Clear();
|
||||
//if value is 58
|
||||
for (var i = 1; i <= Separate; i++)
|
||||
{
|
||||
var block = this.Create<VisualElement>();
|
||||
block.style.flexGrow = 1;
|
||||
|
||||
if (i * 10 < Value)
|
||||
{
|
||||
block.style.opacity = 1;
|
||||
}else if ((i + 1) * 10 > value)
|
||||
{
|
||||
block.style.opacity = 0.5f;
|
||||
}
|
||||
else
|
||||
{
|
||||
block.style.opacity = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user