關於Unity 動畫繪制原理


動畫繪制的原理:現在屏幕中確定動畫的顯示區域,然后將動畫中的每一幀圖片按固定的時間在這個區域中按順序切換,從而實現動畫的效果。建立文件夾Textures,里面放上圖片,此處我截了三張圖,風怒的小鳥圖片放在該文件夾下,運行結果如下圖,點擊相關按鈕小鳥做相關移動,腳本如下:

 private var anim:Object[];//動畫數組

private var nowFram:int;//幀序列

private var mFrameCount:int;//動畫幀總數

private var fps:float=5;//限制一秒多少幀

private var time:float=0;//限制幀的時間

var x:int;

var y:int;

var tex:Object[];

 

var bg:Texture2D;//背景圖片

function Start () {

    anim=Resources.LoadAll("Textures");//得到幀動畫中所有圖片資源

    mFrameCount=anim.Length;//得到動畫有多少幀

    bg=Resources.Load("next_bg");

}

 

function Update () {

 

}

function OnGUI(){

   //繪制背景圖片

   GUI.DrawTexture(Rect(0,0,Screen.width,Screen.height),bg,ScaleMode.StretchToFill,true,0);

   

   //調用DrawAnimation,參數一:動畫數組,參數二:動畫顯示區域

   DrawAnimation(anim,Rect(x,y,32,48));//全局變量x,y確定主角的方向

   if(GUILayout.RepeatButton("up"))

   {

      y-=2;

      tex=anim;

   }

   if(GUILayout.RepeatButton("down"))

   {

      y+=2;

      tex=anim;

   }

   if(GUILayout.RepeatButton("left"))

   {

      x-=2;

      tex=anim;

   }

   if(GUILayout.RepeatButton("right"))

   {

      x+=2;

      tex=anim;

   }

}

 

 

function DrawAnimation(tex:Object[],rect:Rect)

{

   GUILayout.Label("當前動畫播放:地"+nowFram+"");

   GUI.DrawTexture(rect,anim[nowFram],ScaleMode.StretchToFill ,true,0);//繪制動畫數組

   //計算限制幀時間

   time+=Time.deltaTime;

   if(time>=1.0/fps)

   {

      nowFram++;//幀切換

      time=0;//切換后限制幀的時間歸零,從新計算

      if(nowFram>=mFrameCount)

      {

          nowFram=0;//若當前幀到達最后一幀,那么在從第0幀開始播放

      }

   }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM