首页 > 代码库 > Unity3D 预设打包的注意事项
Unity3D 预设打包的注意事项
在平时的开发中,把预设打包成 assetbundle 文件是非常普遍的做法,但是我们不能随便把预设打包成 assetbundle 就算完事,我们应该先清楚把预设打包成 assetbundle 的目的,通常我们把预设打包成 assetbundle 文件是为了压缩预设的文件尺寸以方便从网络快速加载,既然这样,我们就应该知道打包预设的时候应该注意哪些问题。
我们先创建测试环境,场景中的 Stamp7 与 Stamp9 网格不同,但是材质相同,R1_Column1 与 Stamp7 以及 Stamp9 的网格、材质都不相同,场景如图:
接着我们把场景的子对象(场景中的 Stamp7、Stamp9、R1_Column1)先做成预设,如图:
然后我们分别把这三个文件打包成 assetbundle 文件,并观察生成 assetbundle 文件大小,如图:
<style>.imageplus-append-lu-img-txt { overflow: hidden; margin: 10px 0; font-family: Microsoft YaHei; line-height: normal } .imageplus-append-lu-img-txt .imageplus-append-content .imageplus-append-img-txt-ad-item { float: left; position: relative; height: 90px; width: 125px } .imageplus-append-lu-img-txt .imageplus-append-content .imageplus-append-img-txt-ad-item .imageplus-append-img-txt-ad-item-img img { height: 100%; width: 100% } .imageplus-append-lu-img-txt .imageplus-append-content .imageplus-append-img-txt-ad-item .imageplus-append-img-txt-ad-item-desc { position: absolute; bottom: 0; left: 0; width: 100%; height: 18px; line-height: 18px; font-size: 14px; text-align: center; color: #fff; background-color: rgba(0,0,0,0.6); text-decoration: none } .imageplus-append-lu-img-txt .imageplus-append-content .imageplus-append-img-txt-ad-item .imageplus-append-img-txt-ad-item-desc span { color: #fff } .imageplus-append-lu-img-txt .imageplus-append-content .imageplus-append-img-txt-ad-item:hover .imageplus-append-img-txt-ad-item-img { border: 2px solid #f60 } .imageplus-append-close-btn { display: none } .imageplus-baidu-logo { position: absolute; bottom: 0; right: 0; z-index: 9999; background: url("http://ecma.bdimg.com/public03/imageplus/logo.png") no-repeat; background-position: 0 -17px; width: 16px; height: 16px } .imageplus-ad-logo { position: absolute; left: 0; bottom: 0; overflow: hidden; z-index: 12; background: url("http://ecma.bdimg.com/public03/imageplus/logo.png") no-repeat; background-position: 0 0; width: 34px; height: 16px } .imageplus-append { float: none; margin: 0; padding: 0; border: 0; overflow: hidden; visibility: visible; text-align: left; background: transparent; position: relative; text-indent: 0; display: inline-block } .imageplus-append div { float: none; margin: 0; padding: 0; border: 0; overflow: hidden; position: static; display: block; visibility: visible; text-align: left; background: transparent; font-family: Microsoft YaHei; line-height: normal } .imageplus-append a,.imageplus-append img,.imageplus-append span { float: none; margin: 0; padding: 0; border: 0; overflow: visible; position: static; display: inline; visibility: visible; text-align: left; background: transparent; font-family: Microsoft YaHei; line-height: normal }</style>
从图中我们可以看出,因为 Stamp7、Stamp9 材质相同,他们生成出来的文件大小差不多大,但是他们是独自打包,所以材质也是独立的,因为材质无法共享。
下面我们把 Trees(包括 Stamp7 与 Stamp9)以及 Stones 也做成预设,如图:
<style>.imageplus-append-lu-img-txt { overflow: hidden; margin: 10px 0 } .imageplus-append-nova-txt { border: 1px solid #f2f2f2; font-family: Microsoft YaHei; line-height: normal } .imageplus-append-nova-txt .imageplus-append-content .imageplus-append-nova-txt-ad-item { position: relative; width: 100%; height: 50px; background-color: #fff } .imageplus-append-nova-txt .imageplus-append-content .imageplus-append-nova-txt-ad-item a { text-decoration: none } .imageplus-append-nova-txt .imageplus-append-content .imageplus-append-nova-txt-ad-item a:hover { text-decoration: underline } .imageplus-append-nova-txt .imageplus-append-content .imageplus-append-nova-txt-ad-item div { white-space: nowrap; overflow: hidden; width: auto; height: 25px; line-height: 25px; margin: 0 16px; font-weight: normal } .imageplus-append-nova-txt .imageplus-append-content .imageplus-append-nova-txt-ad-item .imageplus-append-nova-txt-ad-item-title span { font-size: 14px; font-weight: bold; color: #003397 } .imageplus-append-nova-txt .imageplus-append-content .imageplus-append-nova-txt-ad-item .imageplus-append-nova-txt-ad-item-desc span { font-size: 12px; color: #333 } .imageplus-append-jian { width: 20px; height: 20px; background-image: url("http://ecmb.bdimg.com/public03/imageplus_m_append_jian_151204.png"); background-repeat: no-repeat; background-position: 0 0; position: absolute; top: 0; left: 0 } .imageplus-append-close-btn { width: 40px; height: 40px; position: absolute; right: 0; top: 0; background-image: url("http://ecmb.bdimg.com/public03/imageplus_m_append_close_btn_151113.png"); background-repeat: no-repeat; background-position: 0 0; display: none } .imageplus-append-logo { height: 18px; width: 18px; background: url("http://cpro.baidustatic.com/cpro/ui/noexpire/img/2.0.1/bg.png") no-repeat left top; position: absolute; right: 0; bottom: 0 } .imageplus-append-nova-txt-ue2 { font-family: Microsoft YaHei; float: left; border: 1px solid #ddd; border-top: 3px solid #ff2f62; background-color: #f9f9f9 } .imageplus-append-nova-txt-ue2 a:focus { outline: 0 } .imageplus-append-nova-txt-ue2 .imageplus-append-content { float: left } .imageplus-append-nova-txt-ue2 .imageplus-append-content .imageplus-append-nova-txt-ad-item { margin-left: 44px; height: 60px; padding-top: 5px; padding-bottom: 5px } .imageplus-append-nova-txt-ue2 .imageplus-append-content .imageplus-append-nova-txt-ad-item a { text-decoration: none } .imageplus-append-nova-txt-ue2 .imageplus-append-content .imageplus-append-nova-txt-ad-item div { white-space: nowrap; overflow: hidden } .imageplus-append-nova-txt-ue2 .imageplus-append-content .imageplus-append-nova-txt-ad-item .imageplus-append-nova-txt-ad-item-title { height: 30px; line-height: 30px; font-size: 16px } .imageplus-append-nova-txt-ue2 .imageplus-append-content .imageplus-append-nova-txt-ad-item .imageplus-append-nova-txt-ad-item-title a { color: #000 } .imageplus-append-nova-txt-ue2 .imageplus-append-content .imageplus-append-nova-txt-ad-item .imageplus-append-nova-txt-ad-item-title .imageplus-append-nova-txt-title-true { float: left } .imageplus-append-nova-txt-ue2 .imageplus-append-content .imageplus-append-nova-txt-ad-item .imageplus-append-nova-txt-ad-item-title .imageplus-append-nova-txt-title-click { float: left; width: 96px } .imageplus-append-nova-txt-ue2 .imageplus-append-content .imageplus-append-nova-txt-ad-item .imageplus-append-nova-txt-ad-item-title .imageplus-append-nova-txt-title-click a { color: #ff2f62 } .imageplus-append-nova-txt-ue2 .imageplus-append-content .imageplus-append-nova-txt-ad-item .imageplus-append-nova-txt-ad-item-desc { height: 26px; line-height: 26px; font-size: 12px } .imageplus-append-nova-txt-ue2 .imageplus-append-content .imageplus-append-nova-txt-ad-item .imageplus-append-nova-txt-ad-item-desc a { color: #7b7b7b } .imageplus-append-nova-txt-ue2 .imageplus-append-go-btn { float: right; margin-top: 19px; margin-right: 18px } .imageplus-append-nova-txt-ue2 .imageplus-append-go-btn a { text-decoration: none } .imageplus-append-nova-txt-ue2 .imageplus-append-go-btn div { width: 100px; height: 32px; line-height: 32px; text-align: center; background-color: #ff2f62; border: 0; color: #fff; font-family: Microsoft YaHei; font-size: 16px; cursor: pointer } .imageplus-append-nova-txt-ue2 .imageplus-append-jian { position: absolute; top: 3px; left: 10px; width: 22px; height: 40px; background-image: url("http://ecma.bdimg.com/public03/imageplus/append/nova_txt_star_160426.png"); background-position: 0 0; background-repeat: no-repeat } .imageplus-append-nova-txt-ue2 .imageplus-append-close-btn { display: none } .imageplus-append-nova-txt-ue2 .imageplus-append-logo { display: none } .imageplus-append-nova-txt-ue2 .imageplus-baidu-logo { position: absolute; bottom: 0; right: 0; z-index: 9999; background: url("http://ecma.bdimg.com/public03/imageplus/logo.png") no-repeat; background-position: 0 -17px; width: 16px; height: 16px } .imageplus-append-nova-txt-ue2 .imageplus-ad-logo { position: absolute; left: 0; bottom: 0; overflow: hidden; z-index: 12; background: url("http://ecma.bdimg.com/public03/imageplus/logo.png") no-repeat; background-position: 0 0; width: 34px; height: 16px } .imageplus-append { float: none; margin: 0; padding: 0; border: 0; overflow: hidden; visibility: visible; text-align: left; background: transparent; position: relative; text-indent: 0; display: inline-block } .imageplus-append div { float: none; margin: 0; padding: 0; border: 0; overflow: hidden; position: static; display: block; visibility: visible; text-align: left; background: transparent; font-family: Microsoft YaHei; line-height: normal } .imageplus-append a,.imageplus-append img,.imageplus-append span { float: none; margin: 0; padding: 0; border: 0; overflow: visible; position: static; display: inline; visibility: visible; text-align: left; background: transparent; font-family: Microsoft YaHei; line-height: normal }</style>
然后我们再把 Trees 与 Stones 打包成 assetbundle 文件,如图:
从图中我们可以看出 Trees(包括 Stamp7 与 Stamp9 两个预设文件)的大小差不多是 Stamp7 与 Stamp9 大小的一半,这是因为 Stamp7 与 Stamp9 材质相同,在打包的时候,共享一份材质,所以材质只有一份。
下面我们给 Stamp7 附加 R1_Column1 的材质,如图:
然后我们再打包 Stamp7 文件,如图:
从下图中,我们可以看到 Stamp7_1 的文件大小比 R1_Column1 与 Stamp7 都要大,因为 Stamp7_1 包括了 R1_Column1 与 Stamp7 的材质。
然后我们再把 Trees 文件重新打包,从下图中我们依然能发现 Trees 文件也变大了,因为 Trees 文件也包括了 R1_Column1 的材质文件。
总结:到这儿我们可以对预设的注意事项有一些了解了,那就是尽量把材质相同的预设放在一起打包
Unity3D 预设打包的注意事项