forked from cjsjy123/Unity-Shader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunityShader.py
151 lines (129 loc) · 4.12 KB
/
unityShader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sublime
import sublime_plugin
import functools
import os
import json
import re
import subprocess
import sys
import time
import codecs
# from xml.etree import ElementTree as ET
# from urllib import urlopen
try:
import helper
import defaultdefine
except ImportError:
from . import helper
from . import defaultdefine
DEFINITION_LIST=[]
U_Version =4
def init():
global DEFINITION_LIST
global U_Version
checkUnityVersion()
temp1 =json.loads(defaultdefine.inlist)
temp2 =json.loads(defaultdefine.olist)
temp3 =json.loads(defaultdefine.vlist)
temp4 =json.loads(defaultdefine.otherslist)
if(U_Version == 5):
temp5 =json.loads(defaultdefine.ulist)
temp6 =json.loads(defaultdefine.uvlist)
DEFINITION_LIST += temp4
DEFINITION_LIST += temp5
DEFINITION_LIST += temp6
else:
DEFINITION_LIST=temp1 +temp2+temp3 +temp4
def checkUnityVersion():
global U_Version
settings = helper.loadSettings("UnityShader")
SUnity_Version = settings.get("Unity_Version", "")
if( len(SUnity_Version) == 0 ):
U_Version =4
elif "U4" in SUnity_Version:
U_Version =4
elif "U5" in SUnity_Version:
U_Version =5
def checkUnityShaderRoot():
global U_Version
settings = helper.loadSettings("UnityShader")
Shader_path = settings.get("Shader_path", "")
if U_Version== 5:
U5_path =settings.get("U5_Shader_path", "")
if len(U5_path)==0:
sublime.error_message("U5_Shader_path no set")
return False
else:
return U5_path
else:
if len(Shader_path)==0 :
sublime.error_message("Shader_path no set")
return False
return Shader_path
# build file definition when save file
class ShaderListener(sublime_plugin.EventListener):
def __init__(self):
self.lastTime=0
def on_post_save(self, view):
filename=view.file_name()
if not filename:
return
if not helper.checkFileExt(filename,"shader"):
return
class ShaderGotoDefinitionCommand(sublime_plugin.TextCommand):
def run(self, edit):
# select text
# print("ShaderGotoDefinitionCommand")
sel=self.view.substr(self.view.sel()[0])
if len(sel)==0:
# extend to the `word` under cursor
sel=self.view.substr(self.view.word(self.view.sel()[0]))
# find all match file
# print("---- %s" % sel)
matchList=[]
showList=[]
for item in DEFINITION_LIST:
for key in item[0]:
if key==sel:
matchList.append(item)
showList.append(item[1])
# print("matchList")
if len(matchList)==0:
sublime.status_message("Can not find definition '%s'"%(sel))
elif len(matchList)==1:
self.gotoDefinition(matchList[0])
else:
# multi match
self.matchList=matchList
on_done = functools.partial(self.on_done)
self.view.window().show_quick_panel(showList,on_done)
def on_done(self,index):
if index==-1:
return
item=self.matchList[index]
self.gotoDefinition(item)
def gotoDefinition(self,item):
# print("ShaderGotoDefinitionCommand-1")
# print("----- item %s" %(item))
definitionType=item[4]
filepath=item[2]
shader_root = checkUnityShaderRoot()
# print("shader_root %s" % shader_root)
if shader_root != "":
filepath=os.path.join(shader_root,filepath)
# print("filepath %s" % filepath)
if os.path.exists(filepath):
self.view.window().open_file(filepath+":"+str(item[3]),sublime.ENCODED_POSITION)
else:
sublime.status_message("%s not exists"%(filepath))
def is_enabled(self):
return helper.checkFileExt(self.view.file_name(),"shader")
def is_visible(self):
return self.is_enabled()
def plugin_loaded():
sublime.set_timeout(init, 200)
# st2
if not helper.isST3():
init()