Unity3D利用UGUI制作多边形雷达图

14 篇文章 0 订阅

  在游戏开发中,我们经常会使用多维图来表示人物的属性。比如三国志中,通过五维图,我们可以清晰地看到武将的属性。

  如果我们自己的游戏需要用多边形雷达图来表示相似的属性,则需要针对UGUI进行一些处理。首先需要获取到Mesh网格和所在的CanvasRenderer渲染,以及Material材质球,从而描绘出完整的多边形。

 /// <summary>
    /// 设置点位
    /// </summary>
    void SetVertices()
    {
        int count = element.Length + 1;
        canvas = GetComponent<CanvasRenderer>();
        mesh = new Mesh();
        vertices = new Vector3[count];
        var uvs = new Vector2[count];
        triangles = new int[count * 3];
        var tangents = new Vector4[count];
        Vector4 tangent = new Vector4(0, 1, 0, -1);
        allAngle = (float)360 / (count - 1);

        for (int i = 0; i < vertices.Length; i++)   //循环顶点
        {
            vertices[i] = Quaternion.Euler(0, 0, allAngle * i) * Vector3.up * sideLength;
            uvs[i] = Quaternion.Euler(0, 0, allAngle * i) * Vector3.up;
            tangents[i] = tangent;
        }

        //为mesh的顶点赋值
        for (int i = 0, index = 0; i < count - 1; i++, index += 3)
        {
            triangles[index + 0] = 0;
            triangles[index + 1] = i == count - 2 ? 1 : 2 + i;//倒数第二
            triangles[index + 2] = i == count - 2 ? count - 1 : 1 + i;//最后一个
        }

        mesh.vertices = vertices;
        mesh.triangles = triangles;
        mesh.RecalculateNormals();
        mesh.tangents = tangents;
        mesh.uv = uvs;

        canvas.SetMesh(mesh);
        canvas.SetMaterial(material, null);
    }

  通过求取夹角,传入不同的维度值(注意这里长度最小需要取3),我们可以得到如下不同维度的雷达图 :

   最后通过调整不同的值,实现多边形雷达图的变化:

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

public class RadarMap : MonoBehaviour
{
    private int[] triangles;
    private Vector3[] vertices;

    [Header("值域")]
    public float[] element = new float[5] { 10, 10, 10, 10, 10 };

    private float sideLength = 10;
    private float allAngle = 0;

    private CanvasRenderer canvas;
    [SerializeField]
    private Material material;
    private Mesh mesh;

    //整体缩放
    public float scale;


    //[Header("描边:这块暂时没开发完")]
    //public bool isRander = false;
    //LineRenderer lineRenderer;

    void Start()
    {
        SetVertices();

        //if (isRander)
        //{
        //    lineRenderer = transform.gameObject.AddComponent<LineRenderer>();

        //    lineRenderer.material = renderMaterial;
        //    lineRenderer.startWidth = 1;
        //    lineRenderer.endWidth = 1;

        //    lineRenderer.positionCount = element.Length + 1;
        //}

    }
 
    private void Update()
    {
        for (int i = 1; i < vertices.Length; i++)
        {
            sideLength = element[i-1] * scale;
            vertices[i] = Quaternion.Euler(0, 0, allAngle * i) * Vector3.up * sideLength;            
        }

        mesh.vertices = vertices;
        canvas.SetMesh(mesh);

        //if(isRander)
        //{
        //    SetLinePositions(vertices);
        //}
    }

    //public void SetLinePositions(Vector3[] vector3s)
    //{
    //    Vector3[] v3 = (Vector3[])vector3s.Clone();

    //    v3[0] = v3[v3.Length - 1];
    //    //for (var i = 0; i < v3.Length; i++)
    //    //    v3[i] += Vector3.up * 960 + Vector3.right * 540;

    //    lineRenderer.SetPositions(v3);
    //}


