静态批处理

本文介绍了一个Unity3D脚本,通过应用静态批处理技术,减少绘制次数,从而提高游戏的帧率(FPS)。代码示例展示了如何创建纹理和立方体,并利用Mathf.PerlinNoise生成柏林噪声,最终结合StaticBatchingUtility进行批处理优化。
摘要由CSDN通过智能技术生成

没有加批处理绘制次数很高  FPS 81

加批处理绘制次数降低  FPS 94

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StaticBatchProcessing : MonoBehaviour
{
public Texture2D texture;//纹理

public int wnum;
public int hnum;
int w = 128;
int h = 128;
// Start is called before the first frame update
void Start()
{

//顶点是有个数限制的最多65000个
//纹理尽力取2的次方
wnum =4;
hnum = 4;
//补缝用

int w2 = w + 2;
int h2 = h + 2;
for (int i = 0; i < wnum; i++)
{
for (int j = 0; j < hnum; j++)
{

for (int x = 0; x < w2; x++)
{
for (int z = 0; z < h2; z++)
{
//柏林噪声算法,传入两个坐标返回一个0-1的值(传入两个相同的值,返回结果一定相同)
float y = Mathf.PerlinNoise((i * w + x) * 0.02f, (j * h + z) * 0.02f);
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.parent = this.transform;
cube.transform.localPosition = new Vector3(w*i+x,y*5,h*j+z);
}
}
}

}

//gameObject得是静态的
StaticBatchingUtility.Combine(gameObject);//此代码就是静态批处理代码


}

// Update is called once per frame
void Update()
{

}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值