레이블이 기능인 게시물을 표시합니다. 모든 게시물 표시
레이블이 기능인 게시물을 표시합니다. 모든 게시물 표시

2019년 9월 4일 수요일

Unity 비동기 로딩(LoadLevelAsync) 일시 정지(Pause)

유니티의 백그라운드에서 비동기적으로 레벨을 로드하는 동안 로딩바를 표시하는 기능과 로딩 완료 시 Play 버튼이 발생해 다른 Scene로 전환되는 기능에 대한 요약 정리



예제

  1. 더미 값을 통해 로딩이 진행 되는 과정 표시
  2. 씬 로딩 완료 후 Start 버튼 표시
  3. 버튼 선택 시 지정된 Scene로 이동

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;  // 씬 이동 처리를 위한 선언
using UnityEngine.UI;               // 게이지 이동 처리를 위한 선언

public class LoadLevelAsyncCode : MonoBehaviour
{
    // 화면에 표시되는 LoadingBar 리소스 연결
    public Slider sliderUI;
    // 화면에 표시되는 toPlay 버튼 리소스 연결
    public GameObject toPlayButtonUI;

    // 더미 시간의 설정 Min Max 값
    float dummyTimeRange_Min;
    float dummyTimeRange_Max;

    // 최초 게이지가 증가되는 더미 시간 -> 이후 본 로딩 시작
    float dummyTime;

    // 로딩 이후 시작 버튼 처리 
    bool toPlayButton;

    void Awake()
    {
        dummyTimeRange_Min = 0.8f;
        dummyTimeRange_Max = 1.5f;
        dummyTime = 0;

        // 버튼의 상태를 초기화 한다.
        toPlayButton = false;
    }

    void Start()
    {
        // UI를 처리 한다.
        sliderUI.gameObject.SetActive(true);
        toPlayButtonUI.SetActive(false);

        // 로딩을 즉시 실행
        StartCoroutine(LoadAsynchronously(1));
    }

    IEnumerator LoadAsynchronously(int _sceneIndex)
    {
        // 더미 타이머로 진행할 값을 설정
        dummyTime = Random.Range(dummyTimeRange_Min, dummyTimeRange_Max);

        // 게이지로 표현되는 loading 값 변수들
        float loadingTime = 0.0f;   // 시간 계산 용
        float progress = 0.0f;      // 게이지 용


        // 타이머 게이지 처리 
        while (loadingTime <= dummyTime)
        {
            // 프레임 당 시간을 증가 
            loadingTime += Time.deltaTime;

            // AsyncOperation 를 통한 추가 로딩 처리를 위해 0.9 값 을 백분율화 
            // 이후 opertaion.progress 는 0.9 까치 처리 되고 완료 된다.
            progress = Mathf.Clamp01(loadingTime / (0.9f + dummyTime));

            // 슬라이더바의 값 증가 처리
            sliderUI.value = progress;
            
            yield return null;
        }

        // "AsyncOperation"라는 "비동기적인 연산을 위한 코루틴을 제공"
        AsyncOperation operation = SceneManager.LoadSceneAsync(_sceneIndex);

        // 로딩 후 스타트 버튼 처리를 위한 선언
        // 이 항목이 없으면 바로 로딩 후 Scene 이동이 처리 됨
        operation.allowSceneActivation = false;

        // 로딩이 종료되기 전까지의 로딩창 게이지 처리
        while (!operation.isDone)
        {
            // 비동기 로딩 진행에 따른 게이지 처리
            progress = Mathf.Clamp01((operation.progress + loadingTime) / (0.9f + dummyTime));

            // 슬라이더 증가 처리
            sliderUI.value = progress;

            yield return null;

            // 로딩 이후의 처리 , 버튼을 누르면 씬 이동이 처리 된다.
            // UI를 반전 처리 한다.
            //sliderUI.gameObject.SetActive(false);
            toPlayButtonUI.SetActive(true);
            Debug.Log("toPlayButtonUI.SetActive(true) - Line");
            // toPlayButton 입력을 대기 한다.
            if (toPlayButton)
            {
                operation.allowSceneActivation = true;
            }

            yield return null;

        }
               
    }

    // 버튼을 입력 한다.
    public void ToPlayButton()
    {
        toPlayButton = true;
    }

}

Unity - Firebase 연동 (Analytics, AdMob)

버전 : firebase_unity_sdk_6.5.0.zip 게임에서 통계 측정 및 광고 추적을 위해 Firebase 을 연동한다. 앞서 [Unity - GPGS 와 Admob 연동 및 배포 준비 작업]  연동 이후에 작업을 진행 한다. 유니...