代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
C#
】
JS转C#工具脚本
作者:
永泰
/ 发布于
2016/10/19
/
871
强大的JS转C#工具脚本: 点击要转换的JS脚本,然后在菜单栏上执行这个脚本就可以自动转换生成一个新的C#文件。
using UnityEngine; using UnityEditor; using System; using System.IO; using System.Collections.Generic; using System.Text.RegularExpressions; public class JStoC : EditorWindow { [MenuItem("Tools/Convert selected JS file(s) to C#")] static void ConvertJStoC() { UnityEngine.Object[] objects = Selection.GetFiltered(typeof(MonoScript), SelectionMode.Editable); int converted = 0; foreach (UnityEngine.Object obj in objects) { if (obj.GetType().ToString() == "UnityEditor.MonoScript") { string jsFile = AssetDatabase.GetAssetPath(obj); int jsEnd = jsFile.LastIndexOf(".js"); if (jsEnd <= 0) { Debug.LogError("JsToCs error: You did not select a .js file!"); return; } converted++; string cFilename = jsFile.Substring(0, jsEnd); cFilename += ".cs"; if (AssetDatabase.LoadAssetAtPath(cFilename, typeof(MonoScript)) != null) { Debug.LogError("JsToCs error: " + cFilename + " already exists!"); return; } string sourceCode = obj.ToString(); //string cCode = OnlineConvert(sourceCode, obj.name); string cCode = ConvertToC(sourceCode, obj.name); if (cCode == "") { Debug.LogError("Failed converting " + jsFile); continue; } try { TextWriter tw = new StreamWriter(cFilename); tw.Write(cCode); tw.Close(); } catch (System.IO.IOException IOEx) { Debug.LogError("Incorrect file permissions? "+IOEx); } //Optional: Delete old JS file //AssetDatabase.DeleteAsset(jsFile); AssetDatabase.ImportAsset(cFilename, ImportAssetOptions.ForceUpdate | ImportAssetOptions.ImportRecursive); Debug.Log("Converted " + Selection.activeObject.name + " to " + cFilename); } } if (converted < 1) { Debug.LogError("You did not select a JS file to convert! " + objects.Length + " files selected."); } } static string ConvertToC(string output, string className) { string VAR = @"[A-Za-z0-9_ \.]"; string VAR_NONARRAY = @"[A-Za-z0-9_]"; string[] patterns = new string[32]; string[] replacements = new string[32]; int patrs = 0; int reps = 0; //var AAA; patterns[patrs++] = @"var(\s+)(" + VAR_NONARRAY + @"+)(\s*);"; replacements[reps++] = "FIXME_VAR_TYPE $2;"; // var AAAA : XXX patterns[patrs++] = @"var(\s+)(" + VAR_NONARRAY + @"+)(\s*):(\s*)(" + VAR + @"+)"; replacements[reps++] = "$5 $2"; // var AAAA = patterns[patrs++] = @"var(\s+)(" + VAR_NONARRAY + @"+)(\s*)="; replacements[reps++] = "FIXME_VAR_TYPE $2="; //Try to fix the "FIXME_VAR_TYPE" for strings; patterns[patrs++] = @"FIXME_VAR_TYPE(\s+)(" + VAR_NONARRAY + @"+)(\s*)=(\s*)"""; replacements[reps++] = "string $2 = \""; //Try to fix the "FIXME_VAR_TYPE" for floats; patterns[patrs++] = @"FIXME_VAR_TYPE(\s+)(" + VAR_NONARRAY + @"+)(\s*)=(\s*)(([0-9\-]*\.+[0-9]+[f]?)+)(\s*);"; replacements[reps++] = "float $2 = $5;"; //Try to fix the "FIXME_VAR_TYPE" for int; patterns[patrs++] = @"FIXME_VAR_TYPE(\s+)(" + VAR_NONARRAY + @"+)(\s*)=(\s*)([0-9\-]+)(\s*);"; replacements[reps++] = "float $2 = $5;"; //Try to fix the "FIXME_VAR_TYPE" for bool true/false; patterns[patrs++] = @"FIXME_VAR_TYPE(\s+)(" + VAR_NONARRAY + @"+)(\s*)=(\s*)true(\s*);"; replacements[reps++] = "bool $2 = true;"; patterns[patrs++] = @"FIXME_VAR_TYPE(\s+)(" + VAR_NONARRAY + @"+)(\s*)=(\s*)false(\s*);"; replacements[reps++] = "bool $2 = false;"; //0.05f patterns[patrs++] = @"([\s\t\r\n\,=]?)(\d+)\.(\d+)([\s\t\r; ]*)"; replacements[reps++] = "$1$2.$3f$4"; //Style patterns[patrs++] = "\n\n\n"; replacements[reps++] = "\n"; //RPC patterns[patrs++] = @"([\n\s \{\}\r]+)@RPC"; replacements[reps++] = "$1[RPC]"; //boolean patterns[patrs++] = @"([\n\t\s \{\}\r:;]*)\bboolean\b([\n\t\s \,\[=\(\)\{\}\r]+)"; replacements[reps++] = "${1}bool${2} "; //String patterns[patrs++] = @"([\n\t\s \{\}\r:;]*)\bString\b([\n\t\s \,\[=\(\)\{\}\r]+)"; replacements[reps++] = "${1}string${2}"; //Remove #pragma strict patterns[patrs++] = "#pragma strict"; replacements[reps++] = ""; patterns[patrs++] = "#pragma implicit"; replacements[reps++] = ""; patterns[patrs++] = "#pragma downcast"; replacements[reps++] = ""; //parseInt parseFloat patterns[patrs++] = "parseInt"; replacements[reps++] = "int.Parse"; patterns[patrs++] = "parseFloat"; replacements[reps++] = "float.Parse"; // (Rect( patterns[patrs++] = @"([]\(=)]+)[\s]*Rect[\s]*\("; replacements[reps++] = "${1} new Rect("; //Yield patterns[patrs++] = @"yield(\s+)(\w+);"; replacements[reps++] = "yield return ${2};"; patterns[patrs++] = @"yield;"; replacements[reps++] = "yield return 0;"; patterns[patrs++] = @"yield(\s+)(\w+)\("; replacements[reps++] = "yield return new ${2}("; patterns[patrs++] = @"yield new"; replacements[reps++] = "yield return new"; //For -> foreach patterns[patrs++] = @"for[\s]*([A?Za?z0?9 : \.\[\]\s\n\r\t]?)in([A?Za?z0?9 : \.\s\n\r\t]?) "; replacements[reps++] = "foreach($1 in $2)"; //function rewrite patterns[patrs++] = @"function(\s+)(\w+)(\s*)([\n\r\tA?Za?z0?9 \[ \]\*\/\.:]?) (\s*):(\s*)(" + VAR + @"+)(\s*)\{"; replacements[reps++] = "$7 $2 ($4){"; //function rewrite patterns[patrs++] = @"function(\s+)(\w+)(\s*)([\n\r\tA?Za?z0?9 \[ \]\*\/\.:]?) (\s*)\{"; replacements[reps++] = @"void $2 ($4){"; //Getcomponent patterns[patrs++] = @"AddComponent([\n\r\tA?Za?z0?9 ′′ "]?) "; replacements[reps++] = "AddComponent<$1>()"; patterns[patrs++] = @"GetComponent([\n\r\tA?Za?z0?9 ′′ "]?) "; replacements[reps++] = "GetComponent<$1>()"; patterns[patrs++] = @"GetComponentsInChildren([\n\r\tA?Za?z0?9 ′′ "]?) "; replacements[reps++] = "GetComponentsInChildren<$1>()"; patterns[patrs++] = @"GetComponentInChildren([\n\r\tA-Za-z0-9_ ''""]*)"; replacements[reps++] = "GetComponentInChildren<$1>()"; patterns[patrs++] = @"FindObjectsOfType([\n\r\tA?Za?z0?9 ′′ "]?) "; replacements[reps++] = "FindObjectsOfType(typeof($1))"; patterns[patrs++] = @"FindObjectOfType([\n\r\tA-Za-z0-9_ ''""]*)"; replacements[reps++] = "FindObjectOfType(typeof($1))"; output = PregReplace(output, patterns, replacements); string before = ""; while (before != output) { //( XX : YY) rewrite before = output; string patt = @"([\t\n\rA?Za?z0?9 ? \/\.\]\[]?)\b(\w+)\b(\s?):(\s?)("+VAR+@"+)([\s]?)([\[\]\n\r\t\sA?Za?z0?9 \* \/:\.]?) "; string repp = "(${1} ${5} ${2} ${6} ${7})";//'(${1} 5--${5}-- 2--${2}-- 5--${6}-- 7=--${7})' output = Regex.Replace(output, patt, repp); } output = "using UnityEngine;\nusing System.Collections;\n\npublic class " + className + " : MonoBehaviour {\n" + output + "\n}"; return output; } // If the Unity store updating doesn't work well enough I might allow online covnerting /*static string OnlineConvert(string input, string className) { WWWForm wwwForm = new WWWForm(); wwwForm.AddField("oldJS", input); WWW www = new WWW("http://www.m2h.nl/files/js_to_c.php?noprint=1", wwwForm); while (!www.isDone) { } if (www.error != null) Debug.LogError("Error converting: " + www.error); else { string cCode = www.text; cCode = cCode.Replace("MYCLASSNAME", className); return cCode; } return ""; }*/ static string PregReplace(string input, string[] pattern, string[] replacements) { if (replacements.Length != pattern.Length) throw new ArgumentException("Replacement and Pattern Arrays must be balanced"); for (var i = 0; i < pattern.Length; i++) { input = Regex.Replace(input, pattern[i], replacements[i]); } return input; } }
试试其它关键字
同语言下
.
C#实现的html内容截取
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
.
实现对图片上传的接收
.
去除字符串中的空格,回车,换行符转变成‘;’在按‘
.
按照回车换行符分割字符串
.
文件MD5码 比较,检测文件是否一样
可能有用的
.
C#实现的html内容截取
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
.
实现对图片上传的接收
.
去除字符串中的空格,回车,换行符转变成‘;’在按‘
.
按照回车换行符分割字符串
.
文件MD5码 比较,检测文件是否一样
永泰
贡献的其它代码
(
12
)
.
通过qrcode库生成二维码
.
等待上一行执行完成再执行下一行
.
导出数据到文件
.
据经纬度查询附近的点
.
是用JDBC从数据库中获取数据并以java对象返回
.
JS转C#工具脚本
.
透明样式,兼容全浏览器的css写法如下
.
加密(md5,sha1,base64)
.
NetUtils,跟网络相关的工具类
.
最小生成树 prim
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3