4일차 


https://doc.photonengine.com/ko-kr/pun/v2/demos-and-tutorials/pun-basics-tutorial/lobby-ui


해당 튜토리얼을 보고 유저네임 입력 UI와 스크립트를 만들었다




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Photon.Pun;
using Photon.Realtime;
 
namespace Com.MyCompany.MyGame
{
 
    [RequireComponent(typeof(InputField))]
    public class PlayerNameInputField : MonoBehaviour
    {
 
        const string playerNamePrefKey = "PlayerName";
        // Start is called before the first frame update
        void Start()
        {
            string defaultName = string.Empty;
            InputField _inputField = this.GetComponent<InputField>();
            if(_inputField != null)
            {
                if (PlayerPrefs.HasKey(playerNamePrefKey))
                {
                    defaultName = PlayerPrefs.GetString(playerNamePrefKey);
                    _inputField.text = defaultName;
                }
            }
 
            PhotonNetwork.NickName = defaultName;
        }
 
        // Update is called once per frame
        void Update()
        {
        
        }
 
        public void SetPlayerName(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                Debug.LogError("Player Name is null or empty");
                return;
            }
            PhotonNetwork.NickName = value;
 
            PlayerPrefs.SetString(playerNamePrefKey, value);
        }
    }
}
cs


**InputField 의 on value changed 이벤트에 SetPlayName걸어도 null이 나가서 

인자를 InputField로 변경 후  value.text로 접근하니 접근이 가능했다.

 ** public void SetPlayerName(InputField value){ value.text ...........

원인을  찾아보자

추가

위에 Dynamic string 쪽에 선택을 했어야했다 ... 글을 대충읽지 말자




[RequireComponent(typeof(컴포넌트))]

스크립트가 달린 오브젝트에 해당 컴포넌트가 없다면 추가를 요청한다


C#에서 [] 연산자 용법중 어트리뷰트 부분을 참고해보자

https://docs.microsoft.com/ko-kr/dotnet/csharp/programming-guide/concepts/attributes/index


PlayerPrefs 클래스는 로컬에 간단한 자료를 저장 할 수있게 해준다

https://docs.unity3d.com/ScriptReference/PlayerPrefs.html




+ Recent posts