增加圆形进度条

This commit is contained in:
MuHua-123
2025-04-03 17:46:35 +08:00
parent 5917f914ba
commit 7597d7e2ee
13 changed files with 2756 additions and 24 deletions
@@ -0,0 +1,71 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.UIElements;
public class CircularBar : VisualElement {
public new class UxmlFactory : UxmlFactory<CircularBar, UxmlTraits> { }
public new class UxmlTraits : VisualElement.UxmlTraits {
private readonly UxmlFloatAttributeDescription Fill = new() { name = "fill", defaultValue = 0.1f };
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc) {
base.Init(ve, bag, cc);
var circularBar = ve as CircularBar;
circularBar.Fill = Fill.GetValueFromBag(bag, cc);
circularBar.UpdateValue();
}
}
public readonly Material material;
public readonly RenderTexture rt;
public VisualElement visual;
public float Fill { get; set; }
public CircularBar() {
visual = new VisualElement();
visual.style.flexGrow = 1;
hierarchy.Add(visual);
var shader = Shader.Find("MuHua/UI/CircularBar");
if (shader == null) {
Debug.LogError("Failed to find Shader Graphs/CircularBar.");
return;
}
material = new Material(shader);
rt = new RenderTexture(128, 128, 0, RenderTextureFormat.ARGBFloat);
Background background = Background.FromRenderTexture(rt);
visual.style.backgroundImage = new StyleBackground(background);
}
public void UpdateValue(float fill) {
Fill = fill;
UpdateValue();
}
public void UpdateValue() {
if (material == null) { return; }
Texture2D texture = resolvedStyle.backgroundImage.texture;
if (texture == null) { return; }
material.SetFloat("_fill", Fill);
CommandBuffer command = CommandBufferPool.Get();
// 设置渲染目标为tempRTHandle
CoreUtils.SetRenderTarget(command, rt);
// 清除纹理内容
CoreUtils.ClearRenderTarget(command, ClearFlag.All, Color.clear);
// 渲染
command.Blit(texture, rt, material, 0);
Graphics.ExecuteCommandBuffer(command);
CommandBufferPool.Release(command);
Background background = Background.FromRenderTexture(rt);
visual.style.backgroundImage = new StyleBackground(background);
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: dd9bb527169c05848b8946bc93645079
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+15 -2
View File
@@ -1,3 +1,16 @@
{
"name": "MuHua.UITool"
}
"name": "MuHua.UITool",
"rootNamespace": "",
"references": [
"GUID:df380645f10b7bc4b97d4f5eb6303d95"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}