最近需要去做下地图的优化(优化就是减低DC),写了个练手的优化方案,没有通过,因为不够灵活,而且优化程度没有九宫格划分来的地图优化来的好,所以最后就取消了。这里我就将我写的简易的优化发上来,大家看看还有什么需要改进的。
优化的想法是,以玩家作为中心画一个圈。大于这个圈的就消失,在里面的就显示。每五秒调用下这个方法。这样就能减少很多DC。
这里我贴出我的代码,还在测试的,没有完全实现这个优化的想法,各位看官将就看看。
public class ClearNextGameObject : MonoBehaviour
{
private List<GameObject> screenObject;
private List<float> list;
public float Distance = 10;
private bool Five = false;
private void Awake()
{
GetGameObject();
}
private void AddTheR()
{
if (ETModel.UnitComponent.Instance.MyUnit != null)
{
Vector3 unit = ETModel.UnitComponent.Instance.MyUnit.Position;
for (int i = 0; i < screenObject.Count; i++)
{
float r = Mathf.Sqrt(Mathf.Pow((Mathf.Abs(screenObject[i].transform.localPosition.x) - Mathf.Abs(unit.x)), 2) + Mathf.Pow((Mathf.Abs(screenObject[i].transform.localPosition.z) - Mathf.Abs(unit.z)), 2));
list.Add(r);
}
}
}
bool IsFirst = true;
private void Update()
{
if (ETModel.UnitComponent.Instance.MyUnit != null)
{
if (IsFirst)
{
AddTheR();
IsFirst = false;
//Debug.Log("------------------------------------ "+list.Count);
}
HideSomeGameObject();
}
}
private void GetGameObject()
{
GameObject ScreenRoot = GameObject.Find("SceneRoot");
screenObject = new List<GameObject>();
list = new List<float>();
GetAllChild(ScreenRoot);
//Debug.Log("---------------"+screenObject.Count);
}
int a = 0;
private async void HideSomeGameObject()
{
if (Five)
{
return;
}
Five = true;
Vector3 unit = ETModel.UnitComponent.Instance.MyUnit.Position;
Debug.Log("_______________" + unit.x+" "+ unit.z);
for (int i = 0; i < screenObject.Count; i++)
{
//Debug.Log(list[i]); screenObject[i].transform.localPosition.x + unit.x < Distance ||
if ( screenObject[i].transform.localPosition.z + unit.z < 16|| screenObject[i].transform.localPosition.z + unit.z>125 )
//screenObject[i].transform.localPosition.x + unit.x < 15 || screenObject[i].transform.localPosition.x + unit.x > 100)
//if (list[i]>1)
{
screenObject[i].SetActive(false);
a++;
}
else
{
screenObject[i].SetActive(true);
}
}
// Debug.Log("_______________________________________________________"+a);
Five = await IsFive();
}
private async ETModel.ETTask<bool> IsFive()
{
ETModel.TimerComponent time = ETModel.Game.Scene.GetComponent<ETModel.TimerComponent>();
float fiveTime = 2;
while (true)
{
await time.WaitAsync(100);
fiveTime -= 0.1f;
if(fiveTime < 0)
{
break;
}
}
return false;
}
/// <summary>
/// 遍历所有的物体
/// </summary>
/// <param name="Obj"></param>
private void GetAllChild(GameObject Obj)
{
//if (times <= 3) return;
if (Obj.GetComponent<Renderer>() == null )
{
for (int i = 0; i < Obj.transform.childCount; i++)
{
GetAllChild(Obj.transform.GetChild(i).gameObject);
}
//times++;
}
else
{
if(Obj.tag != "Ground" && Obj.tag != "Cube")
screenObject.Add(Obj);
}
}
}
我用的框架是ET框架,非常不错,有兴趣的可以去看看。我还只是入门。