Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SafePadding behaving incorrectly on non-mobile platforms #50

Open
doctorpangloss opened this issue Mar 16, 2020 · 0 comments
Open

SafePadding behaving incorrectly on non-mobile platforms #50

doctorpangloss opened this issue Mar 16, 2020 · 0 comments

Comments

@doctorpangloss
Copy link

SafePadding renders incorrect padding on non-mobile platforms in 2019.3 and Device Simulator 2.+

As a workaround, I use the following scripts:

/// <summary>
    /// Provides a replacement for <see cref="Application"/> that better reports the platform currently being used.
    /// </summary>
    public static class EditorCompatibleApplication
    {
        public static RuntimePlatform platform
        {
            get
            {
#if UNITY_ANDROID
                return RuntimePlatform.Android;
#elif UNITY_IOS
                return RuntimePlatform.IPhonePlayer;
#elif UNITY_STANDALONE_OSX
                return RuntimePlatform.OSXPlayer;
#elif UNITY_STANDALONE_WIN
                return RuntimePlatform.WindowsPlayer;
#elif UNITY_WEBGL
                return RuntimePlatform.WebGLPlayer;
#endif
            }
        }

        public static bool isLandscape => Camera.main.aspect > 1f;

        public static bool isMobilePlatform
        {
            get
            {
#if UNITY_EDITOR
                // Game is being played in the editor and the selected BuildTarget is either Android or iOS
                if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.Android ||
                    EditorUserBuildSettings.activeBuildTarget == BuildTarget.iOS)
                {
                    return true;
                }
#endif
                // Game is being played on an Android or iOS device
                if (platform == RuntimePlatform.Android ||
                    platform == RuntimePlatform.IPhonePlayer)
                {
                    return true;
                }
                // Game is being played on something other then an Android or iOS device
                else
                {
                    return Application.isMobilePlatform;
                }
            }
        }
    }

/// <summary>
    /// Disables the safe area when in WebGL, since it appears to misread the device stats in 2019.3.
    /// </summary>
    public sealed class SafeAreaInfluenceFix : UIBehaviour
    {
        [SerializeField] private SafePadding m_Target;

        private void Start()
        {
            if (!EditorCompatibleApplication.isMobilePlatform)
            {
                StartCoroutine(SetInfluence(0f));
            }
            else
            {
                StartCoroutine(SetInfluence(1f));
            }
        }

        private IEnumerator SetInfluence(float value)
        {
            m_Target.influence = value;
            yield return new WaitForEndOfFrame();
            m_Target.enabled = false;
            yield return new WaitForEndOfFrame();
            m_Target.enabled = true;
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant