用SublimeText当Unity Shader的编辑器

用Visual Studio写shader实在蛋疼,那可能就会有人要问了,为啥不用插件可视化制作shader呢?因为我是新手,新手还是老老实实敲代码,慢慢来…

 

所以试着在网上找找,有没有类似的插件或者编辑器,被我找到了,也基本符合我的要求。

我想实现的效果如下:双击xxx.shader打开Sublime Text,可在Sublime Text中对常用的函数进行提示,并支持函数的跳转。

这就分为二个功能:

1、双击shader文件打开Sublime Text;

2、使用Sublime Text来编辑shader;

 

第一个功能,比较简单,在Assets目录下建立“Editor”目录(如果已存在,请忽略),放置 ShaderEditor.cs,代码如下:

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)
    {
        return false;
    }

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

        if (strFileName.EndsWith(".shader"))
        {
            string strSublimeTextPath = Environment.GetEnvironmentVariable("SublimeText_Path");
            if (strSublimeTextPath != null && strSublimeTextPath.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 = strSublimeTextPath + (strSublimeTextPath.EndsWith("/") ? "" : "/") + "sublime_text.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 'SublimeText_Path'.");

                return false;
            }            
        }

        return false;
    }

}

然后,你需要设置 Sublime Text的环境变量 SublimeText_Path (如下图所示)

image

此时,双击 shader 可能仍无法直接打开 Sublime Text。你需要关闭 Unity,重启桌面进程 —— 让环境变量生效(任务管理器 kill 掉进程 explorer.exe,然后再新建任务 explorer.exe)

 

 

第二个功能,安装Sublime Text的插件

网上已经有人做好了,直接拿过来用即可。文章链接请参考  http://blog.csdn.net/w88219003/article/details/46682507

  • 下载插件包,前往 https://github.com/cjsjy123/Unity-Shader 直接下载zip文件。
  • 安装 package,Sublime Text菜单 Preferences –> Browse Packages,将zip文件解压到该目录下,重命名文件夹为 Unity-Shader,如下图所

image

  • 修改源码 unityShader.py,原因代码写的有问题,并没有读取 User 的配置,一直报错“U5_Shader_path no set”。我查看源码,发现按作者写的设置了Shader_path没用,原因见代码

image

上面画圈的那里,代码写的有点迷糊,如果使用 U5,为啥还要设置 Shader_path呢?

知道报错的原因,直接改文件:UnityShader.sublime-settings

{
    // must set the path and version  u5 or u4
    "Unity_Version":"U5",
    "Shader_path": "K:/Unity/Editor/Data/CGIncludes",
    "U5_Shader_path":"K:/Unity/Editor/Data/CGIncludes"
}

上面的K:/xxx,改成你自己的路径,然后就可以愉快的用起来了,是支持跳转的。Shader_path是Unity内置的着色器,以.cginc结尾。在Unity Setup Path/Editor/Data/CGIncludes 目录下

image

posted @ 2017-11-22 17:55  meteoric_cry  阅读(7074)  评论(0编辑  收藏  举报