unity中将多张图片进行椭圆运动
GameObject canvas;
int num = 20;
float angle = 0;
private float shortRadius=200;
float longRadius = 450;
Dictionary<int,GO> storeItem;
GameObject main;
public static bool run = true;
public static int index = 0;
void Start()
{
storeItem = new Dictionary<int,GO>();
angle = 360.0f / num;
canvas = GameObject.Find("Canvas");
main = GameObject.Find("main");
for (int i = 0; i < num; i++)
{
GameObject a = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("prefab/item"));
a.transform.SetParent(canvas.transform);
a.GetComponent<RectTransform>().anchoredPosition = new Vector2(Mathf.Cos(i * angle * Mathf.Deg2Rad) * longRadius, Mathf.Sin(i * angle * Mathf.Deg2Rad) * longRadius);
a.transform.Find("content").GetComponent<Text>().text = (i * angle).ToString();
GO _go = new GO();
_go._o = a;
_go._o.GetComponent<hintPanelEvent>().index = i;
_go.angle = i * angle;
_go.index = i;
storeItem.Add(i,_go);
}
}
// Update is called once per frame
GameObject name =null;
void Update()
{
if(run)
{
foreach (var kk in storeItem.Values)
{
kk.angle = kk.angle + 0.1f;
if (kk.angle >= 360)
{
kk.angle = kk.angle - 360;
}
kk._o.GetComponent<RectTransform>().anchoredPosition = new Vector2(Mathf.Cos(kk.angle * Mathf.Deg2Rad) * longRadius, Mathf.Sin(kk.angle * Mathf.Deg2Rad) * shortRadius);
if (kk._o.GetComponent<RectTransform>().anchoredPosition.y <= 450 && kk._o.GetComponent<RectTransform>().anchoredPosition.y >= 449.9)
{
if (name != kk._o)
{
name = kk._o;
var _text = kk._o.transform.Find("content").GetComponent<Text>().text;
Debug.LogError(_text);
main.transform.Find("content").GetComponent<Text>().text = _text;
//先把所有的隐藏掉
foreach (var hh in storeItem.Values)
{
hh._o.transform.Find("back").gameObject.SetActive(false);
}
kk._o.transform.Find("back").gameObject.SetActive(true);
}
}
}
}
else
{
var text=storeItem[index]._o.transform.Find("content").GetComponent<Text>().text;
main.transform.Find("content").GetComponent<Text>().text = text;
//先把所有的隐藏掉
foreach (var hh in storeItem.Values)
{
hh._o.transform.Find("back").gameObject.SetActive(false);
}
storeItem[index]._o.transform.Find("back").gameObject.SetActive(true);
}
}
class GO
{
public GameObject _o;
public int index = 0;
public float angle = 0;
}