剛剛看完第4章,趁有空接著寫,這一章教製作光效的動畫及文字流動的效果,光效動畫內容意義不大,就是將光的強度從1到0再到1的一個變化動畫,跟著色器的關係不大,主要記錄一下文字流動的效果開發。
首先,打開場景Chapter4_Start,建立一個腳本,命名為ControlTwinkle,代碼內容如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ControlTwinkle : MonoBehaviour {
public bool switched;
public float frequeny = 0.2f; //控制切換速度
public Texture2D origAlbedo;
public Texture2D newAlbedo;
void Start()
{
InvokeRepeating("Switch", frequeny, frequeny);
}
void Update()
{
if (switched)
{
GetComponent<Renderer>().material.SetTexture("_MainTex", newAlbedo);
}
else
{
GetComponent<Renderer>().material.SetTexture("_MainTex", origAlbedo);
}
}
void Switch()
{
switched = !switched;
}
}
代碼內容很簡單,掛到場景中的對象interface_quad上,直接通過渲染的texture的替換來達到閃爍的效果,如圖