温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Unity编辑器下如何重启

发布时间:2021-08-10 14:47:46 来源:亿速云 阅读:203 作者:小新 栏目:编程语言

这篇文章主要介绍Unity编辑器下如何重启,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

Unity编辑器下重启的方法

我们项目AssetBundle打包走的是全自动化流程,打包之前要进行各种资源检测,如果检测顺利通过,则进入打包,否则提示错误资源名称及路径,打包中断!有时候即使资源检测通过也会打包崩溃,初步断定是Unity的内存爆了,因为Unity在编辑器下打开工程中的资源不会释放掉,所以内存一直在占用,打包时要进行一系列资源依赖分析,我们也知道,如果资源量非常大时候,Unity要保存资源依赖的堆栈,所以会有内存崩掉的风险,所以我就想着,打包之前重启下Unity,让Unity释放掉一些没用的内存。完成这个工作,有以下几个步骤:

1.获取Unity的安装路径,实现方法有两种:

  方法一:简单粗暴,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
  }

 第二步:创建一个新的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();公司电脑通过注册表是可以的,家里电脑不行,你们可以用这个函数试下,实在不行先写上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();
  }
}

运行结果:

Unity编辑器下如何重启

以上是“Unity编辑器下如何重启”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI