Support N canvases (webview gameobjects)

This commit is contained in:
Santiago Lo Coco 2024-11-05 23:33:14 +01:00
parent 8551bf9c8f
commit 644b2f80cc
4 changed files with 66 additions and 121 deletions

View File

@ -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

View File

@ -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<BoxCollider>();
boxCollider2 = canvas2.GetComponent<BoxCollider>();
}
public void ToggleVisibilityMethod()
{
List<GameObject> 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<TMP_InputField>(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<BoxCollider>();
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);
}
}
}
}

View File

@ -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<GameObject> canvases;
private Orbital orbital1;
private Orbital orbital2;
private SolverHandler solverHandler1;
private SolverHandler solverHandler2;
void Start()
public void ToggleOrbital()
{
if (canvas1 != null)
orbitalEnabled = !orbitalEnabled;
List<GameObject> canvases = endpointLoader.GetInstantiatedItems();
foreach (GameObject canvas in canvases)
{
orbital1 = canvas1.GetComponent<Orbital>();
solverHandler1 = canvas1.GetComponent<SolverHandler>();
}
Orbital orbital = canvas.GetComponent<Orbital>();
SolverHandler solverHandler = canvas.GetComponent<SolverHandler>();
if (canvas2 != null)
{
orbital2 = canvas2.GetComponent<Orbital>();
solverHandler2 = canvas2.GetComponent<SolverHandler>();
}
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;
}
}
}
}

View File

@ -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<GameObject> 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
{