Unity 游戏框架搭建 2019 (二十七、二十八)弃用的代码警告解决&弃用的代码删除 (2)

对方法的名称,进行全局文本搜索,一般在 IDE 里快捷键是 cmd/ctrl + shift + F,如果不是可以自己搜一下。经过步骤 1 之后,在执行本操作,结果很可能是在宏定义里的引用,这时候就需要用肉眼去检查了,看看是不是最新的方法,如果不是就更新。

每一个弃用的方法我们都要这样一个一个去解决。

解决按之后代码如下:

#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; TransformSimplify.SetLocalPosX(transform, 5.0f); TransformSimplify.SetLocalPosY(transform, 5.0f); TransformSimplify.SetLocalPosZ(transform, 5.0f); } } }

第十个示例解决了。

示例八

使用以上的步骤,整理后的示例代码如下:

#if UNITY_EDITOR using UnityEditor; #endif using UnityEngine; using System; using System.IO; namespace QFramework { public class CommonUtil { public static void CopyText(string text) { GUIUtility.systemCopyBuffer = text; } } public class Exporter { public static string GenerateUnityPackageName() { return "QFramework_" + DateTime.Now.ToString("yyyyMMdd_hh"); } } public class EditorUtil { #if UNITY_EDITOR public static void CallMenuItem(string menuPath) { EditorApplication.ExecuteMenuItem(menuPath); } public static void OpenInFolder(string folderPath) { Application.OpenURL("file:///" + folderPath); } public static void ExportPackage(string assetPathName,string fileName) { AssetDatabase.ExportPackage(assetPathName, fileName, ExportPackageOptions.Recurse); } #endif } public class PreviousFunctions : MonoBehaviour { #if UNITY_EDITOR [MenuItem("QFramework/8.总结之前的方法/1.获取文件名")] private static void MenuClicked() { Debug.Log(Exporter.GenerateUnityPackageName()); } [MenuItem("QFramework/8.总结之前的方法/2.复制文本到剪切板")] private static void MenuClicked2() { CommonUtil.CopyText("要复制的关键字"); } [MenuItem("QFramework/8.总结之前的方法/3.生成文件名到剪切板")] private static void MenuClicked3() { CommonUtil.CopyText(Exporter.GenerateUnityPackageName()); } [MenuItem("QFramework/8.总结之前的方法/4.导出 UnityPackage")] private static void MenuClicked4() { EditorUtil.ExportPackage("Assets/QFramework",Exporter.GenerateUnityPackageName() + ".unitypackage"); } [MenuItem("QFramework/8.总结之前的方法/5.打开所在文件夹")] private static void MenuClicked5() { EditorUtil.OpenInFolder(Application.dataPath); } [MenuItem("QFramework/8.总结之前的方法/6.MenuItem 复用")] private static void MenuClicked6() { EditorUtil.CallMenuItem("QFramework/8.总结之前的方法/4.导出 UnityPackage"); EditorUtil.OpenInFolder(Path.Combine(Application.dataPath, "../")); } #endif } }

是不是感觉清爽了很多?哈哈哈。

示例十一

整理之后代码如下:

using System; #if UNITY_EDITOR using UnityEditor; #endif using UnityEngine; namespace QFramework { 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; } } public class TransformIdentity : MonoBehaviour { #if UNITY_EDITOR [MenuItem("QFramework/11.Transform 归一化")] #endif private static void MenuClicked() { var transform = new GameObject("transform").transform; TransformSimplify.Identity(transform); } } } 示例十二

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zywszy.html