ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 유니티 UI Fade In, Fade Out 클래스
    개발/UNITY 2020. 11. 4. 21:11
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.EventSystems;
    using UnityEngine.UI;
    
    public class FIO_UI
    {
        private Graphic UI;
        private MonoBehaviour mono;
        private float time;
    
        public FIO_UI(Graphic UI, MonoBehaviour mono, float time)
        {
            this.UI = UI;
            this.mono = mono;
            this.time = time;
        }
    
        public void FIO(string command)
        {
            switch (command)
            {
                case "FI":
                    mono.StartCoroutine(FIO(1, time));
                    break;
    
                case "FO":
                    mono.StartCoroutine(FIO(0, time));
                    break;
    
                case "FIO":
                    mono.StartCoroutine(FIO(new Vector2(1, 0), time));
                    break;
    
                case "FOI":
                    mono.StartCoroutine(FIO(new Vector2(0, 1), time));
                    break;
            }
        }
    
        private float temp_alpha;
    
        private IEnumerator FIO(float alpha, float time)
        {
            temp_alpha = UI.color.a;
            for (float t = 0.0f; t < 1.0f; t += Time.deltaTime / time)
            {
                Color newColor = new Color(1, 1, 1, Mathf.Lerp(temp_alpha, alpha, t));
                UI.color = newColor;
                yield return null;
            }
        }
    
        private IEnumerator FIO(Vector2 alpha, float time)
        {
            for (int i = 0; i < 2; i++)
            {
                temp_alpha = UI.color.a;
                for (float t = 0.0f; t < 1.0f; t += Time.deltaTime / time)
                {
                    Color newColor = new Color(1, 1, 1, Mathf.Lerp(temp_alpha, alpha.x, t));
                    UI.color = newColor;
                    yield return null;
                }
                float temp = alpha.x;
                alpha.x = alpha.y;
                alpha.y = temp;
            }
        }
    }

     

    사용시에는 UI 에 새로운 스크립트를 아무거나 하나 붙이고 거기에다가 

     

        private void Start()
        {
            FIO_UI fioui = new FIO_UI(GetComponent<Graphic>(), GetComponent<MonoBehaviour>(), 2);
            fioui.FIO("FOI");
        }

    FIO_UI 아무거나=new FIO_UI(GetComponent<Graphic>(),GetComponent<MonoBehaviour(),걸릴시간초);

     

    이런식으로 FIO(" 여기 ");

    여기에

    FO 는 페이드아웃

    FI 는 페이드인

    FIO 는 페이드인앤아웃

    FOI 는 페이드아웃앤인 

     

    시작할 때 알파값을 지정해준다거나 FIO,FOI 를 몇번 반복한다거나는 지금은 당장 필요 할 것 같지 않아 만들지 않음.

Designed by Tistory.