Unity

[유니티] 포톤 게임 만들기

Skull Crusher 2021. 6. 4. 11:31
728x90

1. 플레인

2. 조명

 

3. 카메라

직교투영 사용

 

4. 패널 추가

 

5. 토글 만들기

 

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

//PUN Document URL : doc-api

using UnityEngine.UI;

public class ConnectManager : MonoBehaviour
{
    [SerializeField] private Text connectMsgText; //접속 로그 표시 텍스트

    [SerializeField] private InputField nickNameInputField; //닉네임 입력 필드

    [SerializeField] private GameObject startPanel; //시작 패널

    [SerializeField] private Button createButton; //방접속 및 캐릭터 생성 버튼

    [SerializeField] private Toggle redToggle; //레드팀 선택 토글
    [SerializeField] private Toggle blueToggle; //블루팀 선택 토글

    // Start is called before the first frame update
    void Start()
    {
        //PhotonNetwork.connected : 이미 포톤 클라우드에 접속이 완료된 상태인지 체크하는 값
        if (!PhotonNetwork.connected)
        {
            //PhotonNetwork.ConnectUsingSettings("버전") : 설정 정보 기준으로 포톤 클라우드 접속
            //* PhotonServerSettings / Auto Join Lobby : 자동 로비 접속까지 수행
            if(PhotonNetwork.ConnectUsingSettings("v1.0"))
            {
                ShowConnectLogMessage("[알림] 게임 서버에 서버에 접속을 시도하는 중 ...");
            }
            else
            {
                ShowConnectLogMessage("[알림] 게임 서버에 서버에 접속을 시도 실패");
            }
        }
    }

    //[포톤 이벤트 메소드] Photon Cloud Server 및 Lobby 접속 완료
    public void OnJoinedLobby()
    {
        ShowConnectLogMessage("[알림] 게임 로비 접속이 완료됨");
    }

    //[포톤 이벤트 메소드] Photon Cloud Server 접속 실패
    public void OnFailedToConnectToPhoton(DisconnectCause cause)
    {
        Debug.Log(cause.ToString());
        connectMsgText.text = "[오류] 게임 로비 접속이 실패함";
    }


    // Update is called once per frame
    void Update()
    {
        //접속이 완료된 상태에서만 방 생성/접속 및 캐릭터생성 버튼의 활성화 되도록 설정
        createButton.interactable = PhotonNetwork.connected;
    }

    public void OnRoomCreateAndJoinButtonClick()
    {
        //방생성 : PhotonNetwork.CreateRoom
        //방접속 : PhotonNetwork.JoinRoom() / PhotonNetwork.JoinRandomRoom()
        //방생성및접속 : PhotonNetwork.JoinOnCreateRoom()

        RoomOptions roomOption = new RoomOptions();
        roomOption.MaxPlayers = 10; //최대 수용 인원
        roomOption.IsOpen = true; //방 공개 여부(검색가능)
        roomOption.IsVisible = true; //방활성/비활성 여부

        PhotonNetwork.JoinOrCreateRoom(
                        "GameRoom", //방 이름
                         roomOption, //방 옵션
                         TypedLobby.Default //로비 타입 : 기본
            );
    }

    public void OnJoinedRoom()
    {
        ShowConnectLogMessage("[알림]" + PhotonNetwork.room.Name + "방에 접속이 완료됨");
        startPanel.SetActive(false); //접속 창 닫기
        
    }

    //[포톤 이벤트 메소드] 방 생성이 실패함
    public void OnPhotonCreateRoomFailed(object[] errorMsg)
    {
        Debug.Log(errorMsg[1].ToString());
        connectMsgText.text = "[오류] 방 생성이 실패함";
    }

    public void OnPhotonJoinRoomFailed(object[] errorMsg)
    {
        Debug.Log(errorMsg[1].ToString());
        connectMsgText.text = "[오류] 방 접속이 실패함";
    }

    public void ShowConnectLogMessage(string msg)
    {
        Debug.Log(msg);
        connectMsgText.text = msg;
    }
}

 

6. 

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

public class UIBillboard : MonoBehaviour
{
    // Start is called before the first frame update
    void LateUpdate()
    {
        transform.rotation = Quaternion.LookRotation(Camera.main.transform.forward);

    }
}

 

빌드하기

 

https://drive.google.com/file/d/1dbqzUB5hL9CHESuEj6RO3twPaNkVcWmH/view

 

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

public class SoldierShot : Photon.MonoBehaviour
{
    [SerializeField] private float shotDelayTime;

    [SerializeField] private GameObject bulletPrefab;

    [SerializeField] private Transform shotPos;

    [SerializeField] private float shotPower;

    private float time;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (photonView.isMine)
        {
            time += Time.deltaTime;

            if (Input.GetButtonDown("Fire1") && time >= shotDelayTime)
            {
                Fire(shotPos.position, shotPos.forward, transform.rotation);

                time = 0;
            }
        }
        
    }

    public void Fire(Vector3 pos, Vector3 direction, Quaternion qt)
    {
        GameObject bullet = Instantiate(bulletPrefab, pos, qt);
        bullet.GetComponent<Rigidbody>().velocity = direction * shotPower;

        Destroy(bullet, 1f);
    }
}