關於在unity中使用序列幀動畫
阿新 • • 發佈:2017-08-03
atime highlight ++ switch ati 是你 一秒 tor 報錯
//動畫數組 public object[] anim; //限制一秒多少幀 public float fps = 30; //幀序列 private int nowFram; //記錄當前時間 private float switchTime; public string path = "Texture/33"; public bool isLoop = false; public Image image; public Texture2D texture; void Awake() { anim = Resources.LoadAll(path); } void Update() { if (anim == null) { anim = Resources.LoadAll(path); } DrawAnimation(anim); } void DrawAnimation(object[] tx) { switchTime += Time.deltaTime; if (switchTime >= 1.0 / fps) { nowFram++; switchTime = 0; if (nowFram >= tx.Length) { if (isLoop) nowFram = 0; else return; } } if (nowFram < tx.Length) { texture = (Texture2D)tx[nowFram]; Sprite sp = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f)); image.sprite = sp; } }
如果在運行時,出現報錯,說是類型無法轉換的錯誤的話。可能是你把圖片類型轉換成sprite了。我們在unity中在把塔轉換成texture格式在運行就不會報錯了。
關於在unity中使用序列幀動畫