|
using UnityEngine; |
|
|
|
#if UNITY_IOS |
|
using System.Collections.Generic; |
|
using UnityEngine.UI; |
|
#endif |
|
|
|
/// <summary> |
|
/// iOS Home Button. |
|
/// |
|
/// <para> |
|
/// Helps to modify the iOS home button parameters for each new scene or |
|
/// canvas on awake, you may consider add just one instance per scene. |
|
/// </para> |
|
/// |
|
/// <para> By Javier García | @jvrgms | 2019 </para> |
|
/// </summary> |
|
[RequireComponent (typeof (Canvas))] |
|
public class IOSHomeButton: MonoBehaviour { |
|
|
|
|
|
|
|
#region Class Members |
|
|
|
[SerializeField] |
|
private bool _hideHomeButton; // Flag to hide home button. |
|
|
|
[SerializeField] |
|
private bool _attenuateHomeButton; // Flag to attenuate home button. |
|
|
|
[SerializeField] |
|
private bool _usePadding; // Flag to create a padding. |
|
|
|
[SerializeField] |
|
private Sprite _bezelSprite; // Bezel Sprite for padding. |
|
|
|
|
|
// References for Padding, Background and Bazel here created. |
|
private GameObject Padding { get; set; } |
|
private GameObject Background { get; set; } |
|
private GameObject Bezel { get; set; } |
|
|
|
#endregion |
|
|
|
|
|
|
|
#region Static Accessors |
|
|
|
public static IOSHomeButton Instance { get; private set; } |
|
|
|
/// <summary> Gets or sets a value indicating whether HomeButton |
|
/// is hide. </summary> |
|
/// <value><c>true</c> if hide; otherwise, <c>false</c>.</value> |
|
public static bool Hide { |
|
|
|
#if UNITY_IOS |
|
get { |
|
return UnityEngine.iOS.Device.hideHomeButton; |
|
} |
|
set { |
|
UnityEngine.iOS.Device.hideHomeButton = value; |
|
if (Instance) |
|
Instance._hideHomeButton = value; |
|
} |
|
|
|
#else |
|
get { return true; } |
|
set { |
|
Debug.LogWarning ( |
|
string.Concat( |
|
"The value ", value, |
|
" just can be used in iOS platform." |
|
) |
|
); |
|
} |
|
#endif |
|
} |
|
|
|
/// <summary> Gets or sets a value indicating whether Home Button |
|
/// is attenuate. </summary> |
|
/// <value><c>true</c> if attenuate; otherwise, <c>false</c>.</value> |
|
public static bool Attenuate { |
|
|
|
#if UNITY_IOS |
|
get { |
|
return |
|
UnityEngine.iOS.Device.deferSystemGesturesMode != |
|
UnityEngine.iOS.SystemGestureDeferMode.None; |
|
} |
|
set { |
|
UnityEngine.iOS.Device.deferSystemGesturesMode = value ? |
|
UnityEngine.iOS.SystemGestureDeferMode.BottomEdge : |
|
UnityEngine.iOS.SystemGestureDeferMode.None; |
|
if (Instance) |
|
Instance._attenuateHomeButton = value; |
|
} |
|
|
|
#else |
|
get { return true; } |
|
set { |
|
Debug.LogWarning ( |
|
string.Concat ( |
|
"The value ", value, |
|
" just can be used in iOS platform." |
|
) |
|
); |
|
} |
|
#endif |
|
} |
|
|
|
#endregion |
|
|
|
|
|
|
|
#region Private Accessors |
|
// These private accessors are used to avoid read only warning on |
|
// visual studio, also you can add #pragma warning disable IDE0044 |
|
// and #pragma warning restore RECS0029 before and after the |
|
// declaration of the variable. |
|
|
|
// Flag to know if the home button must be hide. |
|
private bool HideHomeButton { |
|
get { return _hideHomeButton; } |
|
set { _hideHomeButton = value; } |
|
} |
|
|
|
// Flag to know if the home button must be attenueated. |
|
private bool AttenuateHomeButton { |
|
get { return _attenuateHomeButton; } |
|
set { _attenuateHomeButton = value; } |
|
} |
|
|
|
// Flag to create a padding. |
|
private bool UsePadding { |
|
get { return _usePadding; } |
|
set { _usePadding = value; } |
|
} |
|
|
|
// Bezel Sprite for padding. |
|
private Sprite BezelSprite { |
|
get { return _bezelSprite; } |
|
set { _bezelSprite = value; } |
|
} |
|
|
|
#endregion |
|
|
|
|
|
|
|
#region MonoBehaviour Overrides |
|
|
|
// Called on awake. |
|
private void Awake () { |
|
|
|
Instance = this; |
|
|
|
#if UNITY_IOS |
|
|
|
Hide = HideHomeButton; |
|
Attenuate = AttenuateHomeButton; |
|
|
|
if (UsePadding) { |
|
|
|
// Padding Creation. Container for current canvas content. |
|
Padding = new GameObject ( |
|
name: "Padding", |
|
components: typeof (RectTransform) |
|
); |
|
Padding.transform.SetParent (transform, false); |
|
|
|
RectTransform paddingTransform = Padding.GetComponent<RectTransform> (); |
|
paddingTransform.anchorMin = Vector2.zero; |
|
paddingTransform.anchorMax = Vector2.one; |
|
paddingTransform.offsetMax = Vector2.zero; |
|
paddingTransform.offsetMin = Vector2.up * 40f; |
|
|
|
List<Transform> childs = new List<Transform> (); |
|
for (int i = 0; i < transform.childCount; i++) |
|
childs.Add (transform.GetChild (i)); |
|
childs.ForEach (t => t.SetParent (Padding.transform, false)); |
|
|
|
|
|
// Padding Background. Avoid to show camera background. |
|
Background = new GameObject ( |
|
name: "Padding Background", |
|
components: typeof (RectTransform) |
|
); |
|
Background.transform.SetParent (transform, false); |
|
|
|
RectTransform backgroundTransform = Background.GetComponent<RectTransform> (); |
|
backgroundTransform.offsetMin = Vector2.zero; |
|
backgroundTransform.offsetMax = Vector2.zero; |
|
backgroundTransform.anchorMin = Vector2.zero; |
|
backgroundTransform.anchorMax = Vector2.right; |
|
backgroundTransform.sizeDelta = Vector2.up * 40f; |
|
backgroundTransform.pivot = new Vector2 (0.5f, 0f); |
|
|
|
Image backgroundImage = Background.AddComponent<Image> (); |
|
backgroundImage.color = Color.black; |
|
|
|
// Bezel to simulate the bezel iPad. |
|
Bezel = new GameObject ( |
|
name: "Bezel", |
|
components: typeof (RectTransform) |
|
); |
|
Bezel.transform.SetParent (transform, false); |
|
|
|
RectTransform bezelTransform = Bezel.GetComponent<RectTransform> (); |
|
bezelTransform.anchorMin = Vector2.zero; |
|
bezelTransform.anchorMax = Vector2.one; |
|
bezelTransform.offsetMax = Vector2.zero; |
|
bezelTransform.offsetMin = Vector2.up * 40f; |
|
|
|
Image bezelImage = Bezel.AddComponent<Image> (); |
|
bezelImage.sprite = BezelSprite; |
|
bezelImage.color = Color.black; |
|
bezelImage.type = Image.Type.Sliced; |
|
bezelImage.raycastTarget = false; |
|
|
|
} |
|
|
|
#endif |
|
} |
|
|
|
// Called on destroy. Claen the Instance reference. |
|
private void OnDestroy () { |
|
if (Instance == this) |
|
Instance = null; |
|
} |
|
|
|
#endregion |
|
} |