昨天呢我们把第八个示例整理完了。整理之后学习了类的第一作用:方法的集合,还有 Obselete 这个 API。并且在进行整理的时候贯彻了我们新的约定和规则:先确保功能有效,再去做变更和删除。
今天我们在往下接着整理第九个示例
第九个示例 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; } } }我们先看些类的名字,ResolutionCheck,这个没有太大的问题。所以这个示例就留着。
第十个示例 #if UNITY_EDITOR using UnityEditor; #endif using UnityEngine; namespace QFramework { public class TransformLocalPosImprovements { #if UNITY_EDITOR [MenuItem("QFramework/10.Transform 赋值优化")] #endif private static void GenerateUnityPackageName() { var transform = new GameObject("transform").transform; SetLocalPosX(transform, 5.0f); SetLocalPosY(transform, 5.0f); SetLocalPosZ(transform, 5.0f); } public static void SetLocalPosX(Transform transform, float x) { var localPos = transform.localPosition; localPos.x = x; transform.localPosition = localPos; } public static void SetLocalPosY(Transform transform, float y) { var localPos = transform.localPosition; localPos.y = y; transform.localPosition = localPos; } public static void SetLocalPosZ(Transform transform, float z) { var localPos = transform.localPosition; localPos.z = z; transform.localPosition = localPos; } public static void SetLocalPosXY(Transform transform, float x, float y) { var localPos = transform.localPosition; localPos.x = x; localPos.y = y; transform.localPosition = localPos; } public static void SetLocalPosXZ(Transform transform, float x, float z) { var localPos = transform.localPosition; localPos.x = x; localPos.z = z; transform.localPosition = localPos; } public static void SetLocalPosYZ(Transform transform, float y, float z) { var localPos = transform.localPosition; localPos.y = y; localPos.z = z; transform.localPosition = localPos; } } }同样也是没啥太大的问题,也留着。
第十一个示例 #if UNITY_EDITOR using UnityEditor; #endif using UnityEngine; namespace QFramework { public class TransformIdentity : MonoBehaviour { #if UNITY_EDITOR [MenuItem("QFramework/11.Transform 归一化")] #endif private static void MenuClicked() { var transform = new GameObject("transform").transform; Identity(transform); } /// <summary> /// 重置操作 /// </summary> /// <param>Trans.</param> public static void Identity(Transform trans) { trans.localPosition = Vector3.zero; trans.localScale = Vector3.one; trans.localRotation = Quaternion.identity; } } }类的名字没有问题,但是到这里笔者发现这个类里的方法是可以和 第十个示例: TransformLocalPosImprovements 放在一个类里的,因为都是对 Transform API 的简化。
类名字可以叫 TransformSimplify。
类的定义如下:
public class TransformSimplify { /// <summary> /// 重置操作 /// </summary> /// <param>Trans.</param> public static void Identity(Transform trans) { trans.localPosition = Vector3.zero; trans.localScale = Vector3.one; trans.localRotation = Quaternion.identity; } public static void SetLocalPosX(Transform transform, float x) { var localPos = transform.localPosition; localPos.x = x; transform.localPosition = localPos; } public static void SetLocalPosY(Transform transform, float y) { var localPos = transform.localPosition; localPos.y = y; transform.localPosition = localPos; } public static void SetLocalPosZ(Transform transform, float z) { var localPos = transform.localPosition; localPos.z = z; transform.localPosition = localPos; } public static void SetLocalPosXY(Transform transform, float x, float y) { var localPos = transform.localPosition; localPos.x = x; localPos.y = y; transform.localPosition = localPos; } public static void SetLocalPosXZ(Transform transform, float x, float z) { var localPos = transform.localPosition; localPos.x = x; localPos.z = z; transform.localPosition = localPos; } public static void SetLocalPosYZ(Transform transform, float y, float z) { var localPos = transform.localPosition; localPos.y = y; localPos.z = z; transform.localPosition = localPos; } }很简单,就是把上一个示例与这个示例的方法都复制到了这里,类的定义位置在 TransformIdentity 的上方。