图集与使用
来源:互联网 发布:vmware mac os 10.13 编辑:程序博客网 时间:2024/04/30 19:14
UGUI的Atlas和NGUI的Atlas的区别
NGUI是必须先打出图集然后才能开始做界面。因为始终都要去考虑你的UI图集。比如图集会不会超1024 ,图集该如何来规划
UGUI的原理则是,让开发者彻底模糊图集的概念,让开发者不要去关心自己的图集。做界面的时候只用小图,而在最终打包的时候unity才会把你的小图和并在一张大的图集里面。然而这一切一切都是自动完成的,开发者不需要去care它。
打包图集
Editor->Project Settings 下面有sprite packer的模式。
Disabled表示不启用它,
Enabled For Builds 表示只有打包的时候才会启用它,
Always Enabled 表示永远启用它。
这里的启用它就表示是否将小图自动打成图集。
一般选Always Enabled 。
因为开发的时候我们需要清楚的看到现在是几个Draw Call,从而才能优化小图。
在最终打包的时候unity会自动构建大的图集,
可是开发的时候就想看图集会占几个Draw Call,这怎么办呢?
首先将你的图片拖入unity中,将同一图集的所有图片的packing tag设置成一个名子即可。
图集位置
注意你的图片不能放在Resources文件夹下面
Resources文件夹下的资源将不会被打入图集。
然后在Windows->Sprite Packer 里,点击packer 在这里你就可以预览到你的图集信息。
图集的大小还有图集的格式等等很多参数我们都是可以控制的,也可以通过脚本来设置。
它保存在和Assets文件夹同级的目录,Libary/AtlasCache里面。
你不用管它,也不要删除它,就算你删除了也没用因为只要你打包,它就会生成并且会打到包中。
运行时根据图片的名子来动态创建精灵该如何?
做法:先把小图打成图集,然后把所有小图关联在prefab上,
拷贝在Resources文件夹下,这样运行时也能用Resources.load了。
因为:打成图集的图片如果在放在Resources那么资源就变成双份了。所以我们只能把小图关联在Prefab上,把所有的Prefab放在Resources下面,这样就不占用多余的空间了。
每一张图片建一个profab
动态加载图集中精灵
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class UIMain : MonoBehaviour {
void Start ()
{
CreatImage(loadSprite("image0"));
CreatImage(loadSprite("image1"));
}
private void CreatImage(Sprite sprite ){
GameObject go = new GameObject(sprite.name);
go.layer = LayerMask.NameToLayer("UI");
go.transform.parent = transform;
go.transform.localScale= Vector3.one;
Image image = go.AddComponent<Image>();
image.sprite = sprite;
image.SetNativeSize();
}
private Sprite loadSprite(string spriteName){
return Resources.Load<GameObject>("Sprite/" + spriteName).GetComponent<SpriteRenderer>().sprite;
}
}
图集需要在线更新怎么办?
接下来就是Assetbundle了,如果我们的图集需要在线更新那该怎么办呢? 其实Assetbundle比Resources要更简单一些,无论如何我们要先开始打图集。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
[MenuItem ("MyMenu/Build Assetbundle")]
static private void BuildAssetBundle()
{
string dir = Application.dataPath +"/StreamingAssets";
if(!Directory.Exists(dir)){
Directory.CreateDirectory(dir);
}
DirectoryInfo rootDirInfo = new DirectoryInfo (Application.dataPath +"/Atlas");
foreach (DirectoryInfo dirInfo in rootDirInfo.GetDirectories()) {
List<Sprite> assets = new List<Sprite>();
string path = dir +"/"+dirInfo.Name+".assetbundle";
foreach (FileInfo pngFile in dirInfo.GetFiles("*.png",SearchOption.AllDirectories))
{
string allPath = pngFile.FullName;
string assetPath = allPath.Substring(allPath.IndexOf("Assets"));
assets.Add(Resources.LoadAssetAtPath<Sprite>(assetPath));
}
if(BuildPipeline.BuildAssetBundle(null, assets.ToArray(), path,BuildAssetBundleOptions.UncompressedAssetBundle| BuildAssetBundleOptions.CollectDependencies, GetBuildTarget())){
}
}
}
static private BuildTarget GetBuildTarget()
{
BuildTarget target = BuildTarget.WebPlayer;
#if UNITY_STANDALONE
target = BuildTarget.StandaloneWindows;
#elif UNITY_IPHONE
target = BuildTarget.iPhone;
#elif UNITY_ANDROID
target = BuildTarget.Android;
#endif
return target;
}
如下图所示,我的assetbundle已经打出来了。
然后把UIMain.cs在改一改。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class UIMain : MonoBehaviour {
AssetBundle assetbundle = null;
void Start ()
{
CreatImage(loadSprite("image0"));
CreatImage(loadSprite("image1"));
}
private void CreatImage(Sprite sprite ){
GameObject go = new GameObject(sprite.name);
go.layer = LayerMask.NameToLayer("UI");
go.transform.parent = transform;
go.transform.localScale= Vector3.one;
Image image = go.AddComponent<Image>();
image.sprite = sprite;
image.SetNativeSize();
}
private Sprite loadSprite(string spriteName){
#if USE_ASSETBUNDLE
if(assetbundle == null)
assetbundle = AssetBundle.CreateFromFile(Application.streamingAssetsPath +"/Main.assetbundle");
return assetbundle.Load(spriteName) as Sprite;
#else
return Resources.Load<GameObject>("Sprite/" + spriteName).GetComponent<SpriteRenderer>().sprite;
#endif
}
}
- 图集与使用
- UGUI之打包图集与使用
- UGUI技术之全面理解图集与使用浅析
- UGUI技术之全面理解图集与使用浅析
- UGUI研究院之全面理解图集与使用
- Unity_UGUI学习图集与使用(一)
- UGUI研究院之全面理解图集与使用
- Unity3D-UGUI图集打包与动态使用(TexturePacker)
- Unity3D-UGUI图集打包与动态使用(TexturePacker)
- c#与WMI使用技巧集
- c#与WMI使用技巧集
- c#与WMI使用技巧集
- KITTI数据集简介与使用
- KITTI数据集简介与使用
- vim全选方法与键盘使用图
- 使用Highcharts生成折线图与曲线图
- 使用ERStudio创建数据表与ER图
- 使用Highcharts生成折线图与曲线图
- poj 2533 o(nlgn)做法
- boost分类
- 2.2 Unity面向组件
- SQLSERVER游标及循环语句
- 图集分配透明与不透明打包不到一起的错误
- 图集与使用
- 第六章 Realm及相关对象(二) AuthenticationToken
- 2017ROS暑期学校笔记
- 游标嵌套游标的SQL
- 优化Image设置Tiled类型产生过多的三角形顶点
- 训练日记8.23
- RandomAccessFile
- 【JZOJ5329】【NOIP2017提高A组模拟8.22】时间机器
- HDU 2047 阿牛的EOF牛肉串(递推dp)