Unity3D 实用技巧 - 轻松掌握生成Gif动态图与播放
第一部分:录制一、掌握快速生成Gif 动图。其实方式有多种,小编这边直接介绍常用的方式。 方式一: 1、我们知道Unity3D 自带录屏Unity Recorder 插件,里面包括生成gif图的功能。Window->Package Manager 直接搜索Unity Recorder关键字。 2、然后再选择Window -> General -> Recorder 面板,选择Gi
·
第一部分:录制
一、掌握快速生成Gif 动图。其实方式有多种,小编这边直接介绍常用的方式。 方式一: 1、我们知道Unity3D 自带录屏Unity Recorder 插件,里面包括生成gif图的功能。Window->Package Manager 直接搜索Unity Recorder关键字。 2、然后再选择Window -> General -> Recorder 面板,选择Gif 动画格式即可实现。


方式二: 利用第三方的插件分享,小编特别推荐NatCorder插件。 A、Pro GIF Recorder Toolkit: https://assetstore.unity.com/packages/tools/integration/pro-gif-recorder-toolkit-98258 B、Very Simple GIF: https://assetstore.unity.com/packages/tools/integration/very-simple-gif-71990 C、NatCorder - Video Recording API: https://assetstore.unity.com/packages/tools/integration/very-simple-gif-71990?q=Natcord&orderBy=1
下面是Natcorder的实操: 1、项目导入插件后,直接设置场景与对象。挂上Giffy组件。 2、直接运行项目,实时生成Gif动图。 Gif示例图:




第二部分:播放
Unity目前对图形图像的支持还不是很完善,支持的图形文件格式有 PSD, TIFF, JPG, TGA, PNG, GIF, BMP, IFF, PICT等,但是针对Gif动画,特别有第三方的插件可以支持。 A、Gif Play Plugin: https://assetstore.unity.com/packages/2d/environments/gif-play-plugin-116943 B、Power GIF Runtime Libary + GIPHY API: https://assetstore.unity.com/packages/tools/animation/power-gif-runtime-libary-giphy-api-120039
1、另外,小编这边直接实操一个简单使用的项目。适用于Window 平台,首先复制”"System.Drawing.dll" file in the "C:\Program Files (x86)\Unity\Editor\Data\Mono\lib\mono\2.0(4.0)"文件到"Assets" 文件夹下面; 2、然后新建StreamingAssets文件夹,并把命好名称Gif图资源放进去; 3、PlayGifAction.cs 关键代码:
using
System
;
using
System
.
Collections
;
using
System
.
Collections
.
Generic
;
using
System
.
Drawing
;
using
System
.
Drawing
.
Imaging
;
using
System
.
IO
;
using
UnityEngine
;
/// <summary>
/// Gif 播放类
/// </summary>
public
class
PlayGifAction
:
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
=
MyGifSet
(
image
)
;
}
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
>
MyGifSet
(
System
.
Drawing
.
Image
image
)
{
List
<
Texture2D
>
tex
=
new
List
<
Texture2D
>
(
)
;
if
(
image
!=
null
)
{
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
;
}
}
}
4、直接运行项目,即可预览Gif图播放显示。

更多推荐
所有评论(0)