当前位置:   article > 正文

[Unity优化]UGUI图集的使用_ugui的图集需要放入resources么

ugui的图集需要放入resources么

参考链接:

http://www.xuanyusong.com/archives/3304

http://www.xuanyusong.com/archives/3315


Sprite Packer的使用:

http://liweizhaolili.blog.163.com/blog/static/1623074420131151303310/


关键点:

1.在unity5中,Statistics面板中的Batches即为drawcall。并且,新建一个空场景,drawcall的初始值为2,所以一般drawcall的计算要从2开始

2.在导入图片时,最好就是一个图集对应一个文件夹,文件夹的名字即为图集的名字


  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.IO;
  4. public class AutoSetPackingTag : AssetPostprocessor {
  5. void OnPostprocessTexture(Texture2D texture)
  6. {
  7. string atlasName = new DirectoryInfo(Path.GetDirectoryName(assetPath)).Name;
  8. TextureImporter textureImporter = assetImporter as TextureImporter;
  9. textureImporter.textureType = TextureImporterType.Sprite;
  10. textureImporter.spritePackingTag = atlasName;
  11. textureImporter.mipmapEnabled = false;
  12. }
  13. }

3.需要打包成图集的图片不能放在Resources文件夹内,也就是说,我们无法通过Resources.Load的方式加载图片。对此,有两种解决方法:

a.将图片保存到prefab中,prefab放到Resources文件夹内。不过当需要加载的图片比较多时,这种方法就比较麻烦。可以使用ScriptableObject把图片序列化保存,这样会比较方便一些

  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEditor;
  5. public class SpriteScriptableObject : ScriptableObject {
  6. public List<Sprite> list = new List<Sprite>();
  7. //字典无法被序列化
  8. public Dictionary<string, Sprite> nameSpriteDir = new Dictionary<string, Sprite>();
  9. public void ReadData(string dataDir)
  10. {
  11. list = new List<Sprite>();
  12. string[] filePaths = Directory.GetFiles(dataDir);
  13. foreach(string s in filePaths)
  14. {
  15. if (!s.EndsWith(".meta"))
  16. {
  17. Sprite sprite = AssetDatabase.LoadAssetAtPath<Sprite>(s);
  18. list.Add(sprite);
  19. Debug.Log(sprite.name);
  20. }
  21. }
  22. }
  23. public void Init()
  24. {
  25. for (int i = 0; i < list.Count; i++)
  26. {
  27. nameSpriteDir.Add(list[i].name, list[i]);
  28. }
  29. }
  30. }

  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEditor;
  4. using System.IO;
  5. using System.Reflection;
  6. using System;
  7. public enum ScriptableObjectType
  8. {
  9. SpriteScriptableObject,
  10. }
  11. public class CreateScriptableObject : EditorWindow {
  12. static string dataDir;
  13. static string outputDir = "Assets/";
  14. static string name;
  15. static ScriptableObjectType scriptableObjectType;
  16. [MenuItem("Window/CreateScriptableObject")]
  17. static void Init()
  18. {
  19. EditorWindow.GetWindow(typeof(CreateScriptableObject));
  20. }
  21. void OnGUI()
  22. {
  23. EditorGUILayout.BeginHorizontal();
  24. dataDir = EditorGUILayout.TextField("数据目录", dataDir);
  25. if (GUILayout.Button("拾取路径")) dataDir = AssetDatabase.GetAssetPath(Selection.objects[0]);
  26. EditorGUILayout.EndHorizontal();
  27. EditorGUILayout.BeginHorizontal();
  28. outputDir = EditorGUILayout.TextField("输出目录", outputDir);
  29. if (GUILayout.Button("拾取路径")) outputDir = AssetDatabase.GetAssetPath(Selection.objects[0]);
  30. EditorGUILayout.EndHorizontal();
  31. name = EditorGUILayout.TextField("名字", name);
  32. scriptableObjectType = (ScriptableObjectType)EditorGUILayout.EnumPopup("类型", scriptableObjectType);
  33. if (GUILayout.Button("输出"))
  34. {
  35. //Type.GetType对于继承UnityEngine.Object的类会返回空
  36. //string className = scriptableObjectType.ToString();
  37. //Debug.Log(className);
  38. //Type t = Type.GetType(className);
  39. //if (t == null) Debug.Log("null");
  40. switch (scriptableObjectType)
  41. {
  42. case ScriptableObjectType.SpriteScriptableObject :
  43. SpriteScriptableObject so = ScriptableObject.CreateInstance<SpriteScriptableObject>();
  44. so.ReadData(dataDir);
  45. AssetDatabase.CreateAsset(so, outputDir + name + ".asset");
  46. break;
  47. default:
  48. break;
  49. }
  50. AssetDatabase.Refresh();
  51. }
  52. }
  53. }



  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine.UI;
  5. public class NewBehaviourScript1 : MonoBehaviour {
  6. public Image[] images;
  7. // Use this for initialization
  8. void Start ()
  9. {
  10. SpriteScriptableObject sso = Resources.Load<SpriteScriptableObject>("2");
  11. sso.Init();
  12. Debug.Log(sso.list.Count);
  13. Debug.Log(sso.nameSpriteDir.Count);
  14. images[0].sprite = sso.nameSpriteDir["a"];
  15. images[1].sprite = sso.nameSpriteDir["b"];
  16. images[2].sprite = sso.nameSpriteDir["c"];
  17. }
  18. }


b.使用AB包的形式。将同一个图集的小图的AB包名字设置为相同的,即可将这些小图打包到同一个AB包中

  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. public class NewBehaviourScript : MonoBehaviour {
  5. public Image[] images;
  6. // Use this for initialization
  7. void Start ()
  8. {
  9. AssetbBundleManager.Instance.Load(@"file:///" + Application.dataPath + "/AB/", "2.unity3d",
  10. (ab, name) =>
  11. {
  12. Sprite a = AssetbBundleManager.Instance.GetAsset<Sprite>(ab, "a");
  13. images[0].sprite = a;
  14. Sprite b = AssetbBundleManager.Instance.GetAsset<Sprite>(ab, "b");
  15. images[1].sprite = b;
  16. Sprite c = AssetbBundleManager.Instance.GetAsset<Sprite>(ab, "c");
  17. images[2].sprite = c;
  18. });
  19. }
  20. }

4.图集占内存的计算:对于一张1024*1024的RGBA图集,占4M内存(1024*1024*4)

5.如果把带透明通道和不带透明通道的小图打包到同一图集,unity会把它分成两组


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/636919
推荐阅读
相关标签
  

闽ICP备14008679号