Unity編輯器下重啟的方法
Unity編輯器下重啟的方法
我們項目AssetBundle打包走的是全自動化流程,打包之前要進行各種資源檢測,如果檢測順利通過,則進入打包,否則提示錯誤資源名稱及路徑,打包中斷!有時候即使資源檢測通過也會打包崩潰,初步斷定是Unity的內(nèi)存爆了,因為Unity在編輯器下打開工程中的資源不會釋放掉,所以內(nèi)存一直在占用,打包時要進行一系列資源依賴分析,我們也知道,如果資源量非常大時候,Unity要保存資源依賴的堆棧,所以會有內(nèi)存崩掉的風(fēng)險,所以我就想著,打包之前重啟下Unity,讓Unity釋放掉一些沒用的內(nèi)存。完成這個工作,有以下幾個步驟:
1.獲取Unity的安裝路徑,實現(xiàn)方法有兩種:
方法一:簡單粗暴,E:\\Unity 5.5.2\\Unity\\Editor\\Unity.exe,不具有通用性。
方法二:通過注冊包表獲取安裝路徑,獲取操作如下:
private static string GetUnityPath() { #region 通過注冊便獲取Unity安裝路徑 var regKey = @"Unity package file\DefaultIcon"; 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 }
第二步:創(chuàng)建一個新的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.2\\Unity\\Editor\\Unity.exe"; [MenuItem("Tools/ReStartUnity(重啟Unity)")] public static void ReStartUnity() { string unityPath = UnityExePath;// GetUnityPath();公司電腦通過注冊表是可以的,家里電腦不行,你們可以用這個函數(shù)試下,實在不行先寫上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 file\DefaultIcon"; 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(); } }
運行結(jié)果:
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
C++11運算符重載和向量類重載實例詳解(<<,>>,+,-,*等)
這篇文章主要給大家介紹了關(guān)于C++11運算符重載和向量類重載的相關(guān)資料,主要包括<<,>>,+,-,*等,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2021-07-07C++深入探索內(nèi)聯(lián)函數(shù)inline與auto關(guān)鍵字的使用
本篇文章主要包括內(nèi)聯(lián)函數(shù)和auto關(guān)鍵字。其中,內(nèi)斂函數(shù)包括概念,特性等;auto關(guān)鍵字的使用規(guī)則,使用場景等,接下來讓我們深入了解2022-05-05VScode + keil開發(fā)環(huán)境搭建安裝使用過程
這篇文章主要介紹了VScode + keil開發(fā)環(huán)境搭建及安裝使用過程,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-07-07QT基于TCP實現(xiàn)網(wǎng)絡(luò)聊天室程序
這篇文章主要為大家詳細介紹了QT基于TCP實現(xiàn)網(wǎng)絡(luò)聊天室程序,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-08-08