首页 > 其他分享 >RotationChart2D(轮转图2D)

RotationChart2D(轮转图2D)

时间:2022-12-23 08:22:06浏览次数:34  
标签:distance RotationChart2D 轮转 float list Mathf 2D sorts public

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using DG.Tweening;

public class RotationChart2D : MonoBehaviour,IDragHandler, IEndDragHandler
{
public RadarMap radarMap;
public int num;//个数
public Image prefab;
public float spacing;//间距

public float max = 1;//最大缩放比
public float min = 0.5f;//最小缩放比

public float cutSpeed ; // 减速度

float c;//周长
float r;//半径
float ang;//角弧度
float distance;//拖动距离

List<GameObject> list = new List<GameObject>();

List<Transform> sorts = new List<Transform>();

// Start is called before the first frame update
void Start()
{
//周长
c = (spacing + prefab.rectTransform.rect.width) * num;
//半径
r = c / (2 * Mathf.PI);

ang = 2 * Mathf.PI / num;
Move();
}
public void Move()
{
float moveAng = distance / r;
for (int i = 0; i < num; i++)
{
if (list.Count<=i )
{
GameObject g = Instantiate(prefab.gameObject, transform);
g.transform.Find("Text").GetComponent<Text>().text = i.ToString();
list.Add(g);
sorts.Add(list[i].transform);
}
float x = Mathf.Sin(i * ang + moveAng) * r;
float z = Mathf.Cos(i * ang + moveAng) * r;
//位置
list[i].transform.localPosition = Vector3.right * x;
//计算缩放比
float p = (z + r) / (r + r);
float scale = (max - min) *p + min;

list[i].transform.localScale = Vector3.one * scale;
}
sorts.Sort((a, b) =>
{
if (a.localScale.x > b.localScale.x)
{
return 1;
}
else if (a.localScale.x == b.localScale.x)
{
return 0;
}
else
{
return -1;
}
});
//按层级排序
for (int i = 0; i < sorts.Count; i++)
{
sorts[i].SetSiblingIndex(i);
}

}

// Update is called once per frame
void Update()
{

}

public void OnDrag(PointerEventData eventData)
{
//eventData.delta.x每帧移动的距离
distance += eventData.delta.x;
Move();
}

public void OnEndDrag(PointerEventData eventData)
{
float speed = eventData.delta.x; //初始速度
float time = Mathf.Abs(speed / cutSpeed );//惯性时间
print("我的惯性时间是"+time);
DOTween.To((a) =>
{
distance += a;
Move();
}, speed, 0, time).OnComplete(() =>
{
float aliAng = Mathf.Asin(sorts[num - 1].localPosition.x / r);
float aliDis = aliAng * r;
float aliTime = Mathf.Abs(aliDis) / cutSpeed;
DOTween.To((a) =>
{
distance = a;
Move();
}, distance, distance - aliDis, aliTime);
//惯性结束之后调用对其
//Alignment(list.IndexOf(sorts[sorts.Count-1].gameObject));
});
}
//对齐
public void Alignment(int n)
{
//弧度
float moveAng= Mathf.Asin(sorts[sorts.Count - 1].localPosition.x / r);
//最近的下标
int index = list.IndexOf(sorts[sorts.Count - 1].gameObject);
//两个下标的差值
float a = n - index;
//要旋转的弧度
float b = num - Mathf.Abs(a);
//ab反方向 a正b负 a负b正
b = a > 0 ? -b : b;
//找较短的移动距离
float moveDis = Mathf.Abs(a) < Mathf.Abs(b) ? a : b;
//求弧长
moveAng += moveDis * ang+Mathf.Asin(sorts[num-1].transform.localPosition.x/r);
//弧长
float dis = moveAng * r;//+ c * 3 ;
//转动时间
float time = Mathf.Abs(dis / cutSpeed);
print("转动时间是" + time);
DOTween.To((aa) =>
{
distance = aa;
Move();
}, distance, distance - dis, time).OnComplete(() =>
{
//对其之后执行对应函数
//修改雷达图
for (int i = 0; i < radarMap.arr.Length; i++)
{
radarMap.arr[i] = Random.Range(0, 100);
}

});
//清理脏数据
radarMap.SetVerticesDirty();
}
}

标签:distance,RotationChart2D,轮转,float,list,Mathf,2D,sorts,public
From: https://www.cnblogs.com/zanzz/p/16999930.html

相关文章

  • [ABC266Ex] Snuke Panic (2D)
    ProblemStatementTakahashiistryingtocatchmanySnuke.Therearesomepitsinatwo-dimensionalcoordinateplane,connectedtoSnuke'snest.Now,$N$Snuke......
  • 使用 Doxygen 来生成 Box2d 的 API 文档
      对于Doxygen以前只听别人说过,而现在使用它也是一个偶然,缘分吧。前两天看box2d的官方sdk中,发现他有用户手册却没有说明,只是留下了一个Doxygen的文件。事情告一......
  • [CF1422D] Returning Home 题解
    题目描述一个\(n\timesn\)的网格,求两点间最短用时。你可以用一分钟向上下左右任意一个方向移动一格.特别的,城市中有\(m\)个传送点,第\(i\)个的坐标为\((x_{i},y_{i})......
  • CF1772D
    【题意】\(n\)个数的数组\(a\),选择一个\(x\in[0,10^9]\)使得\(b_i=|a_i-x|\)这个数组单调不减。\(n\le10^5,a_i\in[1,10^8]\)【分析】看到题目,第一......
  • 【iOS-Cocos2d游戏开发之十八】解决滚屏背景/拼接地图有黑边(缝隙)以及禁止游戏中自动
       本章节主要为大家介绍在游戏开发过程中经常遇到的两个问题; 1.解决滚屏背景或拼接地图有黑边!   对于游戏开发中,背景(游戏地图)是必要的元素之一,那......
  • 自定义Live2D插件配置并加载CDN数据
    修复博客问题的时候,发现加载​​Live2D​​的模型报了一堆错误。仔细看,是由于看板娘的动作文件出错了,而且居然是大小写的问题,想必windows服务器就不会出现这个问题,所以模......
  • [ABC282D] Make Bipartite 2 题解
    题目描述给定一个无向简单图\(G\),统计有多少个点对\((u,v)\)满足:\(u,v\)之间没有边直接连接:\((u,v)\notin\textE\)连接\((u,v)\)后\(G\)是二分图......
  • 在springboot项目集成r2dbc,集成mysql的流式代码DAO层
    我引用的是springboot2.7.0版本。在pom.xml里引入r2dbc的包,和mysql的驱动包:<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot......
  • c/c++ 2d矢量库
    CairoCairoisa2Dgraphicslibrarywithsupportformultipleoutputdevices.Currentlysupported outputtargets includetheXWindowSystem(viaboth Xlib......
  • android(安卓)cocos2d-x关于防止游戏中锁屏问题
    于是又在群里问,一般的群里不是在聊女人,就是在瞎扯蛋。在我很失落的时候,群里有一个人主动密我,告诉我安卓里的锁屏是怎么回事。于是我很感动。这位朋友,给了我一个网页。我......