From 644b2f80ccc5d7646e72a9fcee6fc0dfedb87693 Mon Sep 17 00:00:00 2001 From: Santiago Lo Coco Date: Tue, 5 Nov 2024 23:33:14 +0100 Subject: [PATCH] Support N canvases (webview gameobjects) --- Assets/Scenes/WebView.unity | 14 ++--- Assets/Scripts/ConfigureNavBar.cs | 66 ++++++++++-------------- Assets/Scripts/ConfigureOrbital.cs | 83 +++++++++--------------------- Assets/Scripts/EndpointLoader.cs | 24 +++------ 4 files changed, 66 insertions(+), 121 deletions(-) diff --git a/Assets/Scenes/WebView.unity b/Assets/Scenes/WebView.unity index 7057ca3..cf1898a 100644 --- a/Assets/Scenes/WebView.unity +++ b/Assets/Scenes/WebView.unity @@ -5468,7 +5468,7 @@ MonoBehaviour: m_Calls: - m_Target: {fileID: 1193822414} m_TargetAssemblyTypeName: ConfigureOrbital, Assembly-CSharp - m_MethodName: ToggleOrbitalScript + m_MethodName: ToggleOrbital m_Mode: 1 m_Arguments: m_ObjectArgument: {fileID: 0} @@ -6408,10 +6408,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 2692de2cb7707be478671c78efb37020, type: 3} m_Name: m_EditorClassIdentifier: - canvas1: {fileID: 721930529} - canvas2: {fileID: 2137857637} - addressField1: {fileID: 76995324} - addressField2: {fileID: 102826381} + endpointLoader: {fileID: 491721038} --- !u!1 &1041187335 GameObject: m_ObjectHideFlags: 0 @@ -6911,8 +6908,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 487c06418920ab945bfc039c0ba92d8a, type: 3} m_Name: m_EditorClassIdentifier: - canvas1: {fileID: 721930529} - canvas2: {fileID: 2137857637} + endpointLoader: {fileID: 491721038} --- !u!114 &1303729525 MonoBehaviour: m_ObjectHideFlags: 0 @@ -9091,7 +9087,7 @@ MonoBehaviour: m_Calls: - m_Target: {fileID: 1193822414} m_TargetAssemblyTypeName: ConfigureOrbital, Assembly-CSharp - m_MethodName: ToggleOrbitalScript + m_MethodName: ToggleOrbital m_Mode: 1 m_Arguments: m_ObjectArgument: {fileID: 0} @@ -10203,7 +10199,7 @@ GameObject: - component: {fileID: 2094194865} - component: {fileID: 2094194864} m_Layer: 5 - m_Name: AddressField (TMP) + m_Name: AddressField m_TagString: Untagged m_Icon: {fileID: 0} m_NavMeshLayer: 0 diff --git a/Assets/Scripts/ConfigureNavBar.cs b/Assets/Scripts/ConfigureNavBar.cs index 38e7b2e..9f7aeea 100644 --- a/Assets/Scripts/ConfigureNavBar.cs +++ b/Assets/Scripts/ConfigureNavBar.cs @@ -7,52 +7,42 @@ using UnityEngine.UI; public class ConfigureNavBar : MonoBehaviour { [SerializeField] - private GameObject canvas1; - - [SerializeField] - private GameObject canvas2; - - [SerializeField] - private GameObject addressField1; - - [SerializeField] - private GameObject addressField2; - - private BoxCollider boxCollider1; - private BoxCollider boxCollider2; + private EndpointLoader endpointLoader; private bool isVisible = false; - private void Start() - { - boxCollider1 = canvas1.GetComponent(); - boxCollider2 = canvas2.GetComponent(); - } - public void ToggleVisibilityMethod() { + List canvases = endpointLoader.GetInstantiatedItems(); isVisible = !isVisible; - addressField1.SetActive(isVisible); - addressField2.SetActive(isVisible); - - if (boxCollider1 != null) + foreach (GameObject canvas in canvases) { - boxCollider1.size = new Vector3( - boxCollider1.size.x, - isVisible ? 400 : 370, - boxCollider1.size.z - ); - boxCollider1.center = new Vector3(0, isVisible ? 0 : -16, 0); - } + TMP_InputField inputField = canvas.GetComponentInChildren(true); + if (inputField != null) + { + Debug.Log("Setting address field visibility to " + isVisible); + inputField.gameObject.SetActive(isVisible); + } - if (boxCollider2 != null) - { - boxCollider2.size = new Vector3( - boxCollider2.size.x, - isVisible ? 400 : 370, - boxCollider2.size.z - ); - boxCollider2.center = new Vector3(0, isVisible ? 0 : -16, 0); + /* + Transform addressFieldTransform = canvas.transform.Find("AddressField"); + if (addressFieldTransform != null) + { + Debug.Log("Setting address field visibility to " + isVisible); + addressFieldTransform.gameObject.SetActive(isVisible); + } + */ + + BoxCollider boxCollider = canvas.GetComponent(); + if (boxCollider != null) + { + boxCollider.size = new Vector3( + boxCollider.size.x, + isVisible ? 400 : 370, + boxCollider.size.z + ); + boxCollider.center = new Vector3(0, isVisible ? 0 : -16, 0); + } } } } diff --git a/Assets/Scripts/ConfigureOrbital.cs b/Assets/Scripts/ConfigureOrbital.cs index ddc978f..cd84d20 100644 --- a/Assets/Scripts/ConfigureOrbital.cs +++ b/Assets/Scripts/ConfigureOrbital.cs @@ -8,72 +8,39 @@ using UnityEngine; public class ConfigureOrbital : MonoBehaviour { [SerializeField] - private GameObject canvas1; + private EndpointLoader endpointLoader; - [SerializeField] - private GameObject canvas2; + private bool orbitalEnabled = false; - // public List canvases; - - private Orbital orbital1; - private Orbital orbital2; - - private SolverHandler solverHandler1; - private SolverHandler solverHandler2; - - void Start() + public void ToggleOrbital() { - if (canvas1 != null) + orbitalEnabled = !orbitalEnabled; + List canvases = endpointLoader.GetInstantiatedItems(); + + foreach (GameObject canvas in canvases) { - orbital1 = canvas1.GetComponent(); - solverHandler1 = canvas1.GetComponent(); - } + Orbital orbital = canvas.GetComponent(); + SolverHandler solverHandler = canvas.GetComponent(); - if (canvas2 != null) - { - orbital2 = canvas2.GetComponent(); - solverHandler2 = canvas2.GetComponent(); - } - - if ( - orbital1 == null - || solverHandler1 == null - || orbital2 == null - || solverHandler2 == null - ) - { - Debug.LogError( - "One or both Canvas objects are missing an Orbital or SolverHandler component." - ); - } - } - - public void ToggleOrbitalScript() - { - ToggleOrbitalForCanvas(orbital1, solverHandler1); - ToggleOrbitalForCanvas(orbital2, solverHandler2); - } - - private void ToggleOrbitalForCanvas(Orbital orbital, SolverHandler solverHandler) - { - if (orbital != null && solverHandler != null) - { - orbital.enabled = !orbital.enabled; - - if (orbital.enabled) + if (orbital != null && solverHandler != null) { - Vector3 headPosition = Camera.main.transform.position; - Quaternion headRotation = Camera.main.transform.rotation; - Vector3 relativePosition = - Quaternion.Inverse(headRotation) * (orbital.transform.position - headPosition); + orbital.enabled = orbitalEnabled; - orbital.LocalOffset = relativePosition; + if (orbitalEnabled) + { + Vector3 headPosition = Camera.main.transform.position; + Quaternion headRotation = Camera.main.transform.rotation; + Vector3 relativePosition = + Quaternion.Inverse(headRotation) * (orbital.transform.position - headPosition); - solverHandler.UpdateSolvers = true; - } - else - { - solverHandler.UpdateSolvers = false; + orbital.LocalOffset = relativePosition; + + solverHandler.UpdateSolvers = true; + } + else + { + solverHandler.UpdateSolvers = false; + } } } } diff --git a/Assets/Scripts/EndpointLoader.cs b/Assets/Scripts/EndpointLoader.cs index 86b67b8..4db72d3 100644 --- a/Assets/Scripts/EndpointLoader.cs +++ b/Assets/Scripts/EndpointLoader.cs @@ -8,12 +8,6 @@ using UnityEngine.UI; public class EndpointLoader : MonoBehaviour { - [SerializeField] - private WebView webView1; - - [SerializeField] - private WebView webView2; - [SerializeField] private GameObject dynamicItem; @@ -94,24 +88,28 @@ public class EndpointLoader : MonoBehaviour } } + public List GetInstantiatedItems() + { + return instantiatedItems; + } + private IEnumerator TryLoadingFromDefaultEndpoints() { using (UnityWebRequest request = UnityWebRequest.Get(defaultEndpoint1)) { yield return request.SendWebRequest(); - ProcessEndpointResponse(request, webView1, defaultEndpoint1, ref defaultEndpointLoaded); + ProcessEndpointResponse(request, defaultEndpoint1, ref defaultEndpointLoaded); } using (UnityWebRequest request = UnityWebRequest.Get(defaultEndpoint2)) { yield return request.SendWebRequest(); - ProcessEndpointResponse(request, webView2, defaultEndpoint2, ref defaultEndpointLoaded); + ProcessEndpointResponse(request, defaultEndpoint2, ref defaultEndpointLoaded); } } private void ProcessEndpointResponse( UnityWebRequest request, - WebView webView, string endpoint, ref bool loadedFlag ) @@ -126,7 +124,7 @@ public class EndpointLoader : MonoBehaviour else { Debug.Log($"Loaded from {endpoint} successfully."); - webView.Load(endpoint); + SpawnItem(endpoint); loadedFlag = true; } } @@ -233,12 +231,6 @@ public class EndpointLoader : MonoBehaviour StartCoroutine(LoadEndpoints()); } - private void UseDefaultEndpoints() - { - // webView1.Load(defaultEndpoint1); - // webView2.Load(defaultEndpoint2); - } - [Serializable] public class Endpoint {