用unity播放Gif图片

总的来说,这个工具并没有什么大用,没有直接把GIF图切割之后使用来的更节省内存和更少的运行时间。算是个人的练手之作吧。

使用说明:

  1. 将CreateGifSTO和GifSpriteInspector脚本放入Editor文件夹
  2. 右键gif文件选择STO/CreateGifAsset
  3. 创建一个Image然后去掉Image组件并添加GifSprite组件
  4. 将第二步创建的资源拖到SourceSprite中即可(需要将脚本前面的勾去掉,不然拖不上去)
  5. 运行时按q键即可查看效果

CreateGifSTO.cs

using System.Collections;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.IO;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
using System.Runtime.InteropServices;

public static class GifSTOCreater
{
   
    [MenuItem("Assets/STO/CreateGifAsset %#SPACE")]
    public static void CreateAsset()
    {
   
        if (Selection.activeObject == null)
        {
   
            Debug.Log("你选择的对象为空,不能进行");
            return;
        }
        if (AssetDatabase.Contains(Selection.activeObject) == false)
        {
   
            Debug.Log("请选择Assets文件夹下资源");
            return;
        }
        if (Path.GetExtension(AssetDatabase.GetAssetPath(Selection.activeObject)) != ".gif")
        {
   
            Debug.Log("请选择Gif文件");
            return;
        }
        //获取选择文件的全部路径
        string sourcePath = Application.dataPath.Substring(0, Application.dataPath.Length - "Assets".Length) + AssetDatabase.GetAssetPath(Selection.activeObject);
        //创建对应的ScriptableObject
        var sto = ScriptableObject.CreateInstance<GifScriptableObject>();
        //创建相对路径
        string relativePath = Path.ChangeExtension(AssetDatabase.GetAssetPath(Selection.activeObject), ".asset");
        //获取选中的Gif的所有帧的数据,并存入GifAllSprite中
        GetAllSprite(ref sto.GifAllSprite, sourcePath, sto, relativePath);
        //将GifAllSprite存为sto的子物体,此处需要分开存,因为不能将List转为UnityEngine.Object(只有Unity自带的类型可以存)
        for (
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值