Unity编辑器下重启的方法
我们项目AssetBundle打包走的是全自动化流程,打包之前要进行各种资源检测,如果检测顺利通过,则进入打包,否则提示错误资源名称及路径,打包中断!有时候即使资源检测通过也会打包崩溃,初步断定是Unity的内存爆了,因为Unity在编辑器下打开工程中的资源不会释放掉,所以内存一直在占用,打包时要进行一系列资源依赖分析,我们也知道,如果资源量非常大时候,Unity要保存资源依赖的堆栈,所以会有内存崩掉的风险,所以我就想着,打包之前重启下Unity,让Unity释放掉一些没用的内存。完成这个工作,有以下几个步骤:
1.获取Unity的安装路径,实现方法有两种:
方法一:简单粗暴,E:Unity 5.5.2UnityEditorUnity.exe,不具有通用性。
方法二:通过注册包表获取安装路径,获取操作如下:
private static string GetUnityPath() { #region 通过注册便获取Unity安装路径 var regKey = @"Unity package fileDefaultIcon"; RegistryKey registryKey = Registry.ClassesRoot.OpenSubKey(regKey); string pathName = (string)registryKey.GetValue(null); // "(Default)" if (string.IsNullOrEmpty(pathName)) { return null; } int index = pathName.LastIndexOf(","); if (index != -1) { var exepath = pathName.Substring(0, index).Replace(""", string.Empty); var binpath = Path.GetDirectoryName(exepath); var di = new DirectoryInfo(binpath); if (di.Parent != null) { return di.Parent.FullName; } } return null; #endregion }
第二步:创建一个新的Unity进程。
static void StartPeocess(string applicationPath) { Process po = new Process(); po.StartInfo.FileName = applicationPath; po.Start(); }
第三步:杀掉之前旧的进程
string[] args = unityPath.Split(''); Process[] pro = Process.GetProcessesByName(args[args.Length - 1].Split('.')[0]);//Unity foreach (var item in pro) { UnityEngine.Debug.Log(item.MainModule); item.Kill(); }
好了,这样基本上就搞定了!
完整代码:
using Microsoft.Win32; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.IO; using UnityEditor; using UnityEngine; public class ReStartUnityTools : MonoBehaviour { public static string UnityExePath = "E:Unity 5.5.2UnityEditorUnity.exe"; [MenuItem("Tools/ReStartUnity(重启Unity)")] public static void ReStartUnity() { string unityPath = UnityExePath;// GetUnityPath();公司电脑通过注册表是可以的,家里电脑不行,你们可以用这个函数试下,实在不行先写上Unity安装路径吧 StartPeocess(unityPath); string[] args = unityPath.Split(''); Process[] pro = Process.GetProcessesByName(args[args.Length - 1].Split('.')[0]);//Unity foreach (var item in pro) { UnityEngine.Debug.Log(item.MainModule); item.Kill(); } } private static string GetUnityPath() { #region 通过注册便获取Unity安装路径 var regKey = @"Unity package fileDefaultIcon"; RegistryKey registryKey = Registry.ClassesRoot.OpenSubKey(regKey); string pathName = (string)registryKey.GetValue(null); // "(Default)" if (string.IsNullOrEmpty(pathName)) { return null; } int index = pathName.LastIndexOf(","); if (index != -1) { var exepath = pathName.Substring(0, index).Replace(""", string.Empty); var binpath = Path.GetDirectoryName(exepath); // var di = new DirectoryInfo(binpath); if (di.Parent != null) { return di.Parent.FullName; } } return null; #endregion } static void StartPeocess(string applicationPath) { Process po = new Process(); po.StartInfo.FileName = applicationPath; po.Start(); } }
运行结果:
茶杯头甜蜜终章dlc 官方手机版v1.0.0.3
下载火柴人传说暗影格斗内置菜单 最新版v3.0.1
下载荒野乱斗测试服 安卓版v61.10.3
下载荒野乱斗彩虹服 安卓版v61.10.3
下载寒霜启示录 安卓版v1.25.10
寒霜启示录是一款生存模拟游戏,不少玩家可能对于末日都有着自己
末日城堡免广告版 安卓最新版v0.7.1
末日城堡免广告版是一款非常好玩的模拟经营类游戏,内部可以不看
甜蜜人生模拟器 最新版v1.4.5
甜蜜人生模拟器是一款非常好玩的模拟恋爱手游,玩家在这里能够对
武器锻造师内置功能菜单 v10.4
武器锻造师内置菜单版是游戏的破解版本,在该版本中为玩家提供了
开放空间overfield 安卓版v1.0.5
开放空间Overfield是一款箱庭养成经营手游,让你在广阔