溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Unity3D如何實現(xiàn)播放gif圖功能

發(fā)布時間:2021-05-24 14:05:51 來源:億速云 閱讀:1693 作者:小新 欄目:編程語言

這篇文章將為大家詳細講解有關Unity3D如何實現(xiàn)播放gif圖功能,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

Unity是不識別Gif格式圖的,需要我們使用c#將gif里多幀圖轉化為Texture2D格式。需要使用System.Drawing.dll.此dll在unity安裝目錄下就可以找到。由于unity沒有gif格式的文件,所以我們無法在面板指定,需要動態(tài)加載。所以將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<Texture2D> tex2DList = new List<Texture2D>();
  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<Renderer>().material.mainTexture = tex2DList[index];
 
      }
    }
  }
  private List<Texture2D> MyGif(System.Drawing.Image image)
  {
 
    List<Texture2D> tex = new List<Texture2D>();
    if (image != null)
    {
 
      //Debug.Log("圖片張數(shù):" + image.FrameDimensionsList.Length);
      FrameDimension frame = new FrameDimension(image.FrameDimensionsList[0]);
      int framCount = image.GetFrameCount(frame);//獲取維度幀數(shù)
      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);
      // 創(chuàng)建一個字節(jié)數(shù)組,長度為流的長度
      byte[] data = new byte[stream.Length];
      // 重置指針
      stream.Seek(0, SeekOrigin.Begin);
      // 從流讀取字節(jié)塊存入data中
      stream.Read(data, 0, Convert.ToInt32(stream.Length));
      return data;
    }
  }
}

關于“Unity3D如何實現(xiàn)播放gif圖功能”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI