Unity編輯器下重啟的方法
Unity編輯器下重啟的方法
我們項目AssetBundle打包走的是全自動化流程,打包之前要進行各種資源檢測,如果檢測順利通過,則進入打包,否則提示錯誤資源名稱及路徑,打包中斷!有時候即使資源檢測通過也會打包崩潰,初步斷定是Unity的內存爆了,因為Unity在編輯器下打開工程中的資源不會釋放掉,所以內存一直在占用,打包時要進行一系列資源依賴分析,我們也知道,如果資源量非常大時候,Unity要保存資源依賴的堆棧,所以會有內存崩掉的風險,所以我就想著,打包之前重啟下Unity,讓Unity釋放掉一些沒用的內存。完成這個工作,有以下幾個步驟:
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();
}
}
運行結果:
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
您可能感興趣的文章
- 01-10大數(shù)(高精度數(shù))模板(分享)
- 01-10深入解析Linux下\r\n的問題
- 01-10Linux C 獲取進程退出值的實現(xiàn)代碼
- 01-10解析Linux下的時間函數(shù):設置以及獲取時間的方法
- 01-10深入探討linux下進程的最大線程數(shù)、進程最大數(shù)、進程打開的文
- 01-10基于linux下獲取時間函數(shù)的詳解
- 01-10深入sizeof的使用詳解
- 01-10Linux下semop等待信號時出現(xiàn)Interrupted System Call錯誤(EINTR)解決方法
- 01-10深入解析C中的數(shù)值與真假
- 01-10用c語言實現(xiàn)冒泡排序,選擇排序,快速排序


閱讀排行
本欄相關
- 04-02c語言函數(shù)調用后清空內存 c語言調用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調用函數(shù)求fibo C語言調用函數(shù)求
隨機閱讀
- 01-11ajax實現(xiàn)頁面的局部加載
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實例總結
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 04-02jquery與jsp,用jquery
- 01-10使用C語言求解撲克牌的順子及n個骰子


