加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

Unity学习(八)动态合并网格

(2015-08-23 22:41:46)
标签:

unity优化

分类: Unity
unity支持合并动态对象网格和静态对象网格,但是合并动态网格的条件实在太苛刻,像场景中固定不动的物件可以使用StaticBatchingUtility合并。具体的信息看官方文档或者http://blog.sina.com.cn/s/blog_471132920101gbfr.html

Unity的第三方插件功能倒是很强大,比如MeshBaker2,可以将MeshRender和SkinedMeshRender进行合并,但是做项目游戏对象都是动态生成,所以就找了找动态合并的方法。
原理大致如下:
1.获取网格烘焙器
2.获取材质烘焙器
3.创建烘焙结果集,并创建材质球,以及添加需要合并的对象,然后合并图集。
4.将结果集指定给网格烘焙器
5.合并网格

效果如下:

顶问题来了,unity一个mesh的顶点上限是64K,一个模型2k-3k顶点,那么能同时使用这种合并方式创建的对象个数也就30来个,而且当对象个数多的时候,创建新的就会有卡顿现象(可能是我没用好?),尽管如此,这么多的对象只消耗一个DC!http://www/uc/myshow/blog/misc/gif/E___6743EN00SIGG.gif

测试代码如下,仅供参考:
  1. using UnityEngine;  
  2. using System.Collections.Generic;  
  3.   
  4.    
  5. public class BakeTexturesAtRuntime MonoBehaviour  
  6.  
  7.     // 用于测试创建的对象  
  8.     public GameObject resObj1;  
  9.     public GameObject resObj2;  
  10.     // 合并的主脚本对象  
  11.     public GameObject target;  
  12.    
  13.     // 用于合并的资源原始对象,带meshrender  
  14.     public GameObject obj1;  
  15.     public GameObject obj2;  
  16.     private List objs new List();  
  17.    
  18.     private MB2_MeshBaker meshbaker;  
  19.     private MB2_TextureBaker textureBaker;  
  20.    
  21.    
  22.     void Start()  
  23.      
  24.         // 网格烘焙器  
  25.         meshbaker target.GetComponent();  
  26.         // 材质烘焙器  
  27.         textureBaker target.GetComponent();  
  28.         // 材质烘焙结果集  
  29.         textureBaker.textureBakeResults ScriptableObject.CreateInstance();  
  30.         // 创建材质球  
  31.         textureBaker.resultMaterial new Material(Shader.Find("Diffuse"));  
  32.         // 测试要合并的两个对象,将网格指定给材质  
  33.         List resList new List();  
  34.         resList.Add(resObj1);  
  35.         resList.Add(resObj2);  
  36.         textureBaker.objsToMesh resList;  
  37.         // 创建图集  
  38.         textureBaker.CreateAtlases();  
  39.         // 指定材质结果集到网格烘焙器  
  40.         meshbaker.textureBakeResults textureBaker.textureBakeResults;  
  41.      
  42.    
  43.     private int 0 
  44.     void OnGUI(){  
  45.    
  46.         if (GUILayout.Button("创建新的对象"))  
  47.          
  48.             GameObject go;  
  49.             if (i%2 == 0 
  50.              
  51.                 go (GameObject)Instantiate(obj1);  
  52.              
  53.             else  
  54.              
  55.                 go (GameObject)Instantiate(obj2);  
  56.              
  57.             go.GetComponent().cullingType AnimationCullingType.AlwaysAnimate;  
  58.             go.transform.localPosition new Vector3(i++, 00);  
  59.    
  60.             objs.Add(go.GetComponentInChildren().gameObject);  
  61.    
  62.             // 添加到合并列表  
  63.             meshbaker.AddDeleteGameObjects(objs.ToArray(), null);  
  64.             meshbaker.Apply();  
  65.          
  66.      
  67. }

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有