【总】Unity Mono 版本Unity 6000.3.19f1Debug 扩展using System; using System.IO; using System.Text; using System.Threading.Tasks; using UnityEngine; public class DebugExtend { /// summary /// 是否记录日志信息 /// /summary public static bool IsRecordLog true; /// summary /// 打印日志信息 /// /summary /// param namemessage日志内容/param public static async void Log(object message) { Debug.Log(message); await RecordLogInfo(message, DebugLogType.Log); } /// summary /// 打印日志警告信息 /// /summary /// param namemessage日志内容/param public static async void LogWarning(object message) { Debug.LogWarning(message); await RecordLogInfo(message, DebugLogType.Warning); } /// summary /// 打印日志错误信息 /// /summary /// param namemessage日志内容/param public static async void LogError(object message) { Debug.LogError(message); await RecordLogInfo(message, DebugLogType.Error); } /// summary /// 打印日志信息 /// /summary /// param namemessage日志内容/param /// param namelogType打印日志类型/param public static void Log(object message, DebugLogType logType) { switch (logType) { case DebugLogType.Log: Log(message); break; case DebugLogType.Error: LogError(message); break; case DebugLogType.Warning: LogWarning(message); break; } } /// summary /// 记录日志信息 /// /summary /// param namemessage日志内容/param /// param namelogType打印日志类型/param /// returns/returns private static async Task RecordLogInfo(object message, DebugLogType logType) { if (!IsRecordLog) return; // 若关闭日志输出则不再进行记录 if (!Debug.unityLogger.logEnabled) return; string filePath ; string fileName ${logType}.txt; string newLine Environment.NewLine; // 换行符处理保证跨平台兼容性 string time_stamp ${DateTime.Now:yyyy-MM-dd HH:mm:ss}; // 当前时间戳 string appendContent $[{logType.ToString().ToLower()}] {time_stamp}{newLine}{message}{newLine}{newLine}; #if UNITY_EDITOR filePath Path.Combine(Application.dataPath, Editor, fileName); #elif UNITY_ANDROID || UNITY_IOS || UNITY_WEBGL filePath Path.Combine(Application.persistentDataPath, fileName); #else filePath Path.Combine(Environment.CurrentDirectory, fileName); #endif #if UNITY_WEBGL !UNITY_EDITOR File.AppendAllText(filePath, appendContent, Encoding.UTF8); if (File.Exists(filePath)) Debug.Log($Save content\n{File.ReadAllText(filePath)}); await Task.Delay(1); #else Debug.Log(Save file path : filePath); // 确保目录存在 Directory.CreateDirectory(Path.GetDirectoryName(filePath)); // 异步追加文件内容避免主线程卡顿 await Task.Run(() { try { #if !UNITY_ANDROID !UNITY_IOS //// 方法一true表示追加模式 //using (StreamWriter writer new StreamWriter(filePath, true)) //{ // writer.Write(appendContent); //} #endif // 方法二追加文本如果文件不存在会自动创建指定编码格式 File.AppendAllText(filePath, appendContent, Encoding.UTF8); } catch (Exception e) { Debug.LogError($Unable to write log content{e.Message}); } }); #endif #if UNITY_EDITOR // 刷新资源数据刷新日志信息 UnityEditor.AssetDatabase.Refresh(); #endif } } public enum DebugLogType { Log, Warning, Error }获取本地IPusing System.Net.NetworkInformation; using System.Net.Sockets; public class MonoTools { public static string GetIP(AddressFAM addressFAM) { // https://blog.csdn.net/sinat_39291423/article/details/100733882 if (addressFAM AddressFAM.IPv6 !Socket.OSSupportsIPv6) { return null; } string output ; foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces()) { #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN NetworkInterfaceType _type1 NetworkInterfaceType.Wireless80211; NetworkInterfaceType _type2 NetworkInterfaceType.Ethernet; if ((item.NetworkInterfaceType _type1 || item.NetworkInterfaceType _type2) item.OperationalStatus OperationalStatus.Up) #endif { foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses) { if (addressFAM AddressFAM.IPv4) { if (ip.Address.AddressFamily AddressFamily.InterNetwork) { output ip.Address.ToString(); } } else if (addressFAM AddressFAM.IPv6) { if (ip.Address.AddressFamily AddressFamily.InterNetworkV6) { output ip.Address.ToString(); } } } } } return output; } public enum AddressFAM { IPv4, IPv6 } }获取文件夹地址using UnityEngine; public class MonoTools { public static string GetPath(ProjectPath projectPath) { string path ; switch (projectPath) { case ProjectPath.Assets: path Application.dataPath; break; case ProjectPath.Data: path Application.persistentDataPath; break; case ProjectPath.Cache: path Application.temporaryCachePath; break; case ProjectPath.Project: path System.Environment.CurrentDirectory; break; case ProjectPath.StreamingAssets: path Application.streamingAssetsPath; break; } return path; } public enum ProjectPath { Project, Assets, Data, Cache, StreamingAssets } }关闭程序二次确定using System; using System.Runtime.InteropServices; using UnityEngine; public class ApplicationQuit : MonoBehaviour { #region variable /// summary 用于告诉Unity是否要退出 /summary private bool m_IsWantsToQuit false; /// summary 判断是否点击了退出程序按钮 /summary private bool m_IsClickCloseBtn false; /// summary 用于执行退出前的委托 /summary private Action m_WantsToQuitAction; #endregion [DllImport(User32.dll, SetLastError true, ThrowOnUnmappableChar true, CharSet CharSet.Auto)] public static extern int MessageBox(IntPtr handle, String message, String title, int type); private void Awake() { m_IsWantsToQuit false; m_IsClickCloseBtn false; Application.wantsToQuit WantsToQuit; } private void Start() { m_WantsToQuitAction () ShowMessageBox(); } private bool WantsToQuit() { if (!m_IsWantsToQuit !m_IsClickCloseBtn) m_WantsToQuitAction?.Invoke(); return m_IsWantsToQuit; } private void ShowMessageBox() { #if !UNITY_EDITOR m_IsClickCloseBtn true; int messageIndex MessageBox(IntPtr.Zero, 确定要退出程序吗, 提示, 1); if (messageIndex 1) { m_IsWantsToQuit true; Application.Quit(); // 杀死当前进程强制关闭 System.Diagnostics.Process.GetCurrentProcess().Kill(); } else { m_IsClickCloseBtn false; } #endif } }跳过启动Logo动画using UnityEngine; using UnityEngine.Rendering; using UnityEngine.Scripting; /// summary /// 项目运行初始化 /// Preserve此特性用于防止在打包的时候这个脚本没有被打包进程序 /// /summary [Preserve] public class RuntimeInitialize { [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSplashScreen)] private static void BeforeSplashScreen() { #if UNITY_WEBGL Application.focusChanged Application_FocusChanged; #else // 跳过Unity的启动Logo System.Threading.Tasks.Task.Run(AsyncSkip); //Task.Run(() SplashScreen.Stop(SplashScreen.StopBehavior.StopImmediate)); #endif } [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] private static void BeforeSceneLoad() { Debug.Log(加载开始); } [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)] private static void AfterSceneLoad() { Debug.Log(加载完成); } #if UNITY_WEBGL private static void Application_FocusChanged(bool obj) { Application.focusChanged - Application_FocusChanged; SplashScreen.Stop(SplashScreen.StopBehavior.StopImmediate); } #else private static void AsyncSkip() { SplashScreen.Stop(SplashScreen.StopBehavior.StopImmediate); } #endif }

相关新闻

最新新闻

Tiva C系列ADC采样序列控制寄存器ADCSSCTL0配置详解与应用实战

Tiva C系列ADC采样序列控制寄存器ADCSSCTL0配置详解与应用实战

1. ADC采样序列控制寄存器ADCSSCTL0深度解析在嵌入式系统开发中,模数转换器(ADC)是实现模拟信号数字化的核心外设。其工作原理基于采样、保持、量化和编码,将连续变化的模拟量转换为离散的数字量。ADC的技术价值在于为微控制器提供…

2026/7/22 10:52:26
MibSPI多缓冲串行接口:解放CPU,实现高效嵌入式数据通信

MibSPI多缓冲串行接口:解放CPU,实现高效嵌入式数据通信

1. 项目概述:为什么我们需要MibSPI?在嵌入式开发领域,串行外设接口(SPI)就像设备之间对话的“方言”,简单直接,效率也高。无论是读取传感器数据、驱动显示屏,还是与外部存储器通信&a…

2026/7/22 10:52:26
深入解析TI MCU的IOMM模块:ADC触发、ePWM同步与安全配置实战

深入解析TI MCU的IOMM模块:ADC触发、ePWM同步与安全配置实战

1. 项目概述:IOMM在嵌入式系统中的核心角色在嵌入式微控制器(MCU)的世界里,引脚资源永远是稀缺的。一颗芯片集成了ADC、PWM、定时器、通信接口等数十种外设,但物理引脚数量却受限于封装尺寸和成本。如何让有限的引脚承…

2026/7/22 10:52:26
深入解析Tiva™ TM4C129以太网MAC寄存器:中断、地址过滤与MMC实战

深入解析Tiva™ TM4C129以太网MAC寄存器:中断、地址过滤与MMC实战

1. 项目概述与核心价值在嵌入式系统开发,尤其是涉及网络通信的工控、车载或物联网设备中,以太网MAC(媒体访问控制)层的配置往往是驱动开发中最具挑战性的一环。很多开发者习惯于依赖现成的协议栈或驱动库,对底层寄存器…

2026/7/22 10:52:26
提升效率的实用网站推荐与使用技巧

提升效率的实用网站推荐与使用技巧

1. 项目概述作为一名长期活跃在互联网行业的从业者,我经常需要收集和整理各类实用网站资源。这些网站就像工具箱里的各种工具,每个都有其独特的用途和价值。今天要分享的这个项目,是我在日常工作中积累的第二批实用网站清单(继之前…

2026/7/22 10:52:26
LSTM参数详解:从input_size到bidirectional的完整配置指南

LSTM参数详解:从input_size到bidirectional的完整配置指南

在深度学习项目中处理序列数据时,LSTM(长短期记忆网络)是绕不开的核心组件。很多开发者在初次使用PyTorch或TensorFlow中的LSTM API时,会被一堆参数搞得一头雾水——input_size、hidden_size、num_layers、batch_first等参数到底应…

2026/7/22 10:47:26

月新闻