地砖场景60*80资源开销太大,询问优化方法

datacodecat 2018-04-17 05:09:18
我制作了一个场景,使用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);

}
}


...全文
875 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
无名剑 2018-05-29
  • 打赏
  • 举报
回复
地面做成一个mesh吧,自己定义一个mesh,然后根据地形数据设置里头的顶点数据信息 60*80用cube去做 都要4000多个gameobject 即使batch了 创建也是非常耗时的事情
QverOo 2018-05-03
  • 打赏
  • 举报
回复
1、合并mesh。 2、如果地面是平面,且相机看不到全部Cube,可以使用遮挡剔除(但是这个不符合你代码生成地面的需求)。 3、如果地面是平面,且相机看不到全部Cube,假设只能看到4个Cube,那你就只生成16个Cube,根据人物位置来移动Cube,保证相机内能照射到4个Cube大小的范围。
xiaoruanzhu 2018-04-17
  • 打赏
  • 举报
回复
用Window->Profiler查下呗,看下CPU Usage哪里消耗大

2,538

社区成员

发帖
与我相关
我的任务
社区描述
Unity3D相关内容讨论专区
游戏unity 技术论坛(原bbs)
社区管理员
  • Unity3D
  • 芝麻粒儿
  • 「已注销」
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

Unity3D社区公告:

  1. 社区致力于解决各种Unity3D相关的“疑难杂症”。
  2. 社区不允许发布与Unity3D或相关技术无关内容。
  3. 社区版主邀请各位一道为打造优秀社区不懈努力。

试试用AI创作助手写篇文章吧