diff --git a/Assets/Common/Prefabs/CustomColoredAnimatedYeti.meta b/Assets/API.meta similarity index 77% rename from Assets/Common/Prefabs/CustomColoredAnimatedYeti.meta rename to Assets/API.meta index 75ab0bd..81ab039 100644 --- a/Assets/Common/Prefabs/CustomColoredAnimatedYeti.meta +++ b/Assets/API.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: f2aaa75c0bf864ff79ef3c0aba951aa4 +guid: 85542efa4a532704d8e1dbc3007e92b9 folderAsset: yes DefaultImporter: externalObjects: {} diff --git a/Assets/API/FurnitureFetcher.cs b/Assets/API/FurnitureFetcher.cs new file mode 100644 index 0000000..d4e3375 --- /dev/null +++ b/Assets/API/FurnitureFetcher.cs @@ -0,0 +1,138 @@ +using System.Collections; +using UnityEngine; +using UnityEngine.Networking; +using UnityEngine.UIElements; +using Newtonsoft.Json; +using System.Collections.Generic; +using System.Globalization; + +class FurnitureFetcher : MonoBehaviour +{ + public VisualTreeAsset itemTemplate; + public UIDocument uiDocument; + + public static string API_URL = "http://100.123.31.103:5102/api/v1/FurnitureVariant/GetAll"; + + private static readonly Dictionary CurrencySymbols = new() + { + { "USD", "$" }, + { "EUR", "€" }, + { "GBP", "£" }, + { "JPY", "¥" }, + { "MXN", "$" }, + { "CAD", "$" }, + { "BRL", "R$" }, + { "ARS", "$" }, + { "CLP", "$" }, + { "COP", "$" }, + { "CNY", "¥" }, + { "INR", "₹" } + }; + + + private void Awake() + { + StartCoroutine(GetFurnitureData()); + } + + IEnumerator GetFurnitureData() + { + using UnityWebRequest request = UnityWebRequest.Get(API_URL); + request.downloadHandler = new DownloadHandlerBuffer(); + + yield return request.SendWebRequest(); + + string rawResponse = request.downloadHandler.text; + //Debug.Log("📦 RAW RESPONSE:\n" + rawResponse); + + if (request.result == UnityWebRequest.Result.Success) + { + string wrappedJson = "{\"items\":" + rawResponse + "}"; + FurnitureVariantList list; + + try + { + list = JsonConvert.DeserializeObject(wrappedJson); + } + catch (System.Exception ex) + { + Debug.LogError("❌ Error al deserializar JSON: " + ex.Message); + yield break; + } + + var root = uiDocument.rootVisualElement; + var scrollView = root.Q("furniture-list"); + + scrollView.Clear(); + + foreach (var variant in list.items) + { + VisualElement item = itemTemplate.CloneTree(); + item.Q