From cda62f9d2525914f5cf642f1aa9f01aa8e12f9de Mon Sep 17 00:00:00 2001 From: Jannik Lassahn Date: Wed, 28 Mar 2018 23:48:39 +0200 Subject: [PATCH] Fix fullscreen mode for WebGL --- Assets/Examples/Scenes/Vimeo Player.unity | 16 +-- Assets/Examples/Scenes/YouTube Player.unity | 106 ++++++++++++------ .../VideoPlayer Helper/Scripts/ClickRouter.cs | 2 +- .../Scripts/DirectClickRouter.cs | 36 ++++++ .../Scripts/DirectClickRouter.cs.meta | 13 +++ .../Scripts/DisplayController.cs | 8 +- Assets/VideoPlayer Helper/Scripts/Timeline.cs | 17 +-- .../Scripts/VideoPresenter.cs | 13 ++- ProjectSettings/ProjectSettings.asset | 1 - 9 files changed, 147 insertions(+), 65 deletions(-) create mode 100644 Assets/VideoPlayer Helper/Scripts/DirectClickRouter.cs create mode 100644 Assets/VideoPlayer Helper/Scripts/DirectClickRouter.cs.meta diff --git a/Assets/Examples/Scenes/Vimeo Player.unity b/Assets/Examples/Scenes/Vimeo Player.unity index cbea956..1798160 100644 --- a/Assets/Examples/Scenes/Vimeo Player.unity +++ b/Assets/Examples/Scenes/Vimeo Player.unity @@ -429,11 +429,6 @@ MonoBehaviour: handleRect: {fileID: 1445815480} tooltipRect: {fileID: 879018526} position: 0.444 - onPositionChanged: - m_PersistentCalls: - m_Calls: [] - m_TypeName: Unity.VideoHelper.FloatEvent, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, - PublicKeyToken=null onSeeked: m_PersistentCalls: m_Calls: @@ -1903,7 +1898,7 @@ MonoBehaviour: m_GameObject: {fileID: 1657851164} m_Enabled: 1 m_EditorHideFlags: 0 - m_Script: {fileID: 1392445389, guid: f70555f144d8491a825f0804e09c671c, type: 3} + m_Script: {fileID: 11500000, guid: a6e5a9941adb81a4b9351a703392f514, type: 3} m_Name: m_EditorClassIdentifier: m_Navigation: @@ -1931,10 +1926,15 @@ MonoBehaviour: m_DisabledTrigger: Disabled m_Interactable: 1 m_TargetGraphic: {fileID: 1657851168} - m_OnClick: + OnClick: m_PersistentCalls: m_Calls: [] - m_TypeName: UnityEngine.UI.Button+ButtonClickedEvent, UnityEngine.UI, Version=1.0.0.0, + m_TypeName: UnityEngine.Events.UnityEvent, UnityEngine.CoreModule, Version=0.0.0.0, + Culture=neutral, PublicKeyToken=null + OnDoubleClick: + m_PersistentCalls: + m_Calls: [] + m_TypeName: UnityEngine.Events.UnityEvent, UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null --- !u!114 &1657851168 MonoBehaviour: diff --git a/Assets/Examples/Scenes/YouTube Player.unity b/Assets/Examples/Scenes/YouTube Player.unity index cec53bd..38b9b6a 100644 --- a/Assets/Examples/Scenes/YouTube Player.unity +++ b/Assets/Examples/Scenes/YouTube Player.unity @@ -146,10 +146,10 @@ RectTransform: m_Father: {fileID: 2040481571} m_RootOrder: 5 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} - m_AnchorMin: {x: 0, y: 0} - m_AnchorMax: {x: 0, y: 0} - m_AnchoredPosition: {x: 0, y: 0} - m_SizeDelta: {x: 0, y: 0} + m_AnchorMin: {x: 0, y: 1} + m_AnchorMax: {x: 0, y: 1} + m_AnchoredPosition: {x: 50, y: -135} + m_SizeDelta: {x: 100, y: 30} m_Pivot: {x: 0.5, y: 0.5} --- !u!114 &77671953 MonoBehaviour: @@ -378,7 +378,7 @@ GameObject: m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 - m_IsActive: 1 + m_IsActive: 0 --- !u!224 &158675321 RectTransform: m_ObjectHideFlags: 0 @@ -1286,11 +1286,6 @@ MonoBehaviour: handleRect: {fileID: 1911512926} tooltipRect: {fileID: 1920210364} position: 0.398 - onPositionChanged: - m_PersistentCalls: - m_Calls: [] - m_TypeName: Unity.VideoHelper.FloatEvent, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, - PublicKeyToken=null onSeeked: m_PersistentCalls: m_Calls: @@ -1613,6 +1608,7 @@ GameObject: - component: {fileID: 495730001} - component: {fileID: 495730003} - component: {fileID: 495730002} + - component: {fileID: 495730005} - component: {fileID: 495730004} m_Layer: 5 m_Name: Normal | Fullscreen @@ -1691,6 +1687,52 @@ MonoBehaviour: m_FlexibleWidth: -1 m_FlexibleHeight: -1 m_LayoutPriority: 1 +--- !u!114 &495730005 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 495730000} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: a6e5a9941adb81a4b9351a703392f514, type: 3} + m_Name: + m_EditorClassIdentifier: + m_Navigation: + m_Mode: 3 + m_SelectOnUp: {fileID: 0} + m_SelectOnDown: {fileID: 0} + m_SelectOnLeft: {fileID: 0} + m_SelectOnRight: {fileID: 0} + m_Transition: 1 + m_Colors: + m_NormalColor: {r: 1, g: 1, b: 1, a: 1} + m_HighlightedColor: {r: 0.9607843, g: 0.9607843, b: 0.9607843, a: 1} + m_PressedColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 1} + m_DisabledColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 0.5019608} + m_ColorMultiplier: 1 + m_FadeDuration: 0.1 + m_SpriteState: + m_HighlightedSprite: {fileID: 0} + m_PressedSprite: {fileID: 0} + m_DisabledSprite: {fileID: 0} + m_AnimationTriggers: + m_NormalTrigger: Normal + m_HighlightedTrigger: Highlighted + m_PressedTrigger: Pressed + m_DisabledTrigger: Disabled + m_Interactable: 1 + m_TargetGraphic: {fileID: 495730002} + OnClick: + m_PersistentCalls: + m_Calls: [] + m_TypeName: UnityEngine.Events.UnityEvent, UnityEngine.CoreModule, Version=0.0.0.0, + Culture=neutral, PublicKeyToken=null + OnDoubleClick: + m_PersistentCalls: + m_Calls: [] + m_TypeName: UnityEngine.Events.UnityEvent, UnityEngine.CoreModule, Version=0.0.0.0, + Culture=neutral, PublicKeyToken=null --- !u!1 &528747141 GameObject: m_ObjectHideFlags: 0 @@ -1724,10 +1766,10 @@ RectTransform: m_Father: {fileID: 2040481571} m_RootOrder: 6 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} - m_AnchorMin: {x: 0, y: 0} - m_AnchorMax: {x: 0, y: 0} - m_AnchoredPosition: {x: 0, y: 0} - m_SizeDelta: {x: 0, y: 0} + m_AnchorMin: {x: 0, y: 1} + m_AnchorMax: {x: 0, y: 1} + m_AnchoredPosition: {x: 50, y: -165} + m_SizeDelta: {x: 100, y: 30} m_Pivot: {x: 0.5, y: 0.5} --- !u!114 &528747143 MonoBehaviour: @@ -3296,10 +3338,10 @@ RectTransform: m_Father: {fileID: 2040481571} m_RootOrder: 2 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} - m_AnchorMin: {x: 0, y: 0} - m_AnchorMax: {x: 0, y: 0} - m_AnchoredPosition: {x: 0, y: 0} - m_SizeDelta: {x: 0, y: 0} + m_AnchorMin: {x: 0, y: 1} + m_AnchorMax: {x: 0, y: 1} + m_AnchoredPosition: {x: 50, y: -45} + m_SizeDelta: {x: 100, y: 30} m_Pivot: {x: 0.5, y: 0.5} --- !u!114 &1140252625 MonoBehaviour: @@ -3477,10 +3519,10 @@ RectTransform: m_Father: {fileID: 2040481571} m_RootOrder: 4 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} - m_AnchorMin: {x: 0, y: 0} - m_AnchorMax: {x: 0, y: 0} - m_AnchoredPosition: {x: 0, y: 0} - m_SizeDelta: {x: 0, y: 0} + m_AnchorMin: {x: 0, y: 1} + m_AnchorMax: {x: 0, y: 1} + m_AnchoredPosition: {x: 50, y: -105} + m_SizeDelta: {x: 100, y: 30} m_Pivot: {x: 0.5, y: 0.5} --- !u!114 &1232994918 MonoBehaviour: @@ -3590,10 +3632,10 @@ RectTransform: m_Father: {fileID: 2040481571} m_RootOrder: 3 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} - m_AnchorMin: {x: 0, y: 0} - m_AnchorMax: {x: 0, y: 0} - m_AnchoredPosition: {x: 0, y: 0} - m_SizeDelta: {x: 0, y: 0} + m_AnchorMin: {x: 0, y: 1} + m_AnchorMax: {x: 0, y: 1} + m_AnchoredPosition: {x: 50, y: -75} + m_SizeDelta: {x: 100, y: 30} m_Pivot: {x: 0.5, y: 0.5} --- !u!114 &1342722966 MonoBehaviour: @@ -3958,10 +4000,10 @@ RectTransform: m_Father: {fileID: 2040481571} m_RootOrder: 1 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} - m_AnchorMin: {x: 0, y: 0} - m_AnchorMax: {x: 0, y: 0} - m_AnchoredPosition: {x: 0, y: 0} - m_SizeDelta: {x: 0, y: 0} + m_AnchorMin: {x: 0, y: 1} + m_AnchorMax: {x: 0, y: 1} + m_AnchoredPosition: {x: 50, y: -15} + m_SizeDelta: {x: 100, y: 30} m_Pivot: {x: 0.5, y: 0.5} --- !u!114 &1595895798 MonoBehaviour: @@ -5062,7 +5104,7 @@ GameObject: m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 - m_IsActive: 1 + m_IsActive: 0 --- !u!224 &2040481571 RectTransform: m_ObjectHideFlags: 0 @@ -5086,7 +5128,7 @@ RectTransform: m_AnchorMin: {x: 1, y: 0} m_AnchorMax: {x: 1, y: 0} m_AnchoredPosition: {x: -50, y: 90} - m_SizeDelta: {x: 100, y: 0} + m_SizeDelta: {x: 100, y: 180} m_Pivot: {x: 0.5, y: 0.5} --- !u!114 &2040481572 MonoBehaviour: diff --git a/Assets/VideoPlayer Helper/Scripts/ClickRouter.cs b/Assets/VideoPlayer Helper/Scripts/ClickRouter.cs index 8ea1329..d586717 100644 --- a/Assets/VideoPlayer Helper/Scripts/ClickRouter.cs +++ b/Assets/VideoPlayer Helper/Scripts/ClickRouter.cs @@ -10,7 +10,7 @@ public class ClickRouter : Selectable, IPointerClickHandler public UnityEvent OnClick = new UnityEvent(); public UnityEvent OnDoubleClick = new UnityEvent(); - public void OnPointerClick(PointerEventData eventData) + public virtual void OnPointerClick(PointerEventData eventData) { if (eventData.clickCount == 2) OnDoubleClick.Invoke(); diff --git a/Assets/VideoPlayer Helper/Scripts/DirectClickRouter.cs b/Assets/VideoPlayer Helper/Scripts/DirectClickRouter.cs new file mode 100644 index 0000000..475c4e1 --- /dev/null +++ b/Assets/VideoPlayer Helper/Scripts/DirectClickRouter.cs @@ -0,0 +1,36 @@ +using UnityEngine; +using UnityEngine.EventSystems; + +namespace Unity.VideoHelper +{ + /// + /// Forwards clicks when the pointer is pressed. + /// Use for forwarding upon release of the pointer. + /// + /// + /// Use this component on GameObjects that initiate fullscreen in WebGL builds. + /// There is a browser limitation that ensures that you can only go fullscreen when a user clicks the page. + /// Not using requires another click for Unity to go into fullscreen. + /// + public class DirectClickRouter : ClickRouter + { + private float lastClickTime = 0f; + private const float clickInterval = 0.3f; + + public override void OnPointerClick(PointerEventData eventData) + { + // do nothing + } + + public override void OnPointerDown(PointerEventData eventData) + { + if (lastClickTime + clickInterval > Time.time) + OnDoubleClick.Invoke(); + else + OnClick.Invoke(); + + lastClickTime = Time.time; + } + } +} + diff --git a/Assets/VideoPlayer Helper/Scripts/DirectClickRouter.cs.meta b/Assets/VideoPlayer Helper/Scripts/DirectClickRouter.cs.meta new file mode 100644 index 0000000..2339389 --- /dev/null +++ b/Assets/VideoPlayer Helper/Scripts/DirectClickRouter.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: a6e5a9941adb81a4b9351a703392f514 +timeCreated: 1522269432 +licenseType: Free +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/VideoPlayer Helper/Scripts/DisplayController.cs b/Assets/VideoPlayer Helper/Scripts/DisplayController.cs index cff2eca..fa277b8 100644 --- a/Assets/VideoPlayer Helper/Scripts/DisplayController.cs +++ b/Assets/VideoPlayer Helper/Scripts/DisplayController.cs @@ -47,7 +47,7 @@ internal DisplayControllerInternal(int display) private GameObject fullscreenCanvas; private RectTransform target, targetParent; - private bool IsAlwaysFullscreen; + private bool isAlwaysFullscreen; private int targetDisplay; #endregion @@ -86,7 +86,7 @@ public void ToFullscreen(RectTransform rectTransform) target.anchorMax = target.offsetMax = Vector2.one; target.localScale = Vector3.one; - IsAlwaysFullscreen = Screen.fullScreen; + isAlwaysFullscreen = Screen.fullScreen; Screen.fullScreen = true; } @@ -105,7 +105,7 @@ public void ToNormal() fullscreenCanvas.SetActive(false); - Screen.fullScreen = IsAlwaysFullscreen; + Screen.fullScreen = isAlwaysFullscreen; } #endregion @@ -121,7 +121,7 @@ private void Setup() canvas.targetDisplay = targetDisplay; var scaler = fullscreenCanvas.GetComponent(); - scaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize; + scaler.uiScaleMode = CanvasScaler.ScaleMode.ConstantPixelSize; } #endregion diff --git a/Assets/VideoPlayer Helper/Scripts/Timeline.cs b/Assets/VideoPlayer Helper/Scripts/Timeline.cs index d89fac8..f4c42ac 100644 --- a/Assets/VideoPlayer Helper/Scripts/Timeline.cs +++ b/Assets/VideoPlayer Helper/Scripts/Timeline.cs @@ -35,9 +35,6 @@ public class Timeline : Selectable, IDragHandler, ICanvasElement, IInitializePot [Range(0, 1)] private float position; - [SerializeField] - private FloatEvent onPositionChanged = new FloatEvent(); - [SerializeField] private FloatEvent onSeeked = new FloatEvent(); @@ -75,11 +72,6 @@ public float Position set { SetPosition(value); } } - //public UnityEvent OnPositionChanged - //{ - // get { return onPositionChanged; } - //} - public UnityEvent OnSeeked { get { return onSeeked; } @@ -305,7 +297,7 @@ private void UpdateDrag(PointerEventData eventData, Camera cam) localCursor -= clickRect.rect.position; Position = Mathf.Clamp01((localCursor - handleOffset)[0] / clickRect.rect.size[0]); - OnSeeked.Invoke(Position); + onSeeked.Invoke(Position); } } @@ -329,9 +321,6 @@ private void SetPosition(float newPosition, bool sendCallback = true) position = newPosition; UpdateVisuals(); - - //if (sendCallback) - // OnPositionChanged.Invoke(newPosition); } #endregion @@ -425,10 +414,6 @@ public virtual void OnDrag(PointerEventData eventData) public void Rebuild(CanvasUpdate executing) { -#if UNITY_EDITOR - if (executing == CanvasUpdate.Prelayout) - onPositionChanged.Invoke(position); -#endif } public void LayoutComplete() diff --git a/Assets/VideoPlayer Helper/Scripts/VideoPresenter.cs b/Assets/VideoPlayer Helper/Scripts/VideoPresenter.cs index 15d375e..74a833b 100644 --- a/Assets/VideoPlayer Helper/Scripts/VideoPresenter.cs +++ b/Assets/VideoPlayer Helper/Scripts/VideoPresenter.cs @@ -110,6 +110,12 @@ private void Start() PlayPause.OnClick(ToggleIsPlaying); NormalFullscreen.OnClick(ToggleFullscreen); + +#if UNITY_WEBGL + // FIX for not being able to go fullscreen in WebGL. See DirectClickRouter for details. + Screen.gameObject.AddComponent(); +#endif + Screen.OnDoubleClick(ToggleFullscreen); Screen.OnClick(ToggleIsPlaying); @@ -138,14 +144,14 @@ private void Update() Timeline.Position = controller.NormalizedTime; } - #endregion +#endregion public string GetFormattedPosition(float time) { return PrettyTimeFormat(TimeSpan.FromSeconds(time * controller.Duration)); } - #region Private methods +#region Private methods private void ToggleMute() { @@ -196,6 +202,7 @@ private void ToggleIsPlaying() private void OnStartedPlaying() { + Screen.SetGameObjectActive(true); ControlsPanel.SetGameObjectActive(true); LoadingIndicator.SetGameObjectActive(false); @@ -258,7 +265,7 @@ private string PrettyTimeFormat(TimeSpan time) return string.Format(HoursFormat, time.Hours, time.Minutes, time.Seconds); } - #endregion +#endregion } } diff --git a/ProjectSettings/ProjectSettings.asset b/ProjectSettings/ProjectSettings.asset index 9ff498d..000e7ef 100644 --- a/ProjectSettings/ProjectSettings.asset +++ b/ProjectSettings/ProjectSettings.asset @@ -204,7 +204,6 @@ PlayerSettings: tvOSSmallIconLayers: [] tvOSSmallIconLayers2x: [] tvOSLargeIconLayers: [] - tvOSLargeIconLayers2x: [] tvOSTopShelfImageLayers: [] tvOSTopShelfImageLayers2x: [] tvOSTopShelfImageWideLayers: []