Spawn WebView(s) on demand
This commit is contained in:
parent
045f30e4e5
commit
142b5c8338
|
@ -4119,7 +4119,7 @@ GameObject:
|
|||
m_Icon: {fileID: 0}
|
||||
m_NavMeshLayer: 0
|
||||
m_StaticEditorFlags: 0
|
||||
m_IsActive: 1
|
||||
m_IsActive: 0
|
||||
--- !u!114 &721930530
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
|
@ -10104,7 +10104,7 @@ GameObject:
|
|||
m_Icon: {fileID: 0}
|
||||
m_NavMeshLayer: 0
|
||||
m_StaticEditorFlags: 0
|
||||
m_IsActive: 1
|
||||
m_IsActive: 0
|
||||
--- !u!224 &2137857638
|
||||
RectTransform:
|
||||
m_ObjectHideFlags: 0
|
||||
|
|
|
@ -14,6 +14,9 @@ public class EndpointLoader : MonoBehaviour
|
|||
[SerializeField]
|
||||
private WebView webView2;
|
||||
|
||||
[SerializeField]
|
||||
private GameObject dynamicItem;
|
||||
|
||||
[SerializeField]
|
||||
private ServiceDiscovery serviceDiscovery;
|
||||
|
||||
|
@ -23,6 +26,7 @@ public class EndpointLoader : MonoBehaviour
|
|||
private string apiUrl;
|
||||
private bool triedMulticast = false;
|
||||
private bool defaultEndpointLoaded = false;
|
||||
private List<GameObject> instantiatedItems = new List<GameObject>();
|
||||
private HashSet<MdnsService> availableServices = new HashSet<MdnsService>();
|
||||
|
||||
private const string defaultApiUrl = "http://windows.local:5000/api/endpoints";
|
||||
|
@ -35,6 +39,56 @@ public class EndpointLoader : MonoBehaviour
|
|||
StartCoroutine(LoadEndpoints());
|
||||
}
|
||||
|
||||
private Vector3 CalculateNextPosition()
|
||||
{
|
||||
if (instantiatedItems.Count == 0)
|
||||
{
|
||||
// return dynamicItem.transform.position;
|
||||
return new Vector3(-0.4f, 0.1f, 1);
|
||||
}
|
||||
|
||||
GameObject lastItem = instantiatedItems[instantiatedItems.Count - 1];
|
||||
|
||||
Vector3 lastPosition = lastItem.transform.position;
|
||||
float itemWidth = GetItemWidth(lastItem);
|
||||
return new Vector3(lastPosition.x + itemWidth, lastPosition.y, lastPosition.z);
|
||||
}
|
||||
|
||||
private float GetItemWidth(GameObject item)
|
||||
{
|
||||
Renderer renderer = item.GetComponent<Renderer>();
|
||||
if (renderer != null)
|
||||
{
|
||||
return renderer.bounds.size.x;
|
||||
}
|
||||
|
||||
return 1.0f;
|
||||
}
|
||||
|
||||
public void SpawnItem(string url)
|
||||
{
|
||||
if (dynamicItem != null)
|
||||
{
|
||||
Vector3 nextPosition = CalculateNextPosition();
|
||||
|
||||
GameObject newItem = Instantiate(dynamicItem, nextPosition, dynamicItem.transform.rotation, dynamicItem.transform.parent);
|
||||
newItem.SetActive(true);
|
||||
|
||||
instantiatedItems.Add(newItem);
|
||||
|
||||
var webView = newItem.GetComponentInChildren<WebView>();
|
||||
|
||||
if (webView != null)
|
||||
{
|
||||
webView.Load(url);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("Dynamic item is not assigned.");
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator TryLoadingFromDefaultEndpoints()
|
||||
{
|
||||
using (UnityWebRequest request = UnityWebRequest.Get(defaultEndpoint1))
|
||||
|
@ -110,8 +164,15 @@ public class EndpointLoader : MonoBehaviour
|
|||
}
|
||||
else
|
||||
{
|
||||
webView1.Load(endpoints[0].url ?? defaultEndpoint1);
|
||||
webView2.Load(endpoints[1].url ?? defaultEndpoint2);
|
||||
foreach (var endpoint in endpoints)
|
||||
{
|
||||
if (endpoint.url == null || endpoint.url.Length == 0)
|
||||
{
|
||||
Debug.LogWarning($"Endpoint URL is null for endpoint");
|
||||
continue;
|
||||
}
|
||||
SpawnItem(endpoint.url);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -153,8 +214,8 @@ public class EndpointLoader : MonoBehaviour
|
|||
|
||||
private void UseDefaultEndpoints()
|
||||
{
|
||||
webView1.Load(defaultEndpoint1);
|
||||
webView2.Load(defaultEndpoint2);
|
||||
// webView1.Load(defaultEndpoint1);
|
||||
// webView2.Load(defaultEndpoint2);
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
|
|
Loading…
Reference in New Issue