废话不多说,直接上代码
using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Test : MonoBehaviour
{
public float targetPosY = 0F;
public GameObject prefab;
public Transform parent;
private List<GameObject> objList = new List<GameObject>();
public Transform birth;
public void Awake()
{
Vector2 ui1ScreenPos = Camera.main.WorldToScreenPoint(prefab.transform.position);
Vector2 localPos;
bool isSucess = RectTransformUtility.ScreenPointToLocalPointInRectangle(birth.transform.GetComponent<RectTransform>(), ui1ScreenPos, Camera.main, out localPos);
if (isSucess)
{
targetPosY = localPos.y + 110f;
}
}
private void Start()
{
parent.gameObject.SetActive(false);
for (int i = 0; i < 36; i++)
{
GameObject go = Instantiate(prefab, birth);
go.name = i.ToString();
go.SetActive(true);
go.transform.localPosition = Vector3.zero;
objList.Add(go);
}
StartCoroutine(MoveAni());
}
private IEnumerator MoveAni()
{
yield return new WaitForSeconds(3);
yield return new WaitForEndOfFrame();
parent.gameObject.SetActive(true);
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 6; j++)
{
GameObject go = objList[i * 6 + j];
go.SetActive(true);
go.transform.DOLocalMove(new Vector3(parent.transform.localPosition.x, targetPosY, 0) - new Vector3(-30f * 3 + j * 30f, 10f, 0f), 0.3f).OnComplete(() =>
{
go.transform.SetParent(parent);
//go.transform.GetComponent<CardTurnOver>().StartFront();
});
}
yield return new WaitForSeconds(0.5f);
}
for (int i = 0; i < objList.Count; i++)
{
objList[i].transform.GetComponent<CardTurnOver>().StartFront();
yield return new WaitForSeconds(0.01f);
}
}
}
CardTurnOver 参考自这里,谢谢这位大神
坐标转换参考
Unity 不同父物体的UI坐标转换
场景测试如图