|
| 1 | +// UnityWebBrowser (UWB) |
| 2 | +// Copyright (c) 2021-2024 Voltstro-Studios |
| 3 | +// |
| 4 | +// This project is under the MIT license. See the LICENSE.md file for more details. |
| 5 | + |
| 6 | +using UnityEngine; |
| 7 | +using UnityEngine.UI; |
| 8 | +using VoltstroStudios.UnityWebBrowser.Communication; |
| 9 | +using VoltstroStudios.UnityWebBrowser.Core.Engines; |
| 10 | +using VoltstroStudios.UnityWebBrowser.Input; |
| 11 | +using VoltstroStudios.UnityWebBrowser.Shared.Core; |
| 12 | + |
| 13 | +namespace VoltstroStudios.UnityWebBrowser.Prj |
| 14 | +{ |
| 15 | + /// <summary> |
| 16 | + /// Demo script for creating UWB at runtime |
| 17 | + /// </summary> |
| 18 | + public sealed class UWBRuntime : MonoBehaviour |
| 19 | + { |
| 20 | + /// <summary> |
| 21 | + /// <see cref="GameObject"/> to create everything on |
| 22 | + /// </summary> |
| 23 | + [SerializeField] |
| 24 | + private GameObject container; |
| 25 | + |
| 26 | + [SerializeField] |
| 27 | + private Camera mainCamera; |
| 28 | + |
| 29 | + private void Start() |
| 30 | + { |
| 31 | + //Create canvas |
| 32 | + Canvas canvas = container.AddComponent<Canvas>(); |
| 33 | + canvas.renderMode = RenderMode.ScreenSpaceCamera; |
| 34 | + canvas.worldCamera = mainCamera; |
| 35 | + |
| 36 | + CanvasScaler canvasScaler = container.AddComponent<CanvasScaler>(); |
| 37 | + canvasScaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize; |
| 38 | + canvasScaler.referenceResolution = new Vector2(1920, 1080); |
| 39 | + canvasScaler.screenMatchMode = CanvasScaler.ScreenMatchMode.MatchWidthOrHeight; |
| 40 | + canvasScaler.matchWidthOrHeight = 0; |
| 41 | + |
| 42 | + container.AddComponent<GraphicRaycaster>(); |
| 43 | + |
| 44 | + //Child object, where raw image and UWB itself will live |
| 45 | + GameObject uwbGameObject = new("UWBContainer"); |
| 46 | + uwbGameObject.transform.SetParent(container.transform); |
| 47 | + |
| 48 | + //Configure rect transform |
| 49 | + RectTransform uwbRectTransform = uwbGameObject.AddComponent<RectTransform>(); |
| 50 | + uwbRectTransform.anchorMin = Vector2.zero; |
| 51 | + uwbRectTransform.anchorMax = Vector2.one; |
| 52 | + uwbRectTransform.pivot = new Vector2(0.5f, 0.5f); |
| 53 | + uwbRectTransform.localScale = Vector3.one; |
| 54 | + uwbRectTransform.offsetMin = Vector2.zero; |
| 55 | + uwbRectTransform.offsetMax = Vector2.zero; |
| 56 | + |
| 57 | + //Add raw image |
| 58 | + uwbGameObject.AddComponent<RawImage>(); |
| 59 | + |
| 60 | + //UWB Pre-Setup |
| 61 | + |
| 62 | + //Create engine dynamically |
| 63 | + EngineConfiguration engineConfig = ScriptableObject.CreateInstance<EngineConfiguration>(); |
| 64 | + engineConfig.engineAppName = "UnityWebBrowser.Engine.Cef"; |
| 65 | + |
| 66 | +#if UNITY_EDITOR |
| 67 | + engineConfig.engineFiles = new[] { |
| 68 | + new Engine.EnginePlatformFiles |
| 69 | + { |
| 70 | + platform = Platform.Windows64, |
| 71 | + engineFileLocation = "Packages/dev.voltstro.unitywebbrowser.engine.cef.win.x64/Engine~/" |
| 72 | + }, |
| 73 | + new Engine.EnginePlatformFiles |
| 74 | + { |
| 75 | + platform = Platform.Linux64, |
| 76 | + engineFileLocation = "Packages/dev.voltstro.unitywebbrowser.engine.cef.linux.x64/Engine~/" |
| 77 | + } |
| 78 | + }; |
| 79 | +#endif |
| 80 | + |
| 81 | + //Create coms layer dynamically |
| 82 | + CommunicationLayer comsLayer = ScriptableObject.CreateInstance<TCPCommunicationLayer>(); |
| 83 | + |
| 84 | + //Create input handler dynamically |
| 85 | + WebBrowserInputHandler inputHandler = ScriptableObject.CreateInstance<WebBrowserOldInputHandler>(); |
| 86 | + |
| 87 | + //UWB Object Setup |
| 88 | + WebBrowserUIBasic webBrowser = uwbGameObject.AddComponent<WebBrowserUIBasic>(); |
| 89 | + webBrowser.browserClient.engine = engineConfig; |
| 90 | + webBrowser.browserClient.communicationLayer = comsLayer; |
| 91 | + webBrowser.inputHandler = inputHandler; |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments