实现雷达图每个点的拖拽和雷达显示

using System.Collections;
using System.Collections.Generic;
using System.Net;
using UnityEditor;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

[RequireComponent(typeof(CanvasRenderer))]
public class MyRead : MaskableGraphic, IDragHandler, IBeginDragHandler, IEndDragHandler
{
    //设置半径大小
    int r = 150;
    //设置几边形和几个点
    int n = 5;
    [SerializeField]
    //随机小数
    [Range(0, 1)] float[] ran;
    //存顶点
    List<Vector3> pos = new List<Vector3>();
    //雷达图每个可拖拽的顶点
    List<Image> imgas = new List<Image>();
    //
    List<Vector3> YuanPos = new List<Vector3>();
    [SerializeField]
    Image g;
    float angel = 0;




    protected override void Start()
    {
        //如果是运行状态
        if (Application.isPlaying)
        {
            angel = Mathf.PI * 2 / n;
            for (int i = 0; i < 5; i++)
            {
                ran[i] = Random.Range(0, 1f);
                float x = Mathf.Sin(i * angel) * r * ran[i];
                float y = Mathf.Cos(i * angel) * r * ran[i];
                if (i == 0)
                {
                    imgas.Add(g);
                }
                else
                {
                    GameObject go = Instantiate(g.gameObject);
                    go.transform.SetParent(transform);

                    imgas.Add(go.GetComponent<Image>());
                }
                imgas[i].transform.localPosition = new Vector3(x, y, 0);
                imgas[i].transform.localScale = Vector3.one;
                imgas[i].name = "icon" + i;
            }
        }
    }
    /// <summary>
    /// 重写OnPopulateMesh
    /// </summary>
    /// <param name="vh">绘制显示的雷达图</param>
    protected override void OnPopulateMesh(VertexHelper vh)
    {
        //每次刷新清楚一下
        vh.Clear();
        //添加点
        vh.AddVert(new Vector3(0, 0, 0), Color.cyan, Vector2.zero);


        for (int i = 0; i < n; i++)
        {
           //绘制雷达线
            float x1 = Mathf.Sin(i * angel) * r;
            float y1 = Mathf.Cos(i * angel) * r;
            YuanPos.Add(new Vector3(x1, y1, 0));

            float x = Mathf.Sin(i * angel) * r * ran[i];
            float y = Mathf.Cos(i * angel) * r * ran[i];
            pos.Add(new Vector3(x, y, 0));
            vh.AddVert(new Vector3(x, y, 0), Color.cyan, Vector2.zero);
            //处理最后一个点
            if (i + 1 == n)
            {
                vh.AddTriangle(0, 1, i + 1);
            }
            else
            {
                vh.AddTriangle(i + 1, 1, i + 1 + 1);
            }

        }



    }
    int currentindex = -1;
    


    public void OnBeginDrag(PointerEventData eventData)
    {
        //判断点击的为空返回
        if (eventData.pointerCurrentRaycast.gameObject == null)
            return;
        //再判断点击的名字是否是顶点名字
        string name = eventData.pointerCurrentRaycast.gameObject.name;
        if (!name.Contains("icon"))
        {
            return;
        }
        print(name);
        //点击的名字的下标进行拖拽中射线会出现在别的顶点上面会进行更换顶点
        currentindex = int.Parse(name.Replace("icon", ""));
        if (currentindex != -1)
        {
            for (int i = 0; i < imgas.Count; i++)
            {
                if (i != currentindex)
                {
                    imgas[i].raycastTarget = false;
                }
                else
                {
                    imgas[i].raycastTarget = true;
                }
            }
        }

    }


    public void OnDrag(PointerEventData eventData)
    {
        #region
        //拖拽中是否会鼠标过快然后对象变为空
        GameObject g = eventData.pointerCurrentRaycast.gameObject;
        if (g!=null)
        {
            Vector2 pp;
            try
            {
                //把鼠标的位置转为ui坐标赋值给pp
                var flag = RectTransformUtility.ScreenPointToLocalPointInRectangle(
                 this.rectTransform,
                 new Vector2(Input.mousePosition.x, Input.mousePosition.y),
                 Camera.main,
                 out pp
                 );
               
                if (flag)
                {
                    //判断鼠标的模是否超出0,1
                    ran[currentindex] = Mathf.Clamp(pp.magnitude / r, 0, 1);
                    //限制半径0,1
                    float x = Mathf.Sin(currentindex * angel) * r * Mathf.Clamp(ran[currentindex], 0, 1);
                    float y = Mathf.Cos(currentindex * angel) * r * Mathf.Clamp(ran[currentindex], 0, 1);
                    //顶点赋值
                    imgas[currentindex].transform.localPosition = new Vector3(x, y, 0);
                    //刷新顶点助手
                    SetAllDirty();
                }

            }
            catch (System.Exception)
            {
            }
            #endregion

        }
        else
        {
            print("对象为空");
        }


    }

    public void OnEndDrag(PointerEventData eventData)
    {
        //顶点图固定位置
        if (currentindex != -1)
        {
            float x = Mathf.Sin(currentindex * angel) * r * ran[currentindex];
            float y = Mathf.Cos(currentindex * angel) * r * ran[currentindex];
            imgas[currentindex].transform.localPosition = new Vector3(x, y, 0);
            currentindex = -1;
        }

        //打开所有射线检测
        for (int i = 0; i < imgas.Count; i++)
        {
            imgas[i].raycastTarget = true;

        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值
>