Version: 6000.3
语言: 中文
使用矢量图形
使用 Painter2D API 创建径向进度指示器

在编辑器和运行时UI中创建饼图

版本: 2023.2+

此示例演示了如何使用矢量API在编辑器和运行时UI中创建图表。

示例概述

此示例在VisualElement,并将其显示在编辑器和运行时UI中。

饼图显示在场景中,饼图显示在编辑器窗口中。
饼图显示在场景中,饼图显示在编辑器窗口中。

您可以在此 GitHub 存储库中找到此示例创建的已完成文件。

先决条件

本指南适用于熟悉 Unity 编辑器、UI 工具包和 C# 脚本的开发人员。在开始之前,请熟悉以下内容:

创建饼图

创建一个使用ArcFill将饼图绘制到视觉元素实例化或派生自 C# 的可视化树的节点VisualElement类。您可以设置外观样式、定义行为并将其作为 UI 的一部分显示在屏幕上。更多信息
请参阅术语表
.

  1. 使用任何模板创建 Unity 项目。
  2. 创建一个名为pie-chart以存储您的文件。
  3. pie-chart文件夹中,创建一个名为PieChart.cs内容如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;

[UxmlElement]
public partial class PieChart : VisualElement
{
    float m_Radius = 100.0f;
    float m_Value = 40.0f;

    public float radius
    {
        get => m_Radius;
        set
        {
            m_Radius = value;
        }
    }

    public float diameter => m_Radius * 2.0f;

    public float value {
        get { return m_Value; }
        set { m_Value = value; MarkDirtyRepaint(); }
    }

    public PieChart()
    {
        generateVisualContent += DrawCanvas;
    }

    void DrawCanvas(MeshGenerationContext ctx)
    {
        var painter = ctx.painter2D;
        painter.strokeColor = Color.white;
        painter.fillColor = Color.white;

        var percentage = m_Value;

        var percentages = new float[] {
            percentage, 100 - percentage
        };
        var colors = new Color32[] {
            new Color32(182,235,122,255),
            new Color32(251,120,19,255)
        };
        float angle = 0.0f;
        float anglePct = 0.0f;
        int k = 0;
        foreach (var pct in percentages)
        {
            anglePct += 360.0f * (pct / 100);

            painter.fillColor = colors[k++];
            painter.BeginPath();
            painter.MoveTo(new Vector2(m_Radius, m_Radius));
            painter.Arc(new Vector2(m_Radius, m_Radius), m_Radius, angle, anglePct);
            painter.Fill();

            angle = anglePct;
        }
    }
}

在编辑器UI中添加饼图

  1. 创建一个名为Editor以存储您的文件。
  2. Editor文件夹中,创建一个名为PieChartWindow.cs包含以下内容:
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

public class PieChartWindow : EditorWindow
{
    [MenuItem("Tools/PieChart Window")]
    public static void ShowExample()
    {
        PieChartWindow wnd = GetWindow<PieChartWindow>();
        wnd.titleContent = new GUIContent("PieChartWindow");
    }

    public void CreateGUI()
    {
        VisualElement root = rootVisualElement;
            
        root.Add(new PieChart());
    }
}

要在编辑器窗口中查看饼图,请从菜单中选择工具>饼图窗口

在运行时 UI 中添加饼图

在 UIDocument 中添加饼图游戏对象Unity 场景中的基本对象,可以表示角色、道具、风景、相机、航路点等。游戏对象的功能由附加到它的组件定义。更多信息
请参阅术语表
在 SampleScene 中。为此,首先,创建一个名为pie-chart文件夹PieChartComponet.cs内容如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;

[RequireComponent(typeof(UIDocument))]
public class PieChartComponent : MonoBehaviour
{
    PieChart m_PieChart;

    void OnEnable()
    {
        m_PieChart = new PieChart();
        GetComponent<UIDocument>().rootVisualElement.Add(m_PieChart);
    }
}

要在运行时查看饼图,请执行以下作:

  1. 在 SampleScene 中,选择 游戏对象 > UI 工具包 > UI 文档
  2. 在层次结构中选择 UIDocument 游戏对象。
  3. PieChartComponet.cs添加为 UIDocument GameObject 的组件。
  4. 进入播放模式以查看饼图场景场景包含游戏的环境和菜单。将每个唯一的场景文件视为一个独特的关卡。在每个场景中,你放置你的环境、障碍物和装饰品,基本上是将你的游戏设计和构建成碎片。更多信息
    请参阅术语表
    .

其他资源

使用矢量图形
使用 Painter2D API 创建径向进度指示器