在上一篇文章中,我们完成了 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
更多内容
-
QFramework 地址:https://github.com/liangxiegame/QFramework
-
QQ 交流群:623597263
-
Unity 进阶小班:
- 主要训练内容:
- 框架搭建训练(第一年)
- 跟着案例学 Shader(第一年)
- 副业的孵化(第二年、第三年)
- 权益、授课形式等具体详情请查看《小班产品手册》:https://liangxiegame.com/master/intro
- 主要训练内容:
-
关注公众号:liangxiegame 获取第一时间更新通知及更多的免费内容。