    /// <summary>
    /// 设置点位
    /// </summary>
    void SetVertices()
    {
        int count = element.Length + 1;
        canvas = GetComponent<CanvasRenderer>();
        mesh = new Mesh();
        vertices = new Vector3[count];
        var uvs = new Vector2[count];
        triangles = new int[count * 3];
        var tangents = new Vector4[count];
        Vector4 tangent = new Vector4(0, 1, 0, -1);
        allAngle = (float)360 / (count - 1);

        for (int i = 0; i < vertices.Length; i++)   //循环顶点
        {
            vertices[i] = Quaternion.Euler(0, 0, allAngle * i) * Vector3.up * sideLength;
            uvs[i] = Quaternion.Euler(0, 0, allAngle * i) * Vector3.up;
            tangents[i] = tangent;
        }

        //为mesh的顶点赋值
        for (int i = 0, index = 0; i < count - 1; i++, index += 3)
        {
            triangles[index + 0] = 0;
            triangles[index + 1] = i == count - 2 ? 1 : 2 + i;//倒数第二
            triangles[index + 2] = i == count - 2 ? count - 1 : 1 + i;//最后一个
        }

        mesh.vertices = vertices;
        mesh.triangles = triangles;
        mesh.RecalculateNormals();
        mesh.tangents = tangents;
        mesh.uv = uvs;

        canvas.SetMesh(mesh);
        canvas.SetMaterial(material, null);
    }
}

  最终实现的效果图如下:

 

  关于该示例demo,我已经上传到我的资源中,有兴趣可以下载自己实验。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现一个item列表可以通过 Unity3D 中的UGUI组件来完成,以下是一个简单的实现步骤: 1. 创建一个ScrollView对象,它会成为你的item列表的容器。 2. 在ScrollView对象下创建一个Content对象,用于放置所有的item。 3. 创建一个item的Prefab,包含你需要显示的元素。 4. 在运行时,动态生成多个item对象,将它们放置在Content对象下,以此来构建item列表。 5. 根据需要,可以对item列表进行滚动、添加或删除item等操作。 具体实现可以参考以下步骤: 1. 创建ScrollView和Content对象 在场景中创建一个空对象,命名为ScrollView。将Canvas组件的Render Mode设置为Screen Space - Overlay,然后将ScrollView对象的RectTransform组件的Anchors和Pivot都设置为(0, 0)。这样,ScrollView对象的左下角就会位于屏幕左下角。在ScrollView对象下创建一个空对象,命名为Content。将Content对象的RectTransform组件的Anchors和Pivot也都设置为(0, 0),以便于它能够与ScrollView对象的位置重合。 2. 创建item的Prefab 在项目资源中创建一个新的Prefab,将你需要显示的元素放入其中。例如,可以在Prefab中添加一个Text对象,用于显示item的标题。确保这个Prefab的RectTransform组件的Anchors和Pivot都设置为(0, 0),以便于在生成item时它们能够正确地布局。 3. 动态生成item对象 在脚本中,使用Instantiate()方法动态生成多个item对象,并将它们作为Content对象的子对象。例如: ```csharp public GameObject itemPrefab; public int itemCount = 20; void Start() { for (int i = 0; i < itemCount; i++) { GameObject item = Instantiate(itemPrefab, content.transform); // 设置item的位置和大小 item.GetComponent<RectTransform>().anchoredPosition = new Vector2(0, -i * item.GetComponent<RectTransform>().rect.height); } } ``` 这段代码会生成20个item对象,将它们放置在Content对象下,并设置它们的位置和大小。这里假设item的高度是固定的。 4. 对item列表进行滚动 为了让item列表能够滚动,需要将ScrollView对象下的Scrollbar组件与Content对象的RectTransform组件相绑定。在ScrollView对象下添加一个Scrollbar组件,将它的Direction设置为Vertical,并将它的Size设置为0.2(或根据需要调整)。然后将Scrollbar组件的Value属性绑定到Content对象的RectTransform组件的anchoredPosition.y属性上。这样,当拖动Scrollbar时,Content对象就会相应地向上或向下滚动。 5. 添加或删除item 如果需要动态地添加或删除item,可以在脚本中使用Instantiate()和Destroy()方法来完成。例如: ```csharp public void AddItem() { GameObject item = Instantiate(itemPrefab, content.transform); // 设置新的item的位置和大小 item.GetComponent<RectTransform>().anchoredPosition = new Vector2(0, -itemCount * item.GetComponent<RectTransform>().rect.height); itemCount++; } public void RemoveItem() { if (itemCount > 0) { Destroy(content.transform.GetChild(itemCount - 1).gameObject); itemCount--; } } ``` 这样,就可以在运行时动态地添加或删除item了。当添加一个新的item时,只需生成一个新的GameObject,并将它放置在Content对象下;当删除一个item时,只需销毁Content对象下的最后一个子对象即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值