본문 바로가기

TIL

[TIL]2024-1-23 / 22일차 - Unity 입문 개인 과제 제출

1. 오늘의 알고리즘 코드카타

1-1. 없는 숫자 더하기

 

답안 :

using System;
using System.Linq;

public class Solution {
    public int solution(int[] numbers) {
        int answer = 45 - numbers.Sum();
        return answer;
    //혹은
    //var numberArray = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            //return numberArray.Except(numbers).Sum();
    }
}

 

 

 

1-2. 제일 작은 수 제거하기

 

답안 :

//배열의 최솟값, 특정값 구하는 방법이 필요.
//Where 내부에 arr.Min을 선언하면 매 원소마다 최솟값을 찾아야함.
using System;
using System.Linq;

public class Solution {
    public int[] solution(int[] arr) {
        int[] emptyArr = new int[] { -1 };
        
        if (arr.Length < 2)
            return emptyArr;
        
        int min = arr.Min();
        int[] newArr = arr.Where(num => num != min).ToArray();
        return newArr;
    }
}

 

 

1-3. 가운데 글자 가져오기

 

답안 : 

//string을 배열로 옮기는 것이 편하겠지?
//혹은 substring이 변환도 없이 빠르지 않을까 
//이러면 x(자르기 시작할 지점), y(받을 갯수)값을 받아야한다.
//y = 짝수일 때 2 / 홀수일 때 1
//x = 중간값 = s.Length / 2 와 s.Length / 2 -1
using System;

public class Solution {
    public string solution(string s) {
        string answer = ""; 
        int x = 0;
        int y = 0;
            
        if(s.Length % 2 == 1)
        {
            x = s.Length / 2;
            y = 1;
        }
        else
        {
            x = s.Length / 2 -1;
            y = 2;
        }

        return answer = s.Substring(x, y);
    }
}

 

 

 


2. 개인과제

제출본 구동 장면

 

추가된 기능 목록 : 

 

1. 시간 표시

2. 접속자 리스트 표시

3. 대화창 출력

 

 

 

2-1. 시간 표시

 

유니티에 자체적으로 존재하는 DateTime을 써서 시간을 받는다.

 

 

 

시간을 출력하는 모습

 

2-2. 접속자 리스트 표시

 

여기서한 뻘짓이 오늘 전체 시간에 80%다.

 

그냥 리스트나 이런 것 보다 좀 더 좋은 기능이 없을까 하다가 구현하기 쉬운 서버인 포톤에 대해 알게 된 것.

 

인터넷에 많은 가이드들을 보며 전반적인 기능을 공부하고 예제 코드들을 이용해서 실제 구현해보게 되었는데

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Photon.Pun;
using Photon.Realtime;
using TMPro;
using System.Threading;
using Photon.Pun.Demo.Cockpit;

public class NetworkManager : MonoBehaviourPunCallbacks
{
    public TMP_InputField m_InputField;
    public TMP_Text m_textConnectLog;
    public TMP_Text m_textPlayerList;

    void Start()
    {
        Screen.SetResolution(1980, 1080, false);
    }

    public override void OnConnectedToMaster()
    {
        RoomOptions options = new RoomOptions();
        options.MaxPlayers = 5;

        PhotonNetwork.LocalPlayer.NickName = m_InputField.text;
        PhotonNetwork.JoinOrCreateRoom("Room1", options, null);

    }
    public override void OnJoinedRoom()
    {
        updatePlayer();

        m_textConnectLog.text += m_InputField.text;
        m_textConnectLog.text += " 님이 방에 참가하였습니다.\n";
    }

    public override void OnPlayerEnteredRoom(Player newPlayer)
    {
        updatePlayer();

        m_textConnectLog.text += newPlayer.NickName;
        m_textConnectLog.text += " 님이 입장하였습니다.\n";
    }

    public override void OnPlayerLeftRoom(Player otherPlayer)
    {
        updatePlayer();

        m_textConnectLog.text += otherPlayer.NickName;
        m_textConnectLog.text += " 님이 퇴장하였습니다.\n";
    }

    public void Connect()
    {
        PhotonNetwork.LocalPlayer.NickName = m_InputField.text;
        updateNameList();
        PhotonNetwork.ConnectUsingSettings();
    }

    void updatePlayer()
    {
        for(int i = 0; i < PhotonNetwork.PlayerList.Length; i++)
        {
            m_textPlayerList.text += PhotonNetwork.PlayerList[i].NickName;
            m_textPlayerList.text += "\n";
        }
    }
    void updateNameList()
    {
        m_textPlayerList.text = "";
        for (int i = 0; i < PhotonNetwork.PlayerList.Length; i++)
        {
            m_textPlayerList.text += PhotonNetwork.PlayerList[i].NickName;
            m_textPlayerList.text += "\n";
        }
    }
}

 

서버에 접속까지는 되는 것을 확인 했지만

 

닉네임을 받아서 접속자 리스트에 띄우는 방법은 여러가지 시도해보았지만 제대로 작동하는 것을 만들지 못하고

결국 내부적으로 현재 플레이어 리스트 Length를 받고 이를 기반으로 출력하는 정도로 만들게 되었다.

 

거기에 인게임에서 이름을 바꾸는 것이 반영되는 데에 많은 문제가 있었고, 결국 2~3시간 고민해보다가

다시 리스트를 확인하는 무식한 방법으로 우선 구현하게 되었다.

 

욕심을 부리다가 머리가 아파서 타이레놀을 먹었다.

 

 

2-3. 대화창 출력

 

위에서 욕심을 부린 덕분에 이걸 제대로 만들 시간이 없었다.

 

NPC마다 Key값을 주고 colision이 날 시 이를 체크하여 값을 받아오는 방법에 대해 생각은 해보았지만 제대로 구현하기에는 코딩 실력이 부족해서 시간을 써보다가 결국 포기하고 우선 premade된 대화창만 NPC의 콜라이더 범위에 플레이어의 콜라이더가 닿으면 띄우는 방식으로 하였다.

 

이 *플레이어의 콜라이더 라는 것이 중요해진 게, 저번 뻘짓에서 넣은 총알이 isTrigger값을 가지고 있었기 때문에 처음에는 총알이 NPC에게 닿아도 대화창이 켜졌었다.

 

 

항상 숭배해

 

이제는 힘들어서 이대로 제출하였다.

 

이제 내일 팀프로젝트에 두려움 속에 잠을 청하려 한다.