UniGif.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. UniGif
  3. Copyright (c) 2015 WestHillApps (Hironari Nishioka)
  4. This software is released under the MIT License.
  5. http://opensource.org/licenses/mit-license.php
  6. */
  7. using System;
  8. using System.Collections;
  9. using System.Collections.Generic;
  10. using UnityEngine;
  11. public static partial class UniGif
  12. {
  13. /// <summary>
  14. /// Get GIF texture list Coroutine
  15. /// </summary>
  16. /// <param name="bytes">GIF file byte data</param>
  17. /// <param name="callback">Callback method(param is GIF texture list, Animation loop count, GIF image width (px), GIF image height (px))</param>
  18. /// <param name="filterMode">Textures filter mode</param>
  19. /// <param name="wrapMode">Textures wrap mode</param>
  20. /// <param name="debugLog">Debug Log Flag</param>
  21. /// <returns>IEnumerator</returns>
  22. public static IEnumerator GetTextureListCoroutine(
  23. byte[] bytes,
  24. Action<List<GifTexture>, int, int, int> callback,
  25. FilterMode filterMode = FilterMode.Bilinear,
  26. TextureWrapMode wrapMode = TextureWrapMode.Clamp,
  27. bool debugLog = false)
  28. {
  29. int loopCount = -1;
  30. int width = 0;
  31. int height = 0;
  32. // Set GIF data
  33. var gifData = new GifData();
  34. if (SetGifData(bytes, ref gifData, debugLog) == false)
  35. {
  36. Debug.LogError("GIF file data set error.");
  37. if (callback != null)
  38. {
  39. callback(null, loopCount, width, height);
  40. }
  41. yield break;
  42. }
  43. // Decode to textures from GIF data
  44. List<GifTexture> gifTexList = null;
  45. yield return DecodeTextureCoroutine(gifData, result => gifTexList = result, filterMode, wrapMode);
  46. if (gifTexList == null || gifTexList.Count <= 0)
  47. {
  48. Debug.LogError("GIF texture decode error.");
  49. if (callback != null)
  50. {
  51. callback(null, loopCount, width, height);
  52. }
  53. yield break;
  54. }
  55. loopCount = gifData.m_appEx.loopCount;
  56. width = gifData.m_logicalScreenWidth;
  57. height = gifData.m_logicalScreenHeight;
  58. if (callback != null)
  59. {
  60. callback(gifTexList, loopCount, width, height);
  61. }
  62. yield break;
  63. }
  64. }