void Convert(bool forceConversion) { converter = new CSharpToUnityScriptConverter(m_scriptRelativePath); string sourceScriptPath = Application.dataPath + m_scriptRelativePath + m_scriptName + m_sourceScriptExtension; string targetScriptPath = sourceScriptPath.Replace(m_sourceScriptExtension, m_targetScriptExtension); if (!File.Exists(sourceScriptPath)) { Debug.LogError("CustomScriptDev.Convert() : source script does not exists at path [" + sourceScriptPath + "]"); return; } // convert if forcer, or target does not exist, or source newer than target if (forceConversion || !File.Exists(targetScriptPath) || File.GetLastWriteTime(sourceScriptPath) > File.GetLastWriteTime(targetScriptPath)) { CSharpToUnityScriptConverter.importedAssemblies.Clear(); StreamReader reader = new StreamReader(sourceScriptPath); string inputCode = reader.ReadToEnd(); reader.Close(); string outputCode; converter.Convert(inputCode); //CSharpToUnityScriptConverter convertor = new CSharpToUnityScriptConverter (inputCode); outputCode = converter.convertedCode; StreamWriter writer = new StreamWriter(targetScriptPath); writer.Write(outputCode); writer.Flush(); writer.Close(); Debug.Log("Convert " + m_scriptName + " at " + DateTime.Now.ToLocalTime()); AssetDatabase.Refresh(); } }
// ---------------------------------------------------------------------------------- /// <summaray> /// Called 100 times per second /// Perform the conversion, one file per frame /// </summary> void Update() { if (scriptsToConvertList.Count > 0) { Script scriptInConversion = scriptsToConvertList[0]; // makes sure the target directory exists string targetScriptPath = Application.dataPath + targetDirectory + scriptInConversion.relativePath; Directory.CreateDirectory(targetScriptPath); // make sure the directory exist, or create it // write the converted code into the file if (converter == null) { converter = new CSharpToUnityScriptConverter(sourceDirectory); } // converter.Convert(scriptInConversion.text); //CSharpToUnityScriptConverter converter = new CSharpToUnityScriptConverter(scriptInConversion.text); StreamWriter writer = new StreamWriter(targetScriptPath + scriptInConversion.name + ".js"); writer.Write(converter.Convert(scriptInConversion.text)); writer.Flush(); writer.Close(); Debug.Log("Converted " + scriptInConversion.relativePath + scriptInConversion.name); scriptsToConvertList.RemoveAt(0); Repaint(); // update the GUI // auto refresh the project once all files have been converted if (scriptsToConvertList.Count <= 0) { Debug.LogWarning("Conversion done ! Refreshing project."); AssetDatabase.Refresh(); } } }
/// <summary> /// Draw the GUI /// </summary> void OnGUI() { // scrollPosition = GUILayout.BeginScrollView(scrollPosition); GUILayout.Label("The two paths below are relative to the \"Assets\" directory"); sourceDirectory = EditorGUILayout.TextField("Source directory : ", sourceDirectory); targetDirectory = EditorGUILayout.TextField("Target directory : ", targetDirectory); //GUILayout.Label("A copy of the content of the source directory will be created inside the target directory with the converted scripts."); GUILayout.Space(10); GUILayout.BeginHorizontal(); if (GUILayout.Button("Convert", GUILayout.MaxWidth(200))) { if (scriptsToConvertList.Count > 0) { return; } if (!sourceDirectory.StartsWith("/")) { sourceDirectory = "/" + sourceDirectory; } if (!Directory.Exists(Application.dataPath + sourceDirectory)) { Debug.LogError("C# to UnityScript converter : Abording convertion because the source directory [" + sourceDirectory + "] does not exists !"); return; } string[] paths = Directory.GetFiles(Application.dataPath + sourceDirectory, "*.cs", SearchOption.AllDirectories); // only C# scripts in the whole hyerarchie of the source directory // fill scriptsToConvertList foreach (string path in paths) { StreamReader reader = new StreamReader(path); string text = reader.ReadToEnd(); reader.Close(); string relativePath = path.Replace(Application.dataPath + sourceDirectory, ""); // just keep the relative path from the source directory scriptsToConvertList.Add(new Script(relativePath, text)); } if (!targetDirectory.StartsWith("/")) { targetDirectory = "/" + targetDirectory; } if (scriptsToConvertList.Count > 0) { Debug.Log("Proceeding with the conversion of " + scriptsToConvertList.Count + " scripts."); CSharpToUnityScriptConverter.importedAssemblies.Clear(); } else { Debug.Log("No Scripts to convert."); } } if (GUILayout.Button("Abord", GUILayout.MaxWidth(200))) { Debug.LogWarning("Abording conversion and refreshing project."); scriptsToConvertList.Clear(); converter = null; AssetDatabase.Refresh(); } GUILayout.EndHorizontal(); GUILayout.Space(10); if (scriptsToConvertList.Count > 0) { GUILayout.Label(scriptsToConvertList.Count + " scripts left to convert."); } GUILayout.Space(10); GUILayout.Label("-----------"); GUILayout.Label("Options : "); CSharpToUnityScriptConverter.convertMultipleVarDeclaration = GUILayout.Toggle(CSharpToUnityScriptConverter.convertMultipleVarDeclaration, "Convert multiple variable declaration."); // GUILayout.EndScrollView(); }