Memorandum

自分へのメモ。そして誰かの役に立てば、うれしい。

UnityでのUVスクロール

UnityでUVスクロールをやったので、その備忘録。

1.プロジェクトを作成する

こんな感じのプロジェクトを作成します。
初期状態のプロジェクトにPlaneを追加しただけです。
f:id:Keizi:20160529090602p:plain


2.テクスチャを用意する

スクロールが分かりやすいように、市松模様のテクスチャを準備しました。
f:id:Keizi:20160529092312p:plain:w300


テクスチャのwrap modeを「Repeat」に設定しておきましょう。
f:id:Keizi:20160529091814p:plain


3.マテリアルを作成する

マテリアルを作成して、先ほどのテクスチャをNormalMapに設定します。
f:id:Keizi:20160529092532p:plain

4.スクリプトを作成する

以下のスクリプトをPlaneに追加します。

using UnityEngine;
using System.Collections;

public class ScrollUv : MonoBehaviour {
	public float scrollSpeed = 0.5F;
	public Renderer rend;
	void Start() {
		rend = GetComponent<Renderer>();
	}

	void FixedUpdate() {
		float offset = Time.time * scrollSpeed;
		rend.material.SetTextureOffset("_MainTex", new Vector2(offset, 0));
	}
}

5.こんな感じのアニメーションになります。

f:id:Keizi:20160529093820g:plain