美文网首页unity3D技术分享
Unity 游戏框架搭建 2019 (十五) 第九个示例(二)

Unity 游戏框架搭建 2019 (十五) 第九个示例(二)

作者: 凉鞋的笔记 | 来源:发表于2020-03-31 12:14 被阅读0次

在上一篇文章中,我们完成了 Pad 设备分辨率的判断。我们今天把剩下的其他分辨率都搞定。

16:9 手机分辨率

大部分手机是 16 : 9 的。
所以代码如下。

        /// <summary>
        /// 是否是手机分辨率 16:9
        /// </summary>
        /// <returns></returns>
        public static bool IsPhoneResolution()
        {
            var aspect = GetAspectRatio();
            return aspect > 16.0f / 9 - 0.05 && aspect < 16.0f / 9 + 0.05;
        }

3:2 (iPhone 4s)

        /// <summary>
        /// 是否是手机分辨率 3:2 3 / 2 = 1.5
        /// </summary>
        /// <returns></returns>
        public static bool IsPhone15Resolution()
        {
            var aspect = GetAspectRatio();
            return aspect > 3.0f / 2 - 0.05 && aspect < 3.0f / 2 + 0.05;
        }

2436:1125 (iPhone X)

        /// <summary>
        /// 是否是iPhone X 分辨率 2436:1125
        /// </summary>
        /// <returns></returns>
        public static bool IsiPhoneXResolution()
        {
            var aspect = GetAspectRatio();
            return aspect > 2436.0f / 1125 - 0.05 && aspect < 2436.0f / 1125 + 0.05;
        }

其他

相信扩展机型的规律大家应该掌握了,这里就不多说了。

完整示例代码如下:

using UnityEngine;

#if UNITY_EDITOR
using UnityEditor;
#endif

namespace QFramework
{
    public class ResolutionCheck
    {
#if UNITY_EDITOR
        [MenuItem("QFramework/9.屏幕宽高比判断")]
#endif
        private static void MenuClicked()
        {
            Debug.Log(IsPadResolution() ? "是 Pad 分辨率" : "不是 Pad 分辨率");
            Debug.Log(IsPhoneResolution() ? "是 Phone 分辨率" : "不是 Phone 分辨率");
            Debug.Log(IsiPhoneXResolution() ? "是 iPhone X 分辨率" : "不是 iPhone X 分辨率");
        }

        /// <summary>
        /// 获取屏幕宽高比
        /// </summary>
        /// <returns></returns>
        public static float GetAspectRatio()
        {
            return Screen.width > Screen.height ? (float) Screen.width / Screen.height : (float) Screen.height / Screen.width;
        }

        /// <summary>
        /// 是否是 Pad 分辨率 4 : 3 
        /// </summary>
        /// <returns></returns>
        public static bool IsPadResolution()
        {
            var aspect = GetAspectRatio();
            return aspect > 4.0f / 3 - 0.05 && aspect < 4.0f / 3 + 0.05;
        }
        
        /// <summary>
        /// 是否是手机分辨率 16:9
        /// </summary>
        /// <returns></returns>
        public static bool IsPhoneResolution()
        {
            var aspect = GetAspectRatio();
            return aspect > 16.0f / 9 - 0.05 && aspect < 16.0f / 9 + 0.05;
        }
        
        /// <summary>
        /// 是否是iPhone X 分辨率 2436:1125
        /// </summary>
        /// <returns></returns>
        public static bool IsiPhoneXResolution()
        {
            var aspect = GetAspectRatio();
            return aspect > 2436.0f / 1125 - 0.05 && aspect < 2436.0f / 1125 + 0.05;
        }
    }
}

到此呢,我们可以进行一次导出了。

今天内容就这些。

转载请注明地址:凉鞋的笔记:liangxiegame.com

更多内容

相关文章

网友评论

    本文标题:Unity 游戏框架搭建 2019 (十五) 第九个示例(二)

    本文链接:https://www.haomeiwen.com/subject/kxlwuhtx.html