【发布时间】:2016-06-18 09:53:47
【问题描述】:
我有一个带有几个重载的方法,因为需要不同的对象类型(列表 vs 数组和 GameObject vs ParticleSystem):
void ObjectLoop(int uBound, List<GameObject> list, GameObject item, bool projectile, int decalNo = 0, int typeNo = 0)
{
for (int x = 0; x < uBound; x++)
{
list.Add(Instantiate(item) as GameObject);
list[x].transform.SetParent(this.transform);
list[x].SetActive(false);
if (projectile)
{
projScript = list[x].GetComponent<J_Projectile>();
projScript.decalType = decalNo;
projScript.hitType = typeNo;
}
}
}
void ObjectLoop(int uBound, List<ParticleSystem> list, ParticleSystem item)
{
for (int x = 0; x < uBound; x++)
{
list.Add(Instantiate(item) as ParticleSystem);
list[x].transform.SetParent(this.transform);
list[x].gameObject.SetActive(false);
}
}
void ObjectLoop(int uBound, GameObject[] list, GameObject item)
{
for (int x = 0; x < uBound; x++)
{
list[x] = (Instantiate(fireHazard) as GameObject);
list[x].transform.SetParent(this.transform);
list[x].SetActive(false);
}
}
有没有办法将这些压缩成一个方法,也许是通过传递列表或数组以及 GameObject 或 ParticleSystem 的对象类型?我猜它与泛型有关,但我无法完全理解它,所以欢迎对傻瓜进行解释:)
谢谢
【问题讨论】:
标签: c# generics unity3d parameter-passing overloading