资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Unity3D如何实现播放gif图功能

这篇文章将为大家详细讲解有关Unity3D如何实现播放gif图功能,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

创新互联公司专业为企业提供广德网站建设、广德做网站、广德网站设计、广德网站制作等企业网站建设、网页设计与制作、广德企业网站模板建站服务,10多年广德做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

Unity是不识别Gif格式图的,需要我们使用c#将gif里多帧图转化为Texture2D格式。需要使用System.Drawing.dll.此dll在unity安装目录下就可以找到。由于unity没有gif格式的文件,所以我们无法在面板指定,需要动态加载。所以将gif图放在StreamingAssets文件夹下。以下为源代码:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using UnityEngine;
 
public class PlayGif : MonoBehaviour {
 
  public UnityEngine.UI.Image Im;
  public string gifName = "";
  public GameObject[] Ims;
  [SerializeField]
  private float fps = 5f;
  private List tex2DList = new List();
  private float time;
  Bitmap mybitmp;
  void Start()
  {
    System.Drawing.Image image = System.Drawing.Image.FromFile(Application.streamingAssetsPath + "/"+gifName+".gif");
    tex2DList = MyGif(image);
  }
 
  // Update is called once per frame
  void Update()
  {
    if (tex2DList.Count > 0)
    {
      time += Time.deltaTime;
      int index = (int)(time * fps) % tex2DList.Count;
      if (Im != null)
      {
        Im.sprite = Sprite.Create(tex2DList[index], new Rect(0, 0, tex2DList[index].width, tex2DList[index].height), new Vector2(0.5f, 0.5f));
      }
      if (Ims.Length != 0)
      {
        for (int i = 0; i < Ims.Length; i++)
          Ims[i].GetComponent().material.mainTexture = tex2DList[index];
 
      }
    }
  }
  private List MyGif(System.Drawing.Image image)
  {
 
    List tex = new List();
    if (image != null)
    {
 
      //Debug.Log("图片张数:" + image.FrameDimensionsList.Length);
      FrameDimension frame = new FrameDimension(image.FrameDimensionsList[0]);
      int framCount = image.GetFrameCount(frame);//获取维度帧数
      for (int i = 0; i < framCount; ++i)
      {
 
        image.SelectActiveFrame(frame, i);
        Bitmap framBitmap = new Bitmap(image.Width, image.Height);
        using (System.Drawing.Graphics graphic = System.Drawing.Graphics.FromImage(framBitmap))
        {
          graphic.DrawImage(image, Point.Empty);
        }
        Texture2D frameTexture2D = new Texture2D(framBitmap.Width, framBitmap.Height, TextureFormat.ARGB32, true);
        frameTexture2D.LoadImage(Bitmap2Byte(framBitmap));
        tex.Add(frameTexture2D);
      }
    }
    return tex;
  }
  private byte[] Bitmap2Byte(Bitmap bitmap)
  {
    using (MemoryStream stream = new MemoryStream())
    {
      // 将bitmap 以png格式保存到流中
      bitmap.Save(stream, ImageFormat.Png);
      // 创建一个字节数组,长度为流的长度
      byte[] data = new byte[stream.Length];
      // 重置指针
      stream.Seek(0, SeekOrigin.Begin);
      // 从流读取字节块存入data中
      stream.Read(data, 0, Convert.ToInt32(stream.Length));
      return data;
    }
  }
}

关于“Unity3D如何实现播放gif图功能”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。


本文标题:Unity3D如何实现播放gif图功能
文章位置:http://cdkjz.cn/article/gjejpc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220