본문으로 바로가기

게임 매니저 프리팹

 

1. GameManager 프리팹 변환

GameManager.cs 생성, 빈 게임 오브젝트 생성 후 GameManager로 이름 변경, 생성한 스크립트를 오브젝트에 드래그
GameManager 오브젝트를 Asset창으로 드래그하여 Prefab으로 변환하기

2. GameManager 스크립트 코드 추가

using System;
using System.Collections;


using UnityEngine;
using UnityEngine.SceneManagement;


using Photon.Pun;
using Photon.Realtime;


namespace Com.MyCompany.MyGame
{
    public class GameManager : MonoBehaviourPunCallbacks
    {


        #region Photon Callbacks


        /// <summary>
        /// Called when the local player left the room. We need to load the launcher scene.
        /// </summary>
        public override void OnLeftRoom()
        {
            SceneManager.LoadScene(0); //빌드 세팅 0번 씬으로 이동
        }


        #endregion


        #region Public Methods


        public void LeaveRoom()
        {
            PhotonNetwork.LeaveRoom();
        }


        #endregion
    }
}

 

룸 종료 버튼 프리팹

프리팹 생성 이유 : 룸 종료 버튼이 필요한 경우가 많아 재활용하는 것이 효율적이기 때문

 

1. UI 만들기

GameObject-UI-Panel 생성 후 Top Panel로 이름 변경, Image, Canvas Renderer 컴퍼넌트 제거, Top Panel을 우클릭하여 UI-Button 추가, Leave Button으로 이름 변경, 자식 텍스트의 글자를 LeaveGame으로 변경
LeaveButton 오브젝트의 OnClick버튼 이벤트와 Hierarchy의 GameManager인스턴스를 연결해 LeaveRoom()호출하기
LeaveButton 프리팹 변환

 

씬 연결하기

연결하려는 씬 생성, Build Settings에서 로비(0) → 방 순서(1,2,3,4)로 세팅하기,