먼저 만들었던 DB를 사용함
node.js에서 ID가 출력되도록 추가함
public 의 index.js
member.html 에 id 부분 추가
ID가 출력됨
patch(수정), delete(삭제) 될 때 status를 확인하기 위해 코드를 추가해줌
프로토콜 문서를 만듦
유니티에서 이 구조로 씬을 구성해줌
db를 조회할 때마다(get) UIlistItem을 새로 생성해서 보여줌
비활성화 된 건 로딩화면임
코드
Test
using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class Test : MonoBehaviour
{
public UIMembers uiMembers;
public UISignUp uISignUp;
public GameObject loading;
private string host = "http://localhost";
private int port = 3000;
void Start()
{
this.uiMembers.Init();
this.uiMembers.btnRefresh.onClick.AddListener(() =>
{
this.GetMembers();
});
this.GetMembers();
}
public void GetMembers()
{
//로딩바 껴줌
this.loading.SetActive(true);
StartCoroutine(this.Get("/member", (res) =>
{
if (res.status == 200)
{
this.uiMembers.Refresh(res.members);
}
else
{
Debug.LogFormat("{0}", res.status);
}
//로딩바 꺼줌
this.loading.SetActive(false);
}));
}
private IEnumerator Get(string uri, System.Action<Protocols.res_member_get> onComplete)
{
var url = string.Format("{0}:{1}{2}", host, port,uri);
Debug.Log(url);
UnityWebRequest www = UnityWebRequest.Get(url);
yield return www.SendWebRequest();
if(www.isNetworkError || www.isHttpError)
{
Debug.LogFormat("{0}", www.error);
}
else
{
var result = www.downloadHandler.data;
var json = Encoding.UTF8.GetString(result);
Protocols.res_member_get res = JsonConvert.DeserializeObject<Protocols.res_member_get>(json);
Debug.LogFormat("status: {0}, member cnt: {1}", res.status, res.members.Count());
onComplete(res);
}
}
}
UIMembers
using JetBrains.Annotations;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UIMembers : MonoBehaviour
{
public Button btnRefresh;
public Transform contentsTrans;
public UIlistItem uiListItemPrefabs;
public void Init()
{
}
public void Refresh(Protocols.member[] members)
{
foreach(Transform child in this.contentsTrans)
{
Destroy(child.gameObject);
}
foreach(var member in members)
{
var go = Instantiate<GameObject>(this.uiListItemPrefabs.gameObject, this.contentsTrans);
var listItem = go.GetComponent<UIlistItem>();
listItem.Init(member.id, member.username);
}
}
}
UIlistItem
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UIlistItem : MonoBehaviour
{
public Text txtId;
public Text txtMemeberName;
public Button btnUpdata;
public Button btnDelete;
private int id;
private string memberName;
public void Init(int id, string memberName)
{
this.id = id;
this.memberName = memberName;
this.txtId.text = this.id.ToString();
this.txtMemeberName.text = this.memberName.ToString();
}
}
프로토콜 문서를 보고 만들어줌
Protocols
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Protocols
{
public class member
{
public int id;
public string username;
public string password;
public string created_at;
}
public class res_memeber
{
public int status;
}
//멤버 조회
public class res_member_get:res_memeber
{
public member[] members;
}
//멤버 삭제
public class res_member_delete : res_memeber
{
}
//멤버 등록
public class req_member_post
{
public string username;
public string password;
}
public class res_member_post : res_memeber
{
}
//멤버 이름 수정(patch)
public class req_member_put
{
public string username;
}
public class res_member_put : res_memeber
{
}
}
*유니티에서는 patch 대신 put을 사용한다고 함!
DB에 있는 회원 데이터를 가져와서 스크롤뷰 항목으로 보여줌
'C# > 수업내용' 카테고리의 다른 글
2020.07.31. 수업내용 - WebView (2) | 2020.07.31 |
---|---|
2020.07.29. 수업내용 - DB 서버와 유니티 연동 ( MySQL, Nodejs, Unity)(2) (0) | 2020.07.29 |
2020.07.08. 수업내용 - 인공지능 강화학습2 ( 펭귄 MLAgent) (0) | 2020.07.08 |
2020.07.07. 수업내용 - 인공지능 강화학습 1 (0) | 2020.07.07 |
2020.06.24. 수업내용 - Shader 3 (움직이는 불 만들기-uv) (0) | 2020.06.24 |