こんにちは。ヤマヤタケシです。
相変わらずUnityをいじっています。 ゲームの演出でよくある、真っ白からのフェードインとか真っ黒へのフェードアウトとか、簡単そうに思えて意外に手間取ることが多かったのです。 しかし、さっすがUnity! あっさりとできてしまいました。
できたと言っても、unity fadein でググってできてきたのをベースに俺流でアレンジしただけですけどね。 こことか。
とにかく、白いテクスチャを作るのがこのコードだけでできちゃうのがスゴイ。
texture = new Texture2D( 1, 1, TextureFormat.ARGB32, false );
texture.SetPixel(0,0, Color.white );
texture.Apply();
スゴイや・・・。 N88日本語Basic時代に戻った気分です。 あの時は簡単に点がうてて、線が引けました。 そんな時代が長らく失われていたのです。
フェードのコアである、テクスチャの描画もこれだけ・・・
function OnGUI()
{
GUI.color = now;
GUI.DrawTexture( Rect( 0, 0, Screen.width, Screen.height ), texture );
}
たまにはプログラムを書けるアピールのために、フェードインアウトのソースを載っけます。 残りの部分の方が多いですけど、色を時間で補間しているだけです。
#pragma strict
//
//
// フェードイン、フェードアウト
//
//
private var texture : Texture2D;
private var sequence : String = null;
private var from : Color;
private var to : Color;
private var now : Color;
private var time : float ;
function Awake()
{
texture = new Texture2D( 1, 1, TextureFormat.ARGB32, false );
texture.SetPixel(0,0, Color.white );
texture.Apply();
}
function Start () {
}
function Update () {
if( Input.GetButtonDown( "Fire1" ) ){
FadeIn( 1, Color.white );
}
if( Input.GetButtonDown( "Fire2" ) ){
FadeOut( 1, Color.white );
}
}
function OnGUI()
{
if( now.a != 0 ){
GUI.color = now;
GUI.DrawTexture( Rect( 0, 0, Screen.width, Screen.height ), texture );
}
}
function StartSequence( function_name : String )
{
if( sequence ){
StopCoroutine( sequence );
sequence = null;
}
sequence = function_name;
StartCoroutine( sequence );
}
function FadeUpdate()
{
var now_time : float = 0;
while( 0 < time && now_time < time ){
now_time += Time.deltaTime;
now = Color.Lerp( from, to, now_time / time );
yield;
}
now = to;
}
function FadeIn( t_time : float, t_color : Color )
{
to = from = t_color;
to.a = 0;
time = t_time;
StartSequence( "FadeUpdate" );
}
function FadeOut( t_time : float , t_color : Color )
{
to = from = t_color;
from.a = 0;
time = t_time;
StartSequence( "FadeUpdate" );
}
そんじゃまた。
追記!
あらためてコピペしてビルドしました。動作したけどなぁ・・・。
右クリックと左クリックでフェードイン、フェードアウトです。
