最近需要去做下地圖的優化(優化就是減低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框架,非常不錯,有興趣的可以去看看。我還只是入門。