用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

相關文章
相關標籤/搜索