昨天呢咱們把第八個示例整理完了。整理以後學習了類的第一做用:方法的集合,還有 Obselete 這個 API。而且在進行整理的時候貫徹了咱們新的約定和規則:先確保功能有效,再去作變動和刪除。git
今天咱們在往下接着整理第九個示例github
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; } } }
一樣也是沒啥太大的問題,也留着。dom
#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 name="trans">Trans.</param> public static void Identity(Transform trans) { trans.localPosition = Vector3.zero; trans.localScale = Vector3.one; trans.localRotation = Quaternion.identity; } } }
類的名字沒有問題,可是到這裏筆者發現這個類裏的方法是能夠和 第十個示例: TransformLocalPosImprovements 放在一個類裏的,由於都是對 Transform API 的簡化。
類名字能夠叫 TransformSimplify。ide
類的定義以下:函數
public class TransformSimplify { /// <summary> /// 重置操做 /// </summary> /// <param name="trans">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 的上方。學習
接下來,咱們要把原來的實現方法作中轉,再加上 Obselete 屬性。這步就不展現了,內容與上一篇文章一致,優化
#if UNITY_EDITOR using UnityEditor; #endif using UnityEngine; namespace QFramework { public class PercentFunction : MonoBehaviour { #if UNITY_EDITOR [MenuItem("QFramework/12.機率函數")] #endif private static void MenuClicked() { Debug.Log(Percent(50)); } /// <summary> /// 輸入百分比返回是否命中機率 /// </summary> public static bool Percent(int percent) { return Random.Range (0, 100) <= percent; } } }
這個代碼的類名就有點問題了,叫作 PercentFunction,Percent 呢,是數學計算,因此咱們把這個方法歸到 MathUtil 這個類裏。spa
代碼以下:code
using System; #if UNITY_EDITOR using UnityEditor; #endif using UnityEngine; using Random = UnityEngine.Random; namespace QFramework { public class MathUtil { /// <summary> /// 輸入百分比返回是否命中機率 /// </summary> public static bool Percent(int percent) { return Random.Range (0, 100) <= percent; } } public class PercentFunction : MonoBehaviour { #if UNITY_EDITOR [MenuItem("QFramework/12.機率函數")] #endif private static void MenuClicked() { Debug.Log(MathUtil.Percent(50)); } /// <summary> /// 輸入百分比返回是否命中機率 /// </summary> [Obsolete("方法已棄用,請使用 MathUtil.Percent")] public static bool Percent(int percent) { return MathUtil.Percent(percent); } } }
#if UNITY_EDITOR using UnityEditor; #endif using UnityEngine; namespace QFramework { public class GameObejctActiveImprovements { #if UNITY_EDITOR [MenuItem("QFramework/13.GameObejct 的顯示、隱藏簡化")] #endif private static void MenuClicked() { var gameObject = new GameObject(); Hide(gameObject); } public static void Show(GameObject gameObj) { gameObj.SetActive(true); } public static void Hide(GameObject gameObj) { gameObj.SetActive(false); } } }
與 TransformSimplify 同樣,咱們也定義一個 GameObjectSimplify。
整理後代碼以下:
#if UNITY_EDITOR using UnityEditor; #endif using UnityEngine; namespace QFramework { public class GameObjectSimplify { public static void Show(GameObject gameObj) { gameObj.SetActive(true); } public static void Hide(GameObject gameObj) { gameObj.SetActive(false); } } public class GameObejctActiveImprovements { #if UNITY_EDITOR [MenuItem("QFramework/13.GameObejct 的顯示、隱藏簡化")] #endif private static void MenuClicked() { var gameObject = new GameObject(); GameObjectSimplify.Hide(gameObject); } [Obsolete("方法已啓用,請使用 GameObjectSimplify.Show")] public static void Show(GameObject gameObj) { GameObjectSimplify.Show(gameObj); } [Obsolete("方法已啓用,請使用 GameObjectSimplify.Hide")] public static void Hide(GameObject gameObj) { GameObjectSimplify.Hide(gameObj); } } }
到此呢,咱們十三個示例都作了一輪整理。整理以後又要回頭去解決一些整理過程當中發現的問題。這部份內容咱們在下一篇開始解決。
今天的內容就這些,拜拜~
轉載請註明地址:涼鞋的筆記:liangxiegame.com
QFramework 地址:https://github.com/liangxiegame/QFramework
QQ 交流羣:623597263
Unity 進階小班:
關注公衆號:liangxiegame 獲取第一時間更新通知及更多的免費內容。