Unity parses and displays / plays GIF images, supports HTTP URL, supports local file: / /, supports pause and resume playback

Lin Xinfa 2021-04-07 17:41:17
unity parses displays plays gif

One 、 Preface

Focus on not getting lost , Continuous output Unity Dry article .

hi , Hello everyone , I'm Xinfa . We know ,Unity I don't support it gif Picture display , this paper , I'll show you how to Unity It shows that gif Picture bar .
The final effect of this paper is :
 Insert picture description here
this paper Demo The project has been uploaded to CodeChina, Interested students can download and learn by themselves .
Address :https://codechina.csdn.net/linxinfa/UnityShowGifDemo
notes : I use Unity edition :2020.2.7f1c1 (64-bit).
 Insert picture description here

Two 、 Specific operation

1、 Prepare a gif picture

 Insert picture description here
Put it in StreamingAssets In the folder .
 Insert picture description here

2、UniGifImage Components

UniGifImage Component source code, see Demo engineering )
Create a RawImage, Hang up UniGifImage Components , This will automatically hang up UniGifImageAspectController Components , as follows :
 Insert picture description here
UniGifImage Components provide the following main interfaces :

// Set up gif, The coroutine is called internally SetGifFromUrlCoroutine,url Support http/https And local paths , The local path is from StreamingAssets Folder read 
public void SetGifFromUrl(string url, bool autoPlay = true)
// Set up gif
public IEnumerator SetGifFromUrlCoroutine(string url, bool autoPlay = true)
// Play 
public void Play()
// stop it 
public void Stop()
// Pause 
public void Pause()
// continue 
public void Resume()

Set it up UniGifImage Parameters of components :
 Insert picture description here
Parameter description :

Parameters explain
RawImage To render gif The goal of RawImage
ImgAspectCtrl gif Size adapter , Will be based on RawImage Final adaptation gif The size of the display
FilterMode Texture filtering mode
WrapMode Texture surround mode
LoadOnStart Whether in script Start Load it when it's loaded gif
LoadOnStartUrl Script Start When it's loaded gif Of url Address
ROtateOnLoading Whether to rotate the picture while loading
OutputDebgLog Whether to output debug log

3、 test

For testing purposes , Make some simple UI.
 Insert picture description here
Write a test script

using System.Collections;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// gif Show tests 
/// </summary>
public class UniGifTest : MonoBehaviour

public InputField gifUrlInput;
public UniGifImage gifImage;
public Button loadBtn;
public Button playBtn;
public Button stopBtn;
public Button pauseBtn;
public Button resumeBtn;
/// <summary>
/// lock 
/// </summary>
private bool m_mutex;
private void Start()

loadBtn.onClick.AddListener(() =>

if (m_mutex || gifImage == null || string.IsNullOrEmpty(gifUrlInput.text))

m_mutex = true;
playBtn.onClick.AddListener(() => {
 gifImage.Play(); });
stopBtn.onClick.AddListener(() => {
 gifImage.Stop(); });
pauseBtn.onClick.AddListener(() => {
 gifImage.Pause(); });
resumeBtn.onClick.AddListener(() => {
 gifImage.Resume(); });
private IEnumerator ViewGifCoroutine()

yield return StartCoroutine(gifImage.SetGifFromUrlCoroutine(gifUrlInput.text));
m_mutex = false;

Create a UniGifTest An empty object , Hang up UniGifTest Script , Assignment parameter object , as follows :
 Insert picture description here
function Unity, The effect is as follows :
 Insert picture description here

3、 ... and 、 Conclusion

complete .
like Unity Classmate , Don't forget to click follow , If there is anything Unity Related technical problems , Also welcome to leave a message or private message ~

本文为[Lin Xinfa]所创,转载请带上原文链接,感谢

  1. css单位
  2. CSS unit
  3. 前端验证集合
  4. Front end validation collection
  5. 前端学习(四)-----响应行,响应头,响应体
  6. Front end learning (4) -- response line, response head, response body
  7. Lesson 27: using time measuring tools to explore available spare time
  8. CSS 单位
  9. CSS unit
  10. 前端工具集合
  11. Front end Tools Collection
  12. Basic principles and settings of HTTPS
  13. Basic principles and settings of HTTPS
  14. Programming deer Vue.js This is enough for the second chapter of "learning ten thousand words to be easy to understand"
  15. Introduction to HTTP message format
  16. 轻应用介绍 - 用JavaScript进行嵌入式开发
  17. Vue入门常用指令
  18. CSS notes -- showing and hiding elements
  19. Introduction to new features of HTML5
  20. Common instructions for Vue
  21. Programming deer Vue.js This is enough for the second chapter of "learning ten thousand words to be easy to understand"
  22. BootstrapBlazor 组件库介绍
  23. Introduction of bootstrap Blazer component library
  24. HTML5 速览
  25. HTML5 quick view
  26. HTML表格自动排序
  27. For less than $1700, create a private cloud with openstack
  28. HTTP状态码分类
  29. Basic learning of JavaScript (1)
  30. 前端面试之HTTP协议的东西!
  31. 前端编程之路一一HTML标签分类
  32. Front end Programming: HTML tag classification
  33. ColSpan and rowspan merging cells of HTML form elements
  34. JavaScript 日期时间格式化
  35. JavaScript date time formatting
  36. Classification of front end interview questions - html2
  37. Vue dynamic form editor and code generator based on vuetify
  38. JavaScript
  39. HTML学习
  40. HTML learning
  41. CSS块元素
  42. Crawler series | 2, HTTP request and response, write crawler these contents must master
  43. Bitwise operators in JavaScript
  44. CSS block elements
  45. Summary of HTML5 knowledge points (5)
  46. HTML5表格详细教程
  47. HTML5 form detailed tutorial
  48. HTML5表格详细教程
  49. HTML5 form detailed tutorial
  50. javascript当中delTRTDIEFF的用法
  51. HTML5新增的元素表
  52. The usage of deltrtdieff in JavaScript
  53. New element table in HTML5
  54. 3D轮播插件vue-carousel-3d非官方最全文档
  55. 3D carousel plug-in vue-carousel-3d
  56. uniapp(vue通用)整合腾讯位置服务SDK---多平台小程序通用
  57. Uniapp (Vue general) integrates Tencent location service SDK -- multi platform small program general
  58. Slowhttptest slow attack tool use details
  59. 喜大普奔,微信终于支持外网打开小程序啦!
  60. Xi Da Pu Ben, wechat finally supports the opening of small programs on the Internet!