总的来说,这个工具并没有什么大用,没有直接把GIF图切割之后使用来的更节省内存和更少的运行时间。算是个人的练手之作吧。
使用说明:
- 将CreateGifSTO和GifSpriteInspector脚本放入Editor文件夹
- 右键gif文件选择STO/CreateGifAsset
- 创建一个Image然后去掉Image组件并添加GifSprite组件
- 将第二步创建的资源拖到SourceSprite中即可(需要将脚本前面的勾去掉,不然拖不上去)
- 运行时按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 (