public GameObject ball;
//生成的预制体
public GameObject panelPfb;
//缓存池
private Queue<GameObject> _panelPoolList = new Queue<GameObject>();
//场景中已经应用了的格子
private Queue<GameObject> _showObjList = new Queue<GameObject>();
//已经生成过格子的位子
public List<Vector3> _posList = new List<Vector3>();
//生成多少个后开始回收
private int SpawnIndex = 0;
private void Start()
{
Init();
}
private void Init()
{
CountFrame(panelPfb.transform.localPosition);
}
public void Update()
{
CountFrame(ball.transform.localPosition);
if (Mathf.Abs(_leftFrameX - ball.transform.position.x) < 5)
{
Vector3 v = new Vector3(_leftFrameX - 10, 0, _downFrameZ);
Debug.Log("小球到达左边");
isArriveLeft = true;
if (!_posList.Contains(v))
{
_posList.Add(v);
SpawnArea(v);
}
}
else
{
isArriveLeft = false;
}
if (Mathf.Abs(_rightFrameX - ball.transform.position.x) < 5)
{
Debug.Log("小球到达右边");
isArriveRight = true;
Vector3 v = new Vector3(_rightFrameX,0,_upFrameZ-10);
if (!_posList.Contains(v))
{
_posList.Add(v);
SpawnArea(v);
}
}
else
{
isArriveRight = false;
}
if (Mathf.Abs(_upFrameZ - ball.transform.position.z) < 5)
{
Debug.Log("小球到达上边");
isArriveUp = true;
Vector3 v = new Vector3(_leftFrameX,0,_upFrameZ);
if (!_posList.Contains(v))
{
_posList.Add(v);
SpawnArea(v);
}
}
else
{
isArriveUp = false;
}
//同时触发左边和触发上边生成左上的格子
if (isArriveLeft && isArriveUp)
{
Vector3 v = new Vector3(_leftFrameX-10,0,_upFrameZ);
if (!_posList.Contains(v))
{
_posList.Add(v);
SpawnArea(v);
}
}
//同时触发右边和触发上边生成右上的格子
if (isArriveRight && isArriveUp)
{
Vector3 v = new Vector3(_rightFrameX,0,_upFrameZ);
if (!_posList.Contains(v))
{
_posList.Add(v);
SpawnArea(v);
}
}
}
private void SpawnArea(Vector3 pos)
{
GameObject go = GetPool();
go.SetActive(true);
_showObjList.Enqueue(go);
go.transform.position = pos;
go.transform.rotation = ball.transform.rotation;
SpawnIndex++;
if (SpawnIndex > 10)
{
GameObject obj = _showObjList.Dequeue();
AddPool(obj);
}
}
private void AddPool(GameObject obj)
{
obj.SetActive(false);
_panelPoolList.Enqueue(obj);
}
private GameObject GetPool()
{
if (_panelPoolList.Count == 0)
{
return Instantiate(panelPfb);
}
else
{
return _panelPoolList.Dequeue();
}
}
#region 小球判断距离
private float _upFrameZ=5;
private float _downFrameZ = -5;
private float _leftFrameX=-5;
private float _rightFrameX=5;
private bool isArriveUp = false;
private bool isArriveLeft = false;
private bool isArriveRight = false;
//传入球的位置判断球处于运动格子里面还是运动格子的上左右的检测格子
public void CountFrame(Vector3 v)
{
if (v.x <_leftFrameX)
{
_leftFrameX -= 10;
_rightFrameX -= 10;
}
else if (v.x > _rightFrameX)
{
_leftFrameX += 10;
_rightFrameX += 10;
}
else if (v.z > _upFrameZ)
{
_upFrameZ += 10;
_downFrameZ += 10;
}
Debug.Log("上方边界"+_upFrameZ);
Debug.Log("下方边界"+_downFrameZ);
Debug.Log("左方边界"+_leftFrameX);
Debug.Log("右方边界"+_rightFrameX);
}
#endregion
在移动物体周边生成格子
最新推荐文章于 2024-08-14 21:00:35 发布