Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

愚蠢的打开文件方式 #38

Open
0x2CA opened this issue Jun 28, 2021 · 1 comment
Open

愚蠢的打开文件方式 #38

0x2CA opened this issue Jun 28, 2021 · 1 comment

Comments

@0x2CA
Copy link

0x2CA commented Jun 28, 2021

看代码使用填写编辑器判断路径是否有对应关键字形式打开文件是非常愚蠢的行为。

   static void OpenFileAtLineExternal(string filePath, int line)
        {
            string editorPath = LuaDeepProfilerSetting.Instance.luaIDE;
            // 没有path就弹出面板设置
            if (string.IsNullOrEmpty(editorPath) || !File.Exists(editorPath))
            {
                SetExternalEditorPath();
            }
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.StartInfo.FileName = editorPath;
            string procArgument = "";

            if (editorPath.IndexOf("Code") != -1)
            {
                procArgument = string.Format("-g {0}:{1}", filePath, line);
            }
            else if (editorPath.IndexOf("idea") != -1 || editorPath.IndexOf("clion") != -1 || editorPath.IndexOf("rider") != -1)
            {
                procArgument = string.Format("--line {0} {1}", line, filePath);
                Debug.Log(procArgument);
            }
            else
            {
                procArgument = string.Format("{0}:{1}", filePath, line);
            }
            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.CreateNoWindow = true;
            proc.StartInfo.Arguments = procArgument;
            proc.Start();

            if (editorPath.IndexOf("idea") != -1 || editorPath.IndexOf("clion") != -1 || editorPath.IndexOf("rider") != -1)
            {
#if UNITY_EDITOR_WIN
                IntPtr hwd = FindWindow("SunAwtFrame", null);
                if (hwd != IntPtr.Zero)
                {
                    ShowWindow(hwd, 3);
                }
#endif
            }
        }

事实上Unity内置有对应接口打开文件UnityEditorInternal.InternalEditorUtility.OpenFileAtLineExternal,只是如果没有设置正确的运行后缀打开文件行数不对,只需要加入后缀即可,比如

        if (!EditorPrefs.GetString("vscode_userExtensions").Contains("lua"))
        {
            string vscode_userExtensions = EditorPrefs.GetString("vscode_userExtensions");
            HashSet<string> erxtensions = new HashSet<string>(vscode_userExtensions.Split(';'));
            erxtensions.Add("lua");
            EditorPrefs.SetString("vscode_userExtensions", string.Join(";", erxtensions));
        }
@lixiandea
Copy link

你说的这个方案不太行,很多时候unity工程用的编辑器和lua文件夹下的编辑器不是同一个,用vscode_userExtensions 需要将c#工程也有vscode打开,我个人觉得还是这种方式好用,我改成了使用EditorPrefs保存可执行路径的方式:

public static void SetExternalEditorPath()
  {
      string path = EditorPrefs.GetString(LUA_IDE_KEY);
      path = EditorUtility.OpenFilePanel("Select Lua IDE", path, "");
      if (path != "")
      {
          EditorPrefs.SetString(LUA_IDE_KEY, path);
          Debug.Log("Set Lua IDE Path: " + path);
      }
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants