剛剛看完第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的替換來達到閃爍的效果,如圖

看起來還是有點嚇人的,畢竟模型人物現在沒什麼表情,但是想要的效果還是非常好的。

下面學習如何通過UV做動畫,這個對於固定的動畫,比如管線中做水的流動效果,或者像這個教程中做一個文字流動,都是非常合適的。

找到interface_quad對象下的textDisplay對象,取消隱藏,添加腳本textAnim,內容如下

public class textAnim : MonoBehaviour
{
Vector2 uvOffset = Vector2.zero; //UV起始坐標
public Vector2 uvAnimSpeed = new Vector2(0.0f, 0.1f);//變化的長度變數,值越大變化越快

void LateUpdate()
{
uvOffset += (uvAnimSpeed * Time.deltaTime);
GetComponent<Renderer>().material.SetTextureOffset("_MainTex", uvOffset);
}
}

點擊運行,可以看到文字滾動起來了

關於創建星球的全息影像,新建一個材質hotSpot,找到hotSpot紋理,放到Albedo和Emission位置,調整UV使畫面上只出現一個圓環,接下來創建腳本,命名為desHS

public class destHS : MonoBehaviour {

public Vector2 [] UVOffsets;
public int currArrayPos;
public float interval = 0.1f;
public Renderer rend;
public bool UVTileSwitch;
public float currTile;

void Start ()
{
rend = GetComponent<Renderer>();
InvokeRepeating("NextStage", 1, interval);
}

void Update ()
{
if (UVTileSwitch)
{
currTile = 0.5f;
}
else
{
currTile = 0.25f;
}
//0.5繪製正常紋理,0.25則會出現空白紋理,從而會若隱若現
}

void NextStage()
{
UVTileSwitch = !UVTileSwitch;
currArrayPos++;
if(currArrayPos > UVOffsets.Length-1)
{
currArrayPos = 0;
}
rend.material.SetTextureOffset("_MainTex", UVOffsets[currArrayPos]);
rend.material.SetTextureScale("_MainTex", new Vector2(currTile, currTile));
}
}

找到planetscan對象,下面掛一個子物體destination,並給予材質hotSpot,然後加上上面的腳本,需要設置UV offsets的size為4,並填入(0.5,0)(0,0)(0.5,0.5)(0,0.5),與currTile 的值相對應,運行可以看到全息的效果。

推薦閱讀:

相关文章