2014년 5월 15일 목요일

Unity_Pannel Fade In&Out With NGUI

Explan.
NGUI이용시
Script상에서 UIPannel 의 alpha scale을 조정하면서
페이드 인,아웃 효과를 주는 스크립트.
Tween Alpha를 활용하여 줄수도 있으나
개인적으로 직접 코딩하여 진행하는것이 더편하기에...
UIPannel뿐만아니라 그외의 컴포넌트라도
Alpha 속성이 구현되어있는 놈이라면
동일하게 이용할수있다.
Timescale값은 원하는값으로 수정하면된다.


FadeInOut.cs

using UnityEngine;
using System.Collections;

public class PannelFadeInOut : MonoBehaviour {

    private UIPanel mPannel;

    private float mFadeTime = 1f;
    private float mWaitTime = 1f;
    private bool on = false;
    void Start()
    {
        mPannel = GetComponent<UIPanel>();
        mPannel.enabled = false;
    }

    public void StartFade()
    {
        if(!on)
            StartCoroutine("fadeCoroutine");
    }

    public void StopFade()
    {
        StopCoroutine("fadeCoroutine");
    }

    IEnumerator fadeCoroutine()
    {
        mPannel.enabled = true;
        float startTime = RealTime.time;
        Time.timeScale = .2f; // slow Time scale
        while(RealTime.time - startTime < mFadeTime) // fade in
        {
            mPannel.alpha = Mathf.Lerp(.1f, .95f, (RealTime.time - startTime) / mFadeTime);
            yield return null;
        }

        startTime = RealTime.time;
        while (RealTime.time - startTime < mWaitTime) // wait
            yield return null;
        
        startTime = RealTime.time;
        while (RealTime.time - startTime < mFadeTime) // fade out
        {
            mPannel.alpha = Mathf.Lerp(.95f, 0, (RealTime.time - startTime) / mFadeTime);
            yield return null;
        }
        Time.timeScale = 1f;
        mPannel.alpha = 0; // default 0
    }
}
Categories: , ,

댓글 2개:

  1. RealTime.time 은 어떻게 하는건가요??

    답글삭제
    답글
    1. RealTIme의 경우 UnityEngine의 Time.timescale에 독립적으로 함수가 동작하기위해 사용한것입니다. NGUI에 포함되어있는 클래스이고 Time.time을 사용하셔도 무방합니다.(Timescale에 독립적으로 동작하지않아도 괜찮으시다면 ^^)

      삭제