昨天呢我们把第八个示例整理完了。整理之后学习了类的第一作用:方法的集合,还有 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 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。

类的定义如下:

	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 这个类里。

代码如下:

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

更多内容

04-12 06:53