2014年4月24日 星期四

[Unity3D] animation material 動畫材質的作法

我這邊寫了一個class,還不夠完美,先湊合著用吧^^
=========================================================
using UnityEngine;
using System.Collections;

public class AnimPlane : MonoBehaviour {

public Texture[] frames;
public bool loop = false;
public float delayTime = 0.01f;
public bool bAlwaysFaceToUser = false;

Quaternion offset=Quaternion.Euler(-90, 0, 0);
Transform  cam;
bool       bEnableAnim = false;

void Start () {

}

        // Use this for initialization
protected void Init(){
gameObject.SetActive(false);

cam=Camera.main.transform;
}

// Update is called once per frame
void Update () {
if(bAlwaysFaceToUser){
Quaternion rot=cam.rotation*offset;

transform.rotation=rot;
Vector3 dirRight=transform.TransformDirection(-Vector3.right);
}
}

public void PlayAnim()
{
gameObject.SetActive(true);
bEnableAnim = true;
StartCoroutine(IEPlayAnim());
}

public void StopAnim()
{
bEnableAnim = false;
StopCoroutine("IEPlayAnim");
gameObject.SetActive(false);
}

IEnumerator IEPlayAnim()
{
int index = 0;
while(bEnableAnim)
{
renderer.material.mainTexture = frames[index];
index++;
if(index >= frames.Length) {
if(loop) index = 0;
else{
gameObject.SetActive(false);
break;
}
}
//yield return null;
yield return new WaitForSeconds(delayTime);
}
}

}
==========================================================
然後寫一個新的script繼承它

using UnityEngine;
using System.Collections;

public class RunnerSpray :AnimPlane {

// Use this for initialization
void Start () {
base.Init();

PlayAnim();
}

}

=========================================================
然後在unity editor裡建立一個plane
將RunnerSpray腳本拖ㄅ放到plane裡

再將多張texture依序拖放到Frames 底下
(這裡有個缺點,萬一圖片有幾百張不就拖到死了,暫時沒空去處理)

然後拖一張texture到這平面上
shader設定成 Mobile/Particles/Additive 

設定大概參考圖片



然後執行一下Run
就能看到效果囉^^








沒有留言:

張貼留言