First version of UI consumed from API
This commit is contained in:
		
							
								
								
									
										62
									
								
								Assets/API/FurnitureFetcher.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										62
									
								
								Assets/API/FurnitureFetcher.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,62 @@ | ||||
| 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); | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										11
									
								
								Assets/API/FurnitureFetcher.cs.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								Assets/API/FurnitureFetcher.cs.meta
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | ||||
| fileFormatVersion: 2 | ||||
| guid: 3cef81e5bb5c96f40a4dd2c92b625461 | ||||
| MonoImporter: | ||||
|   externalObjects: {} | ||||
|   serializedVersion: 2 | ||||
|   defaultReferences: [] | ||||
|   executionOrder: 0 | ||||
|   icon: {instanceID: 0} | ||||
|   userData:  | ||||
|   assetBundleName:  | ||||
|   assetBundleVariant:  | ||||
							
								
								
									
										18
									
								
								Assets/API/FurnitureSceneLoader.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								Assets/API/FurnitureSceneLoader.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,18 @@ | ||||
| using System.Collections; | ||||
| using System.Collections.Generic; | ||||
| using UnityEngine; | ||||
| using UnityEngine.SceneManagement; | ||||
|  | ||||
| public class FurnitureSceneLoader : MonoBehaviour | ||||
| { | ||||
|     public void LoadSceneByName(string sceneName) | ||||
|     { | ||||
|         SceneManager.LoadScene(sceneName); | ||||
|     } | ||||
|  | ||||
|     // También puedes usar índice si prefieres: | ||||
|     public void LoadSceneByIndex(int index) | ||||
|     { | ||||
|         SceneManager.LoadScene(index); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										11
									
								
								Assets/API/FurnitureSceneLoader.cs.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								Assets/API/FurnitureSceneLoader.cs.meta
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | ||||
| fileFormatVersion: 2 | ||||
| guid: 91f284e82a188974b93e9fe03f967300 | ||||
| MonoImporter: | ||||
|   externalObjects: {} | ||||
|   serializedVersion: 2 | ||||
|   defaultReferences: [] | ||||
|   executionOrder: 0 | ||||
|   icon: {instanceID: 0} | ||||
|   userData:  | ||||
|   assetBundleName:  | ||||
|   assetBundleVariant:  | ||||
							
								
								
									
										8
									
								
								Assets/API/Models.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								Assets/API/Models.meta
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,8 @@ | ||||
| fileFormatVersion: 2 | ||||
| guid: e6667553da6104042891509ac68a2089 | ||||
| folderAsset: yes | ||||
| DefaultImporter: | ||||
|   externalObjects: {} | ||||
|   userData:  | ||||
|   assetBundleName:  | ||||
|   assetBundleVariant:  | ||||
							
								
								
									
										30
									
								
								Assets/API/Models/FurnitureItem.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								Assets/API/Models/FurnitureItem.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,30 @@ | ||||
| using System; | ||||
| using System.Collections.Generic; | ||||
|  | ||||
| [Serializable] | ||||
| public class FurnitureIdsRequest | ||||
| { | ||||
|     public List<string> ids; | ||||
| } | ||||
|  | ||||
| [Serializable] | ||||
| public class FurnitureVariant | ||||
| { | ||||
|     public string modelId; | ||||
|     public string name; | ||||
|     public string color; | ||||
|     public string line; | ||||
|     public string _id; | ||||
|     public string id; | ||||
|     public string createdAt; | ||||
|     public string createdBy; | ||||
|     public string updatedAt; | ||||
|     public string updatedBy; | ||||
|     public string status; | ||||
| } | ||||
|  | ||||
| [Serializable] | ||||
| public class FurnitureVariantList | ||||
| { | ||||
|     public List<FurnitureVariant> items; | ||||
| } | ||||
							
								
								
									
										11
									
								
								Assets/API/Models/FurnitureItem.cs.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								Assets/API/Models/FurnitureItem.cs.meta
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | ||||
| fileFormatVersion: 2 | ||||
| guid: 82b08171216be2d47b45bbc738118fde | ||||
| MonoImporter: | ||||
|   externalObjects: {} | ||||
|   serializedVersion: 2 | ||||
|   defaultReferences: [] | ||||
|   executionOrder: 0 | ||||
|   icon: {instanceID: 0} | ||||
|   userData:  | ||||
|   assetBundleName:  | ||||
|   assetBundleVariant:  | ||||
							
								
								
									
										8
									
								
								Assets/API/Styles.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								Assets/API/Styles.meta
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,8 @@ | ||||
| fileFormatVersion: 2 | ||||
| guid: 10d82c400c9a6bd48bd2e50509bf8e8f | ||||
| folderAsset: yes | ||||
| DefaultImporter: | ||||
|   externalObjects: {} | ||||
|   userData:  | ||||
|   assetBundleName:  | ||||
|   assetBundleVariant:  | ||||
							
								
								
									
										7
									
								
								Assets/API/Styles/FurnitureItem.uxml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								Assets/API/Styles/FurnitureItem.uxml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,7 @@ | ||||
| <ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | ||||
|     <ui:VisualElement class="item-container" style="margin: 5px; padding: 8px; border-bottom: 1px solid #ccc;"> | ||||
|         <ui:Label name="nameLabel" /> | ||||
|         <ui:Label name="colorLabel" /> | ||||
|         <ui:Label name="lineLabel" /> | ||||
|     </ui:VisualElement> | ||||
| </ui:UXML> | ||||
							
								
								
									
										10
									
								
								Assets/API/Styles/FurnitureItem.uxml.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								Assets/API/Styles/FurnitureItem.uxml.meta
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,10 @@ | ||||
| fileFormatVersion: 2 | ||||
| guid: 13b2bef3f605fb04586c1110bfcbe58f | ||||
| ScriptedImporter: | ||||
|   internalIDToNameTable: [] | ||||
|   externalObjects: {} | ||||
|   serializedVersion: 2 | ||||
|   userData:  | ||||
|   assetBundleName:  | ||||
|   assetBundleVariant:  | ||||
|   script: {fileID: 13804, guid: 0000000000000000e000000000000000, type: 0} | ||||
							
								
								
									
										6
									
								
								Assets/API/Styles/MainScreen.uxml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								Assets/API/Styles/MainScreen.uxml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,6 @@ | ||||
| <ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | ||||
|     <ui:VisualElement style="flex-grow: 1; padding: 10px;"> | ||||
|         <ui:Label text="Furniture List" style="unity-font-style: bold; font-size: 20px; margin-bottom: 10px;" /> | ||||
|         <ui:ScrollView name="furniture-list" style="flex-grow: 1;" /> | ||||
|     </ui:VisualElement> | ||||
| </ui:UXML> | ||||
							
								
								
									
										10
									
								
								Assets/API/Styles/MainScreen.uxml.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								Assets/API/Styles/MainScreen.uxml.meta
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,10 @@ | ||||
| fileFormatVersion: 2 | ||||
| guid: a4152151ddf5a734ab4b95fdb521e68a | ||||
| ScriptedImporter: | ||||
|   internalIDToNameTable: [] | ||||
|   externalObjects: {} | ||||
|   serializedVersion: 2 | ||||
|   userData:  | ||||
|   assetBundleName:  | ||||
|   assetBundleVariant:  | ||||
|   script: {fileID: 13804, guid: 0000000000000000e000000000000000, type: 0} | ||||
		Reference in New Issue
	
	Block a user