生成新的Object
public GameObject objectPrefab;
GameObject newObject = Instantiate(objectPrefab) as GameObject; //1
GameObject newObject = (GameObject)Instantiate(objectPrefab); //2
Transform newObject = Instantiate(myPrefab) as Transform; //3
Transform newObject = (Instantiate(myPrefab) as GameObject).transform; //4
Transform newObject = (Instantiate(myPrefab) as GameObject).GetComponent<Transform>(); //5
较建议使用的ObjectPool
public GameObject objectPrefab;
public Transform inUseParent;
public List<Transform> objectInPool;
public List<Transform> objectInUse;
public Transform GetNewObject()
{
Transform newObj = null;
if(objectInPool.Count > 0)
{
newObj = objectInPool[0];
SetObjStatus(newObj, true);
}
else
{
newObj = (Instantiate(objectPrefab) as GameObject).transform;
objectTransform.SetParent(inUseParent, false);
SetObjStatus(newObj, true);
}
return newObj;
}
public void SetObjStatus(Transform obj, bool SetToInUse)
{
if(obj != null)
{
obj.gameObject.SetActive(SetToInUse);
if(SetToInUse)
{
if(objectInPool.Contains(obj))
{
objectInPool.Remove(obj);
}
if(!objectInUse.Contains(obj))
{
objectInUse.Add(obj);
}
}
else
{
if(!objectInPool.Contains(obj))
{
objectInPool.Add(obj);
}
if(objectInUse.Contains(obj))
{
objectInUse.Remove(obj);
}
}
}
}
Collider
- 首先,所有有关的GameObject上面必须有Collider,IsTrigger不勾选。
- 触发Collider的GameObject任何之一必须有RigiBody,其UseGravity必须勾选,IsKinematic不能勾选。
- 如果不想其受重力影响请修改其他参数。
private void OnCollisionEnter(Collision col)
{
Debug.Log("Col Enter");
}
转载:https://blog.csdn.net/MikeW138/article/details/103557518
查看评论