一键完成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 \,=( ]*)(d+).(d+)([s \,;) ]*)";
replacements[reps++] = "$1$2.$3f$4";
//Style
patterns[patrs++] = " ";
replacements[reps++] = " ";
//RPC
patterns[patrs++] = @"([ s(){} ]+)@RPC";
replacements[reps++] = "$1[RPC]";
//boolean
patterns[patrs++] = @"([ s (){} :;]*)boolean([ s \,[=(){} ]+)";
replacements[reps++] = "${1}bool${2} ";
//String
patterns[patrs++] = @"([ s (){} :;]*)String([ s \,[=(){} ]+)";
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 ]*) in ([A-Za-z0-9_ :\,.s ]*))";
replacements[reps++] = "foreach($1 in $2)";
//function rewrite
patterns[patrs++] = @"function(s+)(w+)(s*)(([ A-Za-z0-9_[]*/ .:\,]*))(s*):(s*)(" + VAR + @"+)(s*){";
replacements[reps++] = "$7 $2 ($4){";
//function rewrite
patterns[patrs++] = @"function(s+)(w+)(s*)(([ A-Za-z0-9_[]*/ .:\,]*))(s*){";
replacements[reps++] = @"void $2 ($4){";
//Getcomponent
patterns[patrs++] = @"AddComponent(([ A-Za-z0-9_ ""]*))";
replacements[reps++] = "AddComponent<$1>()";
patterns[patrs++] = @"GetComponent(([ A-Za-z0-9_ ""]*))";
replacements[reps++] = "GetComponent<$1>()";
patterns[patrs++] = @"GetComponentsInChildren(([ A-Za-z0-9_ ""]*))";
replacements[reps++] = "GetComponentsInChildren<$1>()";
patterns[patrs++] = @"GetComponentInChildren(([ A-Za-z0-9_ ''""]*))";
replacements[reps++] = "GetComponentInChildren<$1>()";
patterns[patrs++] = @"FindObjectsOfType(([ A-Za-z0-9_ ""]*))";
replacements[reps++] = "FindObjectsOfType(typeof($1))";
patterns[patrs++] = @"FindObjectOfType(([ A-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 = @"(([ A-Za-z0-9_*/ .\,][]*)(w+)(s*):(s*)(" + VAR + @"+)([s\,]*)([[] 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; using System.Collections; public class " + className + " : MonoBehaviour { " + output + " }";
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;
}
}

原文地址:https://www.cnblogs.com/biubiubiubiu/p/4185455.html