实习时遇到的大神写的代码, 自己差距还是有的,潜力巨大!加油!
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
// 数据
[System.Serializable]
public class TransData
{
// 初始位置
public Vector3 m_InitPos;
//初始化的旋转
public Vector3 m_InitRot;
//需要创建的物体
public GameObject m_Prefab;
//实例化的物体
public GameObject m_InitGo;
//移动方向
public Vector3 m_MoveDir;
//当前移动时间
public float m_CurrTime;
}
public class TestLize : MonoBehaviour
{
// 反应前的物体
public List<TransData> m_PreDatas;
//反应后的物体
public List<TransData> m_AfterDatas;
[SerializeField]
//间隔时间
public float m_InvTime;
// 当前时间
private float m_CurrTime;
// 当前创建在场景中的物体
public List<TransData> m_CurrTransData;
void Update()
{
m_CurrTime += Time.deltaTime;
if(m_CurrTime >= m_InvTime)
{
foreach(TransData item in m_PreDatas)
{
GameObject go = Instantiate(item.m_Prefab, item.m_InitPos, Quaternion.Euler(item.m_InitRot)) as GameObject;
TransData td = new TransData()
{
m_Speed = item.m_Speed;
m_MoveDir = item.m_MoveDir;
m_InitGo = go;
m_movTime = item.m_MovTime;
};
m_CurrTransData.Add(td);
}
m_CurrTime = 0.0f;
}
int count = m_CurrTransData.Count;
if(count > 0)
{
List<TransData> tempData = new List<TransData>();
for(int i = 0; i < count; i++)
{
Debug.Log(m_CurrTransData[i].m_InitGo);
m_CurrTransData[i].m_InitGo.transform.position += m_CurrTransData[i].m_MoveDir * m_CurrTransData[i].m_Speed;
m_CurrTransData[i].m_CurrTime += Time.deltaTime;
float tempTime = m_CurrTransData[i].m_MovTime - m_CurrTransData[i].m_CurrTime;
Debug.Log(m_CurrTransData[i].m_MovTime - m_CurrTransData[i].m_CurrTime);
if(tempTime < 0)
{
tempData.Add(m_CurrTransData[i]);
}
}
foreach(TransData item in tempData)
{
Destroy(item.m_InitGo);
m_CurrTransData.Remove(item);
}
}
}
}
}