If you are like me and can’t simple get one day without crashing the editor, having to delete the UnityLockFile everytime to reopen unity gets really boring. Then this script is for you:
Put this script inside any Editor folder inside our project.
This script works by creating a bash script that checks from time to time if Unity is still open and if not it will try to delete that pesky UnityLockFile;
using System.IO;
using System.Linq;
using System.Diagnostics;
using UnityEngine;
using UnityEditor;
using System;
namespace Linux.Editor
{
[InitializeOnLoad]
static class LinuxUnlockProject
{
private static string _projectPath = null;
/// <summary>
/// Thread safe project path
/// </summary>
public static string projectPath
{
get
{
if (string.IsNullOrEmpty(_projectPath))
{
var path = System.Reflection.Assembly.GetExecutingAssembly().Location;
_projectPath = path.Substring(0, path.IndexOf("Library")).Replace("\\", "/");
}
return _projectPath;
}
}
private static readonly string _script = "#!/bin/bash\n" +
"\n" +
"wd=`pwd`\n" +
"for pid in `pidof -x Unlock.sh`; do\n" +
"\tif [[ $ = $pid ]]; then continue; fi\n" +
"\tdir=`pwdx $pid | cut -d' ' -f2-`\n" +
"\tif [[ $wd = $dir ]]; then\n" +
"\t\techo \"Already running\"\n" +
"\t\texit 0\n" +
"\tfi\n" +
"done\n" +
"\n" +
"while true\n" +
"do\n" +
"\tif ps -p $1 > /dev/null\n" +
"\tthen\n" +
"\t\tsleep 0.250s\n" +
"\t\tcontinue\n" +
"\tfi\n" +
"\techo \"Removing UnityLock\"\n" +
"\trm Temp/UnityLockfile\n" +
"\tbreak\n" +
"done\n";
// Force reload
static LinuxUnlockProject()
{
var file = "Unlock.sh";
var path = projectPath + "/" + file;
if (!File.Exists(path))
{
File.WriteAllText(path, _script);
Run("chmod", "+x " + file);
}
var pid = Process.GetCurrentProcess().Id;
//UnityEngine.Debug.Log(pid);
RunBackground("./" + file, pid.ToString());
}
private static void RunBackground(string program, string args = null)
{
var p = Create(program, args);
p.Start();
}
private static void Run(string program, string args = null)
{
var p = Create(program, args);
p.Start();
p.WaitForExit();
}
private static Process Create(string program, string args = null)
{
var p = new Process();
p.StartInfo.WorkingDirectory = projectPath;
p.StartInfo.UseShellExecute = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.FileName = program;
p.StartInfo.Arguments = args;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
return p;
}
}
}