基于unity line render的五维属性雷达图效果

基于unity line render的五维属性雷达图效果_第1张图片
基于unity line render的五维属性雷达图效果_第2张图片
挂在带有LineRender的GameObject上就可以了
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[ExecuteInEditMode]
public class AttrLine : MonoBehaviour
{
[Range(0, 1)]
public float[] numbers;
public float size = 512;
public bool realTimeUpdate = false;
public bool clampMax = true;
LineRenderer line;
void Start()
{
line = GetComponent();
line.loop = true;
}
void Update()
{
if (realTimeUpdate)
Refresh();
}

public void Refresh()
{
    int pointCount = numbers.Length;
    Vector3[] positions = new Vector3[pointCount];
    float offsetAngle = 360f / pointCount;
    line.positionCount = pointCount;
    for (int i = 0; i < pointCount; i++)
    {
        positions[i] = Quaternion.AngleAxis(i * offsetAngle, Vector3.back) * Vector2.up;
        float value;
        if (clampMax)
            value = Mathf.Clamp(numbers[i], 0, 1);
        else
            value = Mathf.Clamp(numbers[i], 0, int.MaxValue);
        positions[i] *= value * size;
        positions[i] += transform.position;
    }
    line.SetPositions(positions);

}

你可能感兴趣的:(unity)