我制作了一個場景,使用cube作為地磚載體,設定了一個地板的材質,然后進行渲染。場景大小為60*80,但是他的開銷似乎比較大,我想問一下像我這種情況應該怎么優化呢?我的cube做成了prefab的模式,在程式里動態添加的。
下圖是我的開銷情況

下面是我的設定地形部分的代碼。
謝謝
public GameObject cellteil;
public GameObject character_walker;
public GameObject cellcube;
public GameObject character_solder;
public GameObject character_solder_impl;
public GameObject treep01;//場景中 帶碰撞的樹的模型
public int map_w, map_h;
// Use this for initialization
void Start () {
map_w = 60;
map_h = 80;
InitMap ();
//放置人物
MakeCharacter ();
}
// Update is called once per frame
void Update () {
doCharacterOnGround ();
}
void InitMap(){
TileCell[,] tc = new TileCell[map_w,map_h];
//首先生成資料模型,第一步全部填充為木質地板。
for (int x = 0; x < map_w; x++) {
for (int y = 0; y < map_h; y++) {
tc [x, y].tiletype = 0;
//增加下修改,邊緣地區全部換為3型別, 3型別表示帶有1x1碰撞的樹,作為地圖邊界
if (x == 0 || x == map_w - 1 || y == 0 || y == map_h - 1) {
tc [x, y].tiletype = 0;
}
//CreateCell (x, y);
}
}
//在某個位置放置 小花園
MakeGarden(tc);
//根據模型資料生成實際顯示物件
for (int x = 0; x < map_w; x++) {
for (int y = 0; y < map_h; y++) {
//tc [x, y].tiletype = 0;
//if (tc [x, y].tiletype == 0) {
CreateCell (x, y,tc[x,y].tiletype);
//}
}
}
}
//創建隨機花園
void MakeGarden(TileCell[,] tc){
//隨機坐標位置,花園默認大小 8*8 所以坐標必須在1-13 之間
//int i = num1.Next(12);
int gx =Random.Range(1, 40);
int gy=Random.Range(1, 40);
tc [gx, gy].tiletype = 1;
tc [gx+1, gy].tiletype = 1;
tc [gx+2, gy].tiletype = 1;
tc [gx+5, gy].tiletype = 1;
tc [gx+6, gy].tiletype = 1;
tc [gx+7, gy].tiletype = 1;
tc [gx, gy+1].tiletype = 1;
tc [gx, gy+2].tiletype = 1;
tc [gx+7, gy+1].tiletype = 1;
tc [gx+7, gy+2].tiletype = 1;
tc [gx, gy+5].tiletype = 1;
tc [gx, gy+6].tiletype = 1;
tc [gx+7, gy+5].tiletype = 1;
tc [gx+7, gy+6].tiletype = 1;
tc [gx, gy+7].tiletype = 1;
tc [gx+1, gy+7].tiletype = 1;
tc [gx+2, gy+7].tiletype = 1;
tc [gx+5, gy+7].tiletype = 1;
tc [gx+6, gy+7].tiletype = 1;
tc [gx+7, gy+7].tiletype = 1;
}
//將陣列中的地塊實作化
private void CreateCell (int x, int z,int type) {
if (type == 0) {
GameObject go01 = Instantiate (cellteil);
//go01.transform.parent = transform;
go01.transform.position = new Vector3 (x * 1.0f, 2.0f, z * 1.0f);
}
if (type == 1) {
GameObject go01 = Instantiate (cellcube);
//go01.transform.parent = transform;
go01.transform.position = new Vector3 (x * 1.0f, 3.0f, z * 1.0f);
}
if (type == 3) {
GameObject go01 = Instantiate (treep01);
//go01.transform.parent = transform;
go01.transform.position = new Vector3 (x * 1.0f, 3.0f, z * 1.0f);
}
//go01.transform.localPosition = new Vector3(x - 3 * 0.5f + 0.5f, 0f, z - 3 * 0.5f + 0.5f);
//prefab_gress newCell = Instantiate(cellteil) as prefab_gress;
//cells[x, z] = newCell;
//newCell.name = "Maze Cell " + x + ", " + z;
//newCell.transform.parent = transform;
//newCell.transform.localPosition = new Vector3(x - sizeX * 0.5f + 0.5f, 0f, z - sizeZ * 0.5f + 0.5f);
}
//放置人物
private void MakeCharacter(){
//主要角色
character_solder_impl = Instantiate (character_solder);
character_solder_impl.name = "solder01";
character_solder_impl.transform.position=new Vector3 (10 * 1.0f, 6.0f, 10 * 1.0f);
GameObject.Find ("Main Camera").GetComponent<follow> ().doSetFollower ();
//敵人,也就是僵尸
character_walker= Instantiate (character_walker);
character_walker.name = "character_walker01";
character_walker.transform.position=new Vector3 (20 * 1.0f, 6.0f, 30 * 1.0f);
}
private void doCharacterOnGround(){
if (character_solder_impl==null) {
//移動到位置就停止了
return ;
}
Vector3 moveDirection = Vector3.zero;
float gravity = 10f; //這個是下落速度
// doCharacterOnGround = GetComponent<CharacterController> ();
if (character_solder_impl.GetComponent<CharacterController>().isGrounded) {
//Debug.Log ("已經落地");
//character_solder_impl.GetComponent<CharacterController>().SimpleMove( new Vector3(0,0,-30) * Time.deltaTime);
} else {
//沒有落地
moveDirection.y -= gravity ;
//Debug.Log ("沒有沒有沒有沒有落地"+moveDirection.y);
character_solder_impl.GetComponent<CharacterController>().SimpleMove(moveDirection * Time.deltaTime);
}
}
uj5u.com熱心網友回復:
用Window->Profiler查下唄,看下CPU Usage哪里消耗大uj5u.com熱心網友回復:
1、合并mesh。2、如果地面是平面,且相機看不到全部Cube,可以使用遮擋剔除(但是這個不符合你代碼生成地面的需求)。
3、如果地面是平面,且相機看不到全部Cube,假設只能看到4個Cube,那你就只生成16個Cube,根據人物位置來移動Cube,保證相機內能照射到4個Cube大小的范圍。
uj5u.com熱心網友回復:
地面做成一個mesh吧,自己定義一個mesh,然后根據地形資料設定里頭的頂點資料資訊60*80用cube去做 都要4000多個gameobject
即使batch了 創建也是非常耗時的事情
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/51933.html
標籤:Unity3D
下一篇:cocos2dx 調度器問題
