Unity设置文件的默认打开方式

在unity开发时,我们通常需要编辑不同的脚本(C#、Lua、XML、Shader...),而unity通常打开所有二级制文本文件都是要么MONO要么VS,下面将以shader为例改变shader文件的默认打开方式为VS Code。


1.首先我我们在editor下添加以下脚本

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class ShaderEditor : MonoBehaviour
{
    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 editorPath = Environment.GetEnvironmentVariable("VSCode_Path");//环境变量名
            if (editorPath != null && editorPath.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 = editorPath + (editorPath.EndsWith("/") ? "" : "/") + "Code.exe";   //你的文件名字
                startInfo.Arguments = "\"" + strFileName + "\"";
                process.StartInfo = startInfo;
                process.Start();
                return true;
            }
            else
            {
                Debug.Log("null environment : VSCode_Path");
                return false;
            }
        }
        return false;
    }
}

                其实就是通过环境变量获取exe的路径,然后修改process的startinfo

2.在环境变量中,将VSCode_Path添加进去(如果不使用环境变量,直接在代码里面写死就行)

3.重启unity,双击shader,以VScode打开了要编辑的shader,另外,用这个vscode扩展敲shader有提示哦


用此方法可以修改任意unity asset面板中资源的打开方式,是不是很方便

  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值