id="cproIframe_u1121907_1" width="567" height="472.5" src="http://pos.baidu.com/acom?adn=4&at=134&aurl=&cad=1&ccd=24&cec=UTF-8&cfv=0&ch=0&col=en-US&conOP=0&cpa=1&dai=1&dis=0&layout_filter=rank%2Ctabcloud<r=http%3A%2F%2Fwww.xuanyusong.com%2F<u=http%3A%2F%2Fwww.xuanyusong.com%2Farchives%2F3732&lunum=6&n=92004029_cpr&pcs=1280x659&pis=10000x10000&ps=369x883&psr=1280x720&pss=1280x409&qn=aa2b4f8fb4301140&rad=&rsi0=300&rsi1=250&rsi5=4&rss0=%23FFFFFF&rss1=%23FFFFFF&rss2=%230000FF&rss3=%23444444&rss4=%23008000&rss5=&rss6=%23e10900&rss7=&scale=&skin=&td_id=1121907&tn=text_default_300_250&tpr=1447667478353&ts=1&version=2.0&xuanting=0&dtm=BAIDU_DUP2_SETJSONADSLOT&dc=2&di=u1121907&ti=Unity3D%E7%A0%94%E7%A9%B6%E9%99%A2%E7%BC%96%E8%BE%91%E5%99%A8%E4%B9%8B%E5%88%9B%E5%BB%BALua%E8%84%9A%E6%9C%AC%E6%A8%A1%E6%9D%BF%EF%BC%88%E5%8D%81%E5%85%AD%EF%BC%89%20%7C%20%E9%9B%A8%E6%9D%BEMOMO%E7%A8%8B%E5%BA%8F%E7%A0%94%E7%A9%B6%E9%99%A2&tt=1447667478341.13.39.44" align="center,center" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" allowtransparency="true" style="margin: 0px; padding: 0px; font-family: inherit;">
Unity里能创建 c#脚本模板,但是如果我想创建Lua脚本模板怎么办呢?拓展一下编辑器吧。
设置一下Lua脚本的模板地址 : Assets/Editor/Lua/Template/lua.lua
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
|
using
UnityEngine
;
using
UnityEditor
;
using
System
;
using
System
.
IO
;
using
System
.
Text
;
using
UnityEditor
.
ProjectWindowCallback
;
using
System
.
Text
.
RegularExpressions
;
public
class
Test
{
[
MenuItem
(
"Assets/Create/Lua Script"
,
false
,
80
)
]
public
static
void
CreatNewLua
(
)
{
ProjectWindowUtil
.
StartNameEditingIfProjectWindowExists
(
0
,
ScriptableObject
.
CreateInstance
<
MyDoCreateScriptAsset
>
(
)
,
GetSelectedPathOrFallback
(
)
+
"/New Lua.lua"
,
null
,
"Assets/Editor/Lua/Template/lua.lua"
)
;
}
public
static
string
GetSelectedPathOrFallback
(
)
{
string
path
=
"Assets"
;
foreach
(
UnityEngine
.
Object
obj
in
Selection
.
GetFiltered
(
typeof
(
UnityEngine
.
Object
)
,
SelectionMode
.
Assets
)
)
{
path
=
AssetDatabase
.
GetAssetPath
(
obj
)
;
if
(
!
string
.
IsNullOrEmpty
(
path
)
&&
File
.
Exists
(
path
)
)
{
path
=
Path
.
GetDirectoryName
(
path
)
;
break
;
}
}
return
path
;
}
}
class
MyDoCreateScriptAsset
:
EndNameEditAction
{
public
override
void
Action
(
int
instanceId
,
string
pathName
,
string
resourceFile
)
{
UnityEngine
.
Object
o
=
CreateScriptAssetFromTemplate
(
pathName
,
resourceFile
)
;
ProjectWindowUtil
.
ShowCreatedAsset
(
o
)
;
}
internal
static
UnityEngine
.
Object
CreateScriptAssetFromTemplate
(
string
pathName
,
string
resourceFile
)
{
string
fullPath
=
Path
.
GetFullPath
(
pathName
)
;
StreamReader
streamReader
=
new
StreamReader
(
resourceFile
)
;
string
text
=
streamReader
.
ReadToEnd
(
)
;
streamReader
.
Close
(
)
;
string
fileNameWithoutExtension
=
Path
.
GetFileNameWithoutExtension
(
pathName
)
;
text
=
Regex
.
Replace
(
text
,
"#NAME#"
,
fileNameWithoutExtension
)
;
//string text2 = Regex.Replace(fileNameWithoutExtension, " ", string.Empty);
//text = Regex.Replace(text, "#SCRIPTNAME#", text2);
//if (char.IsUpper(text2, 0))
//{
// text2 = char.ToLower(text2[0]) + text2.Substring(1);
// text = Regex.Replace(text, "#SCRIPTNAME_LOWER#", text2);
//}
//else
//{
// text2 = "my" + char.ToUpper(text2[0]) + text2.Substring(1);
// text = Regex.Replace(text, "#SCRIPTNAME_LOWER#", text2);
//}
bool
encoderShouldEmitUTF8Identifier
=
true
;
bool
throwOnInvalidBytes
=
false
;
UTF8Encoding
encoding
=
new
UTF8Encoding
(
encoderShouldEmitUTF8Identifier
,
throwOnInvalidBytes
)
;
bool
append
=
false
;
StreamWriter
streamWriter
=
new
StreamWriter
(
fullPath
,
append
,
encoding
)
;
streamWriter
.
Write
(
text
)
;
streamWriter
.
Close
(
)
;
AssetDatabase
.
ImportAsset
(
pathName
)
;
return
AssetDatabase
.
LoadAssetAtPath
(
pathName
,
typeof
(
UnityEngine
.
Object
)
)
;
}
}
|
因为是模板就可以添加一些预制代码在上面, 当填写完类名后可以来替换操作, 例如unity自带的 创建C#文件, 外面写了类名里面也跟这变。Creat -> Lua Script
然后可以输入Lua脚本的类名。
然后就根据自己创建Lua的文件名 正则替换模板里的东西了。
下载地址:https://bitbucket.org/xuanyusong/project-createlua/
- 本文固定链接: http://www.xuanyusong.com/archives/3732
- 转载请注明: 雨松MOMO 2015年11月04日 于 雨松MOMO程序研究院 发表