修改Unity中Lua文件的默认打开程序

项目中引用了XLua,而Lua文件又是以txt文件结尾的,当修改系统的扩展脚本编辑器为vs后双击lua文件(xx.txt)默认也使用vs打开了,无提示的黑白文本编辑悲伤

 

image

 

昨办?

 

….

 

后来看到网上有写Unity的插件,想着应该也能判断后缀名然后调用指定的编辑器,果然可以。直接贴代码了(C#文件,只要建一个名为Editor的目录 —— 与路径无关,扔进去就行,Unity会自动编译的)

using UnityEngine;
using UnityEditor;
using System;

public class LuaTxtEditor
{

    //http://www.xuanyusong.com/archives/3702 

    [UnityEditor.Callbacks.OnOpenAssetAttribute(1)]
    public static bool step1(int instanceID, int line)
    {
        //string name = EditorUtility.InstanceIDToObject(instanceID).name;
        //Debug.Log("Open Asset step: 1 (" + name + ")");

        return false;
    }

    [UnityEditor.Callbacks.OnOpenAssetAttribute(2)]
    public static bool step2(int instanceID, int line)
    {
        string strFilePath = AssetDatabase.GetAssetPath(EditorUtility.InstanceIDToObject(instanceID));
        string strFileName = Application.dataPath + "/" + strFilePath.Replace("Assets/", "");

        if (strFileName.EndsWith(".txt"))
        {
            string strZBStudioPath = Environment.GetEnvironmentVariable("ZEROBRANESTUDIO_PATH");

            if (strZBStudioPath != null && strZBStudioPath.Length > 0)
            {
                System.Diagnostics.Process process = new System.Diagnostics.Process();
                System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
                startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                startInfo.FileName = strZBStudioPath + (strZBStudioPath.EndsWith("/") ? "" : "/") +  "zbstudio.exe";
                startInfo.Arguments = strFileName;
                process.StartInfo = startInfo;
                process.Start();

                //Debug.Log(startInfo.FileName + " \t " + startInfo.Arguments);

                return true;
            }
            else
            {
                Debug.Log("Not Found Enviroment Variable 'ZEROBRANESTUDIO_PATH'.");

                return false;
            }            
        }

        //string name = EditorUtility.InstanceIDToObject(instanceID).name;
        //Debug.Log("Open Asset step: 1 (" + name + ")");

        return false;
    }

}

 

上面使用ZeroBraneStudio来打开lua文件,你也可以修改为自己常用的编辑器,上面使用了环境变量获取程序的安装路径。

 

 

另外介绍几个小技巧:

1、shift + space(空格键),打以让鼠标所停留的视窗最大化

2、Unity在运行模式(Play)下所做的修改是不保存的,为了防止这种误操作,可以修改运行模式下的颜色;

菜单Edit –> Preferences –> Colors –> playmode tint。

image

 

更多的技巧,可以参考知乎:Unity游戏开发有哪些让你拍案叫绝的技巧?

posted @ 2017-08-16 16:33  meteoric_cry  阅读(981)  评论(0编辑  收藏  举报