用SublimeText当Unity Shader的编辑器

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

 

因此试着在网上找找,有没有相似的插件或者编辑器,被我找到了,也基本符合个人要求。github

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

这就分为二个功能:函数

一、双击shader文件打开Sublime Text;spa

二、使用Sublime Text来编辑shader;.net

 

第一个功能,比较简单,在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 (以下图所示)code

image

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

 

 

第二个功能,安装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

相关文章
相关标签/搜索