62 lines
2.0 KiB
C#
62 lines
2.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
using UnityEngine.UIElements;
|
|
|
|
class FurnitureFetcher : MonoBehaviour
|
|
{
|
|
public VisualTreeAsset itemTemplate;
|
|
public UIDocument uiDocument;
|
|
|
|
public static string API_URL = "http://100.123.31.103:5102/api/v1/FurnitureVariant/GetByIds";
|
|
|
|
private void Awake()
|
|
{
|
|
StartCoroutine(GetFurnitureData());
|
|
}
|
|
|
|
IEnumerator GetFurnitureData()
|
|
{
|
|
Debug.Log($"API URL: {API_URL}");
|
|
List<string> variantIds = new()
|
|
{
|
|
"ab6fd51f-81f0-490d-b713-8d4f3de61a58",
|
|
"1f3b348f-b3ee-4985-b76f-7f6d61f2c835"
|
|
};
|
|
|
|
FurnitureIdsRequest data = new() { ids = variantIds };
|
|
string json = JsonUtility.ToJson(data);
|
|
|
|
using UnityWebRequest request = new(API_URL, "POST");
|
|
byte[] bodyRaw = Encoding.UTF8.GetBytes(json);
|
|
request.uploadHandler = new UploadHandlerRaw(bodyRaw);
|
|
request.downloadHandler = new DownloadHandlerBuffer();
|
|
request.SetRequestHeader("Content-Type", "application/json");
|
|
|
|
yield return request.SendWebRequest();
|
|
|
|
if (request.result == UnityWebRequest.Result.Success)
|
|
{
|
|
string wrappedJson = "{\"items\":" + request.downloadHandler.text + "}";
|
|
FurnitureVariantList list = JsonUtility.FromJson<FurnitureVariantList>(wrappedJson);
|
|
|
|
var root = uiDocument.rootVisualElement;
|
|
var scrollView = root.Q<ScrollView>("furniture-list");
|
|
|
|
foreach (var variant in list.items)
|
|
{
|
|
VisualElement item = itemTemplate.CloneTree();
|
|
item.Q<Label>("nameLabel").text = $"Name: {variant.name}";
|
|
item.Q<Label>("colorLabel").text = $"Color: {variant.color}";
|
|
item.Q<Label>("lineLabel").text = $"Line: {variant.line}";
|
|
scrollView.Add(item);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("Error: " + request.error);
|
|
}
|
|
}
|
|
} |