2011年8月31日 星期三

UNITY3D - 2D圖動畫工具


遊戲中常常會用到一些2D連續圖來做UI特效,但是UNITY 3D本身不支援GIF格式的檔案,基於這些需求而寫了以下這隻程式。
目前的功能有:
1.播放一次
2.循環播放
3.淡入
4.淡出
5.放大
6.縮小
7.上升
8.下降

變數功能:
aniplayers :動畫詳細設定
size:有幾段動畫要播放
aniTexture:放連續圖的位置,需依序放入。
playTime:這段動畫的播放時間長度
frequency:時間內播放次數(淡入、淡出、放大、縮小、上升、下降無作用)
playMode:播放模式(播放一次、循環播放、淡入、淡出、放大、縮小、上升、下降)
waitTime:播放後等待時間(循環播放的作用為多久循環一次)
offset:偏移量,放大、縮小、上升、下降的依據
sync:同步,勾選後會跟下一段動畫同步播放
destroy:動畫播放完是否銷毀此GameObject

注意事項:
若勾選destroy時,至少要有一段動畫不能勾選sync

=======================================================================

enum AniPlayMode {PlayOnce,PlayCycle,FadeIn,FadeOut,Magnify,Minify,Up,Down};
class ANIPLAYER {
var aniTexture : Texture[];
@HideInInspector var startTime : float;
var playTime : float;
var frequency : int;
var playMode : AniPlayMode;
var waitTime : float;
var offset : float;
var sync : boolean;
}

var aniplayers : ANIPLAYER[];
var destroy : boolean;

@script RequireComponent (GUITexture)

function OnEnable () {
Play();
}

function Play () {
for(var aniplayer : ANIPLAYER in aniplayers)
{
if(aniplayer.playMode == AniPlayMode.PlayOnce)
{
if(aniplayer.sync)
PlayOnce(aniplayer);
else
yield PlayOnce(aniplayer);
}
else if(aniplayer.playMode == AniPlayMode.PlayCycle)
{
if(aniplayer.sync)
PlayCycle(aniplayer);
else
yield PlayCycle(aniplayer);
}
else if(aniplayer.playMode == AniPlayMode.FadeIn)
{
if(aniplayer.sync)
FadeIn(aniplayer);
else
yield FadeIn(aniplayer);
}
else if(aniplayer.playMode == AniPlayMode.FadeOut)
{
if(aniplayer.sync)
FadeOut(aniplayer);
else
yield FadeOut(aniplayer);
}
else if(aniplayer.playMode == AniPlayMode.Magnify)
{
if(aniplayer.sync)
Magnify(aniplayer);
else
yield Magnify(aniplayer);
}
else if(aniplayer.playMode == AniPlayMode.Minify)
{
if(aniplayer.sync)
Minify(aniplayer);
else
yield Minify(aniplayer);
}
else if(aniplayer.playMode == AniPlayMode.Up)
{
if(aniplayer.sync)
Up(aniplayer);
else
yield Up(aniplayer);
}
else if(aniplayer.playMode == AniPlayMode.Down)
{
if(aniplayer.sync)
Down(aniplayer);
else
yield Down(aniplayer);
}
}
if(destroy)
{
Destroy(gameObject);
}
}

function PlayOnce (aniplayer : ANIPLAYER) {
var index : int;
aniplayer.startTime = 0.0f;
while(aniplayer.startTime <= aniplayer.playTime)
{
index = (aniplayer.startTime * (aniplayer.aniTexture.Length / aniplayer.playTime) * aniplayer.frequency) % aniplayer.aniTexture.Length;
guiTexture.texture = aniplayer.aniTexture[index];
aniplayer.startTime = aniplayer.startTime + Time.deltaTime;
yield;
}
yield WaitForSeconds(aniplayer.waitTime);
}

function PlayCycle (aniplayer : ANIPLAYER) {
var index : int;
aniplayer.startTime = 0.0f;
while(true)
{
while(aniplayer.startTime <= aniplayer.playTime)
{
index = (aniplayer.startTime * (aniplayer.aniTexture.Length / aniplayer.playTime) * aniplayer.frequency) % aniplayer.aniTexture.Length;
guiTexture.texture = aniplayer.aniTexture[index];
aniplayer.startTime = aniplayer.startTime + Time.deltaTime;
yield;
}
yield WaitForSeconds(aniplayer.waitTime);
aniplayer.startTime = 0.0f;
}
}

function FadeIn (aniplayer : ANIPLAYER) {
aniplayer.startTime = 0.0f;
guiTexture.color.a = 0;
while(aniplayer.startTime <= aniplayer.playTime)
{
aniplayer.startTime = aniplayer.startTime + Time.deltaTime;
guiTexture.color.a = guiTexture.color.a + (Time.deltaTime / aniplayer.playTime);
yield;
}
yield WaitForSeconds(aniplayer.waitTime);
}

function FadeOut (aniplayer : ANIPLAYER) {
aniplayer.startTime = 0.0f;
while(aniplayer.startTime <= aniplayer.playTime)
{
aniplayer.startTime = aniplayer.startTime + Time.deltaTime;
guiTexture.color.a = guiTexture.color.a - (Time.deltaTime / aniplayer.playTime);
yield;
}
yield WaitForSeconds(aniplayer.waitTime);
}

function Magnify (aniplayer : ANIPLAYER) {
var scale : float = guiTexture.pixelInset.width / guiTexture.pixelInset.height;
aniplayer.startTime = 0.0f;
while(aniplayer.startTime <= aniplayer.playTime)
{
aniplayer.startTime = aniplayer.startTime + Time.deltaTime;
guiTexture.pixelInset.width = guiTexture.pixelInset.width + Time.deltaTime * aniplayer.offset * scale;
guiTexture.pixelInset.height = guiTexture.pixelInset.height + Time.deltaTime * aniplayer.offset;
guiTexture.pixelInset.x = guiTexture.pixelInset.x - Time.deltaTime * aniplayer.offset * scale / 2;
guiTexture.pixelInset.y = guiTexture.pixelInset.y - Time.deltaTime * aniplayer.offset / 2;
yield;
}
yield WaitForSeconds(aniplayer.waitTime);
}

function Minify (aniplayer : ANIPLAYER) {
var scale : float = guiTexture.pixelInset.width / guiTexture.pixelInset.height;
aniplayer.startTime = 0.0f;
while(aniplayer.startTime <= aniplayer.playTime)
{
aniplayer.startTime = aniplayer.startTime + Time.deltaTime;
guiTexture.pixelInset.width = guiTexture.pixelInset.width - Time.deltaTime * aniplayer.offset * scale;
guiTexture.pixelInset.height = guiTexture.pixelInset.height - Time.deltaTime * aniplayer.offset;
guiTexture.pixelInset.x = guiTexture.pixelInset.x + Time.deltaTime * aniplayer.offset * scale / 2;
guiTexture.pixelInset.y = guiTexture.pixelInset.y + Time.deltaTime * aniplayer.offset / 2;
yield;
}
yield WaitForSeconds(aniplayer.waitTime);
}

function Up (aniplayer : ANIPLAYER) {
aniplayer.startTime = 0.0f;
while(aniplayer.startTime <= aniplayer.playTime)
{
aniplayer.startTime = aniplayer.startTime + Time.deltaTime;
guiTexture.pixelInset.y = guiTexture.pixelInset.y + Time.deltaTime * aniplayer.offset;
yield;
}
yield WaitForSeconds(aniplayer.waitTime);
}

function Down (aniplayer : ANIPLAYER) {
aniplayer.startTime = 0.0f;
while(aniplayer.startTime <= aniplayer.playTime)
{
aniplayer.startTime = aniplayer.startTime + Time.deltaTime;
guiTexture.pixelInset.y = guiTexture.pixelInset.y - Time.deltaTime * aniplayer.offset;
yield;
}
yield WaitForSeconds(aniplayer.waitTime);
}

沒有留言:

張貼留言