Params, UIBase, UIBaseParam은 제외합니다.
CharacterData, RawData도 제외.
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
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InGame : MonoBehaviour
{
public UIInGame uiInGame;
public Hero hero;
private int selectedHeroId;
public void Init(int selectedHeroId)
{
this.selectedHeroId = selectedHeroId;
this.uiInGame.Init(selectedHeroId);
var data = DataManager.Instance.GetDataById<CharacterData>(this.selectedHeroId);
var heroModelPrefab = Resources.Load<GameObject>("Prefabs/" + data.res_name);
var heroModelGo = Instantiate<GameObject>(heroModelPrefab);
heroModelGo.transform.SetParent(this.hero.transform, false);
this.hero.Init(selectedHeroId);
}
}
|
cs |
InGame.cs
히어로 모델 동적 생성후 준비된 hero에 붙였습니다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UIInGame : MonoBehaviour
{
public UIInGameHeroStatus uiInGameHeroStatus;
private int characterId;
public void Init(int selectedHeroId)
{
this.uiInGameHeroStatus.Init(selectedHeroId);
}
}
|
cs |
UIInGame.cs
InGame에서 UIInGame.Init 호출할 때 넘겨받은 id를 저장해두고 캐릭터
정보 UI에 id를 넘겨주며 Init 호출합니다.
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
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.U2D;
using UnityEngine.UI;
public class UIInGameHeroStatus : MonoBehaviour
{
public Text nameText;
public Text hpText;
public Text levelText;
public SpriteAtlas characterAtlas;
public Image characterImage;
public Slider sliderHp;
public Slider sliderExp;
private int characterId;
private CharacterData characterData;
public void Init(int characterId)
{
this.characterId = characterId;
this.characterData = DataManager.Instance.GetDataById<CharacterData>(characterId);
//이름 - ch_01_01
//썸네일 - thumb_ch_01_01
//체력 게이지 (현재 / 최대)
//경험치 게이지
//레벨
this.UpdateUI();
}
public void UpdateUI()
{
this.nameText.text = this.characterData.res_name;
this.characterImage.sprite = this.characterAtlas.GetSprite("thumb_" + this.characterData.res_name);
this.hpText.text = 700 + "/" + this.characterData.hp;
this.levelText.text = 10.ToString();
sliderHp.value = (float)700 / (float)this.characterData.hp;
sliderExp.value = (float)300 / 1000f;
}
}
|
cs |
UIInGameHeroStatus.cs
Init시에 Id를 넘겨받고 데이터 매니저에서 Data를 가져옵니다. 후에 UI를 업데이트 해야 하는데
Data를 다시 찾지 않기 위해서 Init시에 data를 가지도록 했습니다.
Init외에 UpdateUI를 만들었으며 캐릭터 상태가 바뀔때마다 Update하기 위함입니다.
최초 Init시에 이를 한번 실행합니다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Login : MonoBehaviour
{
public UILogin uiLogin;
public void Init()
{
this.uiLogin.Init();
Debug.Log("Login Init");
}
private void Start()
{
this.Init();
}
}
|
cs |
Login.cs
uiLogin의 Init호출 외에 없습니다.
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
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UILogin : UIBase
{
public UILoginPopup uiLoginPopup;
public Button loginPopupBtn;
public override void Init(UIBaseParam param = null)
{
this.uiLoginPopup.Init();
this.loginPopupBtn.onClick.AddListener(() =>
{
this.uiLoginPopup.Show();
this.loginPopupBtn.gameObject.SetActive(false);
});
this.uiLoginPopup.closeBtn.onClick.AddListener(() =>
{
this.uiLoginPopup.Hide();
this.loginPopupBtn.gameObject.SetActive(true);
});
}
}
|
cs |
UILogin.cs
로그인 팝업 버튼, 로그인 팝업 닫기버튼에 대한 onClick 이벤트만 정의했습니다.
UILogin이 UILoginPop을 가지고 있게 했습니다.
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.U2D;
using UnityEngine.UI;
public class UILoginPopup : UIPopupBase
{
public class UILoginPopupParam : UIBaseParam
{
public int Id { get; private set; }
public UILoginPopupParam(int id) => this.Id = id;
}
public Button closeBtn;
public InputField idInputField;
public InputField passwordInputFeild;
public Toggle rememberToggle;
public Button loginBtn;
public Button signBtn;
public Button findPasswordBtn;
public SpriteAtlas inputFieldAtlas;
private void Awake()
{
this.idInputField.onEndEdit.AddListener((text) =>
{
if(!string.IsNullOrEmpty(text))
this.idInputField.image.sprite = this.inputFieldAtlas.GetSprite("txt_field_typing_0");
});
this.passwordInputFeild.onEndEdit.AddListener((text) =>
{
if (!string.IsNullOrEmpty(text))
this.passwordInputFeild.image.sprite = this.inputFieldAtlas.GetSprite("txt_field_typing_0");
});
this.idInputField.onValueChanged.AddListener((text) =>
{
this.idInputField.image.sprite = this.inputFieldAtlas.GetSprite("txt_field_normal_0");
});
this.passwordInputFeild.onValueChanged.AddListener((text) =>
{
this.passwordInputFeild.image.sprite = this.inputFieldAtlas.GetSprite("txt_field_normal_0");
});
this.loginBtn.onClick.AddListener(() =>
{
Debug.Log("id : " + this.idInputField.text);
Debug.Log("password : " + this.passwordInputFeild.text);
Debug.Log(this.rememberToggle.isOn.ToString());
var oper = SceneManager.LoadSceneAsync("Lobby");
oper.completed += ((completed) =>
{
GameObject.Find("Lobby").GetComponent<Lobby>().Init();
});
});
this.signBtn.onClick.AddListener(() =>
{
Debug.Log("아이디 찾기");
});
this.findPasswordBtn.onClick.AddListener(() =>
{
Debug.Log("비밀번호 찾기");
});
}
public void Init(UIBaseParam param = null)
{
this.gameObject.SetActive(false);
}
}
|
cs |
UILoginPopup.cs
로그인 팝업에서 closeBtn을 제외한 다른 버튼들에 대한 onClick이벤트가 정의되어 있습니다.
Atlas를 한개 가지고 있는데, 로그인, 패스워드 InputField에서 텍스트가 입력되고 완료될 때
색깔을 바꿔주기 위해 하나로 묶어놨습니다.
onEndEdit.AddListener를 사용하면 텍스트 수정이 끝난 후에 일어날 일을 넣어줄 수 있습니다.
onValueChanged.AddListener를 사용하면 텍스트의 값이 바뀔때 일어날 일을 넣어줄 수 있습니다.
이 두개다 string 매개변수 하나를 받으며 이는 InputField에 들어간 내용이 들어옵니다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class App : MonoBehaviour
{
private void Awake()
{
DontDestroyOnLoad(this);
DataManager.Instance.LoadData();
}
private void Start()
{
var oper = SceneManager.LoadSceneAsync("Login");
oper.completed += ((completed) =>
{
GameObject.Find("Login").GetComponent<Login>().Init();
});
}
}
|
cs |
App.cs
Login 신을 로드합니다.
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
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class UIListItemCharacter : MonoBehaviour
{
public Text txtId;
public Button selectBtn;
private GameObject uiModel;
private int id;
public void Init(int id)
{
this.id = id;
this.txtId.text = id.ToString();
var data = DataManager.Instance.GetDataById<CharacterData>(id);
string uiModelPrefabName = "Prefabs/UI/ui_" + data.res_name;
this.uiModel = Instantiate<GameObject>(Resources.Load<GameObject>(uiModelPrefabName));
uiModel.transform.SetParent(this.transform, false);
this.selectBtn.onClick.AddListener(() =>
{
var oper = SceneManager.LoadSceneAsync("InGame");
oper.completed += ((completed) =>
{
GameObject.Find("InGame").GetComponent<InGame>().Init(this.id);
});
});
}
}
|
cs |
UIListItemCharacter.cs
Init시에 id하나만 받으며 select 버튼을 누르면 다음 신으로 넘어가는데
다음 신의 Init시에 매개변수로 넘겨줍니다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
using System.Collections;
using System.Collections.Generic;
using UnityEditorInternal.VersionControl;
using UnityEngine;
public class UILobby : MonoBehaviour
{
public Transform contentsTrans;
public void Init()
{
var listItemPrefab = Resources.Load<GameObject>("Prefabs/UI/listItemCharacter");
var characterDatas = DataManager.Instance.GetAllDatasByType<CharacterData>();
foreach(var data in characterDatas)
{
var listItemGo = Instantiate<GameObject>(listItemPrefab);
var listItemCompo = listItemGo.GetComponent<UIListItemCharacter>();
listItemCompo.Init(data.id);
listItemGo.transform.SetParent(contentsTrans, false);
}
}
}
|
cs |
UILobby.cs
리스트 아이템 생성시 자식으로 붙을 contentsTrans를 가지고 있습니다.
리스트 아이템을 동적 생성하고 Init하며 Id를 넘겨줍니다.
값의 세팅은 리스트아이템 Init에서 일어납니다.
Unity Study_015 HudText, Coroutine (0) | 2020.05.26 |
---|---|
Unity Study_013-2 마우스 클릭 좌표 이동 (0) | 2020.05.26 |
Unity Study_007 무기변경, 닭 잡기. (0) | 2020.05.14 |
Unity Study_006 Effect and WeaponChange (0) | 2020.05.13 |
Study_004 - UnityActionExmaple (0) | 2020.05.11 |