Skip to content

Instantly share code, notes, and snippets.

@accidentalrebel
Created January 22, 2018 23:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save accidentalrebel/69ac38f729e72c170a8d091b4daaec52 to your computer and use it in GitHub Desktop.
Save accidentalrebel/69ac38f729e72c170a8d091b4daaec52 to your computer and use it in GitHub Desktop.
Opens a text-based file from Unity in Emacs at the correct line.
using UnityEngine;
using UnityEditor;
using System;
using System.IO;
using UnityEditor.Callbacks;
[InitializeOnLoad]
public class FileOpener : MonoBehaviour {
static string _emacsPath = "/Applications/Emacs.app/Contents/MacOS/bin/emacsclient";
const string _fileExtensions = ".cs, .txt, .js, .javascript, .json, .html, .shader, .template";
[OnOpenAssetAttribute()]
public static bool OnOpenedAsset(int instanceID, int line)
{
UnityEngine.Object selected = EditorUtility.InstanceIDToObject(instanceID);
string selectedFilePath = AssetDatabase.GetAssetPath(selected);
string selectedFileExt = Path.GetExtension(selectedFilePath);
if (selectedFileExt == null) {
selectedFileExt = String.Empty;
}
if (!String.IsNullOrEmpty(selectedFileExt)) {
selectedFileExt = selectedFileExt.ToLower();
}
if (selected.GetType().ToString() == "UnityEditor.MonoScript" ||
selected.GetType().ToString() == "UnityEngine.Shader" ||
_fileExtensions.IndexOf(selectedFileExt, StringComparison.OrdinalIgnoreCase) >= 0 ) {
string ProjectPath = System.IO.Path.GetDirectoryName(UnityEngine.Application.dataPath);
string completeFilepath = ProjectPath + Path.DirectorySeparatorChar + AssetDatabase.GetAssetPath(selected);
string args = null;
if (line == -1) {
args = completeFilepath;
}
else {
args = "-n +" + line.ToString() + " " + completeFilepath;
}
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = _emacsPath;
proc.StartInfo.Arguments = args;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.Start();
Debug.Log("Opened in Emacs.");
return true;
}
Debug.LogWarning("Letting Unity handle opening of script.");
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment