Image
图片挂载材质球来贴图
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
public class MeshImage : MaskableGraphic
{
public float[] arr;
//精灵
public Sprite activeSprite;
public float m = 1;
//纹理
public override Texture mainTexture
{
get
{
return material.mainTexture;
}
}
protected override void OnPopulateMesh(VertexHelper vh)
{
if (arr.Length < 3)
{
base.OnPopulateMesh(vh);
return;
}
float w = rectTransform.sizeDelta.x;
float h = rectTransform.sizeDelta.y;
//数组最大值
float max = arr.Max();
float p = (w < h ? ((w / 2) / max) : ((h / 2) / max));
vh.Clear();
//每个角的弧度
float angle = 2 * Mathf.PI / arr.Length;
//圆心
vh.AddVert(Vector3.zero, color, new Vector2(0.5f, 0.5f));
for (int i = 0; i < arr.Length; i++)
{
float x = Mathf.Sin(i * angle) * arr[i];
float y = Mathf.Cos(i * angle) * arr[i];
float uvx = (x + w / 2) / w;//坐标
float uvy = (y + h / 2) / h;//坐标
vh.AddVert(new Vector3(x, y, 0), color, new Vector2(uvx, uvy));
if (i == 0)
{
vh.AddTriangle(0, arr.Length, 1);
}
else
{
vh.AddTriangle(0, i, i + 1);
}
}
}
}
Btn
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Btn : MonoBehaviour
{
public Button btn;
public MeshImage MeshImage;
private void Start()
{
btn.onClick.AddListener(OnBtn);
}
public void OnBtn()
{
List<float> lis = new List<float>();
for (int i = 0; i < Random.Range(3, 9); i++)
{
lis.Add(Random.Range(0.1f, 100));
}
MeshImage.arr = lis.ToArray();
MeshImage.SetVerticesDirty();
}
}