유니티 심화

LearnUGUI - Test07 [ StageClear ] - Ribbon, Stars, Particle [ UI ]

다모아 2023. 9. 12. 14:24

 

AnimationReceiver

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//컴포넌트 속성을 자동추가해줌
[RequireComponent(typeof(Animator))]
public class AnimationReceiver : MonoBehaviour
{
    public Action onFinished;
    public void OnFinished()
    {
        Debug.Log("animation finish");
        if(this.onFinished != null)
        {
            this.onFinished();
        }        
    }
}

 

Test07Main

using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class Test07Main : MonoBehaviour
{
    [SerializeField]
    private Button btnRibbonPlay;
    [SerializeField]
    private Button btnLeftStarPlay;
    [SerializeField]
    private Button btnMiddleStarPlay;
    [SerializeField]
    private Button btnRightStarPlay;
    [SerializeField]
    private TMP_Dropdown dropdown;
    [SerializeField]
    private Button btnStarsPlay;
    [SerializeField]
    private Button btnRibbonStarsPlay;
    [SerializeField]
    private Test07UICellView uiCellView;

    [SerializeField]
    private Animator animRibbon;
    [SerializeField]
    private Animator animLeftStar;
    [SerializeField]
    private Animator animMiddleStar;
    [SerializeField]
    private Animator animRightStar;
    [SerializeField]
    private ParticleSystem[] fxStars;
    private int starCount = 1;
    // Start is called before the first frame update
    void Start()
    {
        Test07AtlasManager.instance.LoadAtlasDatas();

        this.uiCellView.Init();

        this.btnRibbonPlay.onClick.AddListener(() => {
            this.animRibbon.SetTrigger("Play");
        });

        this.btnLeftStarPlay.onClick.AddListener(() => {
            this.animLeftStar.SetTrigger("Play");
        });

        this.btnMiddleStarPlay.onClick.AddListener(() => {
            this.animMiddleStar.SetTrigger("Play");
        });

        this.btnRightStarPlay.onClick.AddListener(() => {
            this.animRightStar.SetTrigger("Play");
        });

        this.dropdown.onValueChanged.AddListener((starCount) => {
            this.starCount = starCount + 1;
        });

        this.btnRibbonStarsPlay.onClick.AddListener(() => {
            //1. Ribbon먼저 실행
            this.animRibbon.SetTrigger("Play");
            //2. 실행 다 끝났으면 Stars 차례대로 실행
            this.animLeftStar.GetComponent<StarAnimationReceiver>().onPlayNext = () => {
                var fx = this.fxStars[0];
                fx.gameObject.SetActive(true);
                fx.Play();
                this.animMiddleStar.GetComponent<StarAnimationReceiver>().onPlayNext = () => {
                    var fx1 = this.fxStars[1];
                    fx1.gameObject.SetActive(true);
                    fx1.Play();

                    this.animRightStar.GetComponent<StarAnimationReceiver>().onPlayNext = () => {
                        var fx2 = this.fxStars[2];
                        fx2.gameObject.SetActive(true);
                        fx2.Play();
                    };
                    this.animRightStar.SetTrigger("Play");
                };
                this.animMiddleStar.SetTrigger("Play");
            };
            this.animLeftStar.SetTrigger("Play");
        });

        this.btnStarsPlay.onClick.AddListener(() => {
            Debug.LogFormat("별 애니메이션 실행: {0}", this.starCount);

            switch (this.starCount)
            {
                case 1:
                    this.animLeftStar.GetComponent<StarAnimationReceiver>().onPlayNext = () => {
                        var fx = this.fxStars[0];
                        fx.gameObject.SetActive(true);
                        fx.Play();
                    };
                    this.animLeftStar.SetTrigger("Play");
                    break;

                case 2:
                    this.animLeftStar.GetComponent<StarAnimationReceiver>().onPlayNext = () => {
                        var fx = this.fxStars[0];
                        fx.gameObject.SetActive(true);
                        fx.Play();
                        this.animMiddleStar.GetComponent<StarAnimationReceiver>().onPlayNext = () => {
                            var fx1 = this.fxStars[1];
                            fx1.gameObject.SetActive(true);
                            fx1.Play();
                        };
                        this.animMiddleStar.SetTrigger("Play");
                    };
                    this.animLeftStar.SetTrigger("Play");
                    break;

                case 3:
                    this.animLeftStar.GetComponent<StarAnimationReceiver>().onPlayNext = () => {
                        var fx = this.fxStars[0];
                        fx.gameObject.SetActive(true);
                        fx.Play();
                        this.animMiddleStar.GetComponent<StarAnimationReceiver>().onPlayNext = () => {
                            var fx1 = this.fxStars[1];
                            fx1.gameObject.SetActive(true);
                            fx1.Play();
                            
                            this.animRightStar.GetComponent<StarAnimationReceiver>().onPlayNext = () => {
                                var fx2 = this.fxStars[2];
                                fx2.gameObject.SetActive(true);
                                fx2.Play();
                            };
                            this.animRightStar.SetTrigger("Play");
                        };
                        this.animMiddleStar.SetTrigger("Play");
                    };
                    this.animLeftStar.SetTrigger("Play");
                    break;
            }
        });
    }
}