protected void DrawToolbar() { using (new EditorGUILayout.HorizontalScope()) { if (GUILayout.Button("Copy To Clipboard", EditorStyles.toolbarButton)) { uNodeEditorUtility.CopyToClipboard(script); } if (GUILayout.Button("Check Errors", EditorStyles.toolbarButton)) { try { EditorUtility.DisplayProgressBar("Loading", "Compiling Scripts", 1); compileResult = GenerationUtility.CompileScript(script); } finally { EditorUtility.ClearProgressBar(); } } } }
private static void DoCompileReferences(uNodeRoot graph, ICollection <GameObject> referencedGraphs) { if (referencedGraphs.Count > 0) { uNodeThreadUtility.Queue(() => { var graphs = referencedGraphs.Where(g => g.GetComponent <uNodeComponentSystem>() is uNodeRoot root && !(root is IIndependentGraph) || g == null).ToList(); if (graphs.FirstOrDefault() != null) { var graphNames = string.Join("\n", graphs.Select(g => AssetDatabase.GetAssetPath(g))); if (!(graph is IIndependentGraph)) { graphs.Insert(0, graph.gameObject); } if (EditorUtility.DisplayDialog("Compile graph to c#", "Some graph has referenced to the renamed member and need to be compiled" + "\nReferenced graphs:" + "\n" + graphNames + "\n\nDo you want to compile this graph and referenced graphs?", "Ok", "Cancel")) { GenerationUtility.CompileNativeGraph(graphs); } } }); } }
public static void GenerateCSharpScript() { var scripts = GenerationUtility.GenerateProjectScripts(true); // var pathScripts = new List<string>(); // Directory.CreateDirectory(GenerationUtility.tempGeneratedFolder); // foreach(var script in scripts) { // var path = Path.GetFullPath(GenerationUtility.tempGeneratedFolder) + Path.DirectorySeparatorChar + "F" + script.GetGenerationID() + ".cs"; // using(StreamWriter sw = new StreamWriter(path)) { // sw.Write(GenerationUtility.ConvertLineEnding(script.ToScript(), false)); // sw.Close(); // } // pathScripts.Add(path); // Debug.Log(File.Exists(path)); // } // EditorUtility.DisplayProgressBar("Compiling Scripts", "", 1); // GenerationUtility.Compile(scripts.Select(s => s.ToScript()).ToArray()); var db = GetDatabase(); EditorUtility.DisplayProgressBar("Saving Scripts", "", 1); var dir = generatedPath + Path.DirectorySeparatorChar + "Scripts"; Directory.CreateDirectory(dir); foreach (var script in scripts) { var path = Path.GetFullPath(dir) + Path.DirectorySeparatorChar + script.fileName + ".cs"; var assetPath = AssetDatabase.GetAssetPath(script.graphOwner); if (File.Exists(assetPath.RemoveLast(6).Add("cs"))) { //Skip when the graph has been compiled manually continue; } using (StreamWriter sw = new StreamWriter(path)) { List <ScriptInformation> informations; var generatedScript = script.ToScript(out informations); if (informations != null) { uNodeEditor.SavedData.RegisterGraphInfos(informations, script.graphOwner, path); } sw.Write(ConvertLineEnding(generatedScript, false)); sw.Close(); } foreach (var root in script.graphs) { if (db.graphDatabases.Any(g => g.graph == root)) { continue; } db.graphDatabases.Add(new uNodeResourceDatabase.RuntimeGraphDatabase() { graph = root, }); EditorUtility.SetDirty(db); } } AssetDatabase.Refresh(); AssetDatabase.SaveAssets(); EditorUtility.ClearProgressBar(); db.ClearCache(); Debug.Log("Successful generating project script, project graphs will run with native c#." + "\nRemember to compiles the graph again if you made a changes to a graphs to keep the script up to date." + "\nRemoving generated scripts will makes the graph to run with reflection again." + "\nGenerated project script can be found on: " + dir); }
public static void CompileNativeGraph(GameObject graphObject, bool enableLogging = true) { string fileName = graphObject.name; GameObject prefabContent = null; var go = graphObject; if (uNodeEditorUtility.IsPrefab(graphObject)) { if (GraphUtility.HasTempGraphObject(graphObject)) { go = GraphUtility.GetTempGraphObject(graphObject); } else { prefabContent = PrefabUtility.LoadPrefabContents(AssetDatabase.GetAssetPath(graphObject)); go = prefabContent; } } else if (GraphUtility.IsTempGraphObject(graphObject)) { graphObject = GraphUtility.GetOriginalObject(graphObject); } uNodeRoot renamedRoot = null; { var root = go.GetComponent <uNodeRoot>(); if (root && string.IsNullOrEmpty(root.Name)) { root.Name = fileName; renamedRoot = root; } } Directory.CreateDirectory(GenerationUtility.tempFolder); char separator = Path.DirectorySeparatorChar; string path = GenerationUtility.tempFolder + separator + fileName + ".cs"; try { System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch(); watch.Start(); var script = GenerationUtility.GenerateCSharpScript(go, true, (progress, text) => { EditorUtility.DisplayProgressBar("Loading", text, progress); }); List <ScriptInformation> informations; var generatedScript = script.ToScript(out informations); if (preferenceData.generatorData.convertLineEnding) { generatedScript = GenerationUtility.ConvertLineEnding(generatedScript, Application.platform != RuntimePlatform.WindowsEditor); } if (preferenceData.generatorData != null && preferenceData.generatorData.analyzeScript && preferenceData.generatorData.formatScript) { var codeFormatter = TypeSerializer.Deserialize("MaxyGames.uNode.Editors.CSharpFormatter", false); if (codeFormatter != null) { var str = codeFormatter. GetMethod("FormatCode"). Invoke(null, new object[] { generatedScript }) as string; generatedScript = str; } } using (StreamWriter sw = new StreamWriter(path)) { sw.Write(generatedScript); sw.Close(); } watch.Stop(); if (enableLogging) { Debug.LogFormat("Generating C# took {0,8:N3} s.", watch.Elapsed.TotalSeconds); } if (preferenceData.generatorData.compileScript) { bool isBecauseOfAccessibility = false; try { watch.Reset(); watch.Start(); EditorUtility.DisplayProgressBar("Loading", "Compiling", 1); var compileResult = CompileScript(generatedScript); if (compileResult.assembly == null) { isBecauseOfAccessibility = true; foreach (var error in compileResult.errors) { if (error.errorNumber != "CS0122") { isBecauseOfAccessibility = false; break; } } throw new Exception(compileResult.GetErrorMessage()); } watch.Stop(); #if !NET_STANDARD_2_0 if (enableLogging) { Debug.LogFormat("Compiling script took {0,8:N3} s.", watch.Elapsed.TotalSeconds); } #endif } catch (System.Exception ex) { watch.Stop(); EditorUtility.ClearProgressBar(); if (EditorUtility.DisplayDialog("Compile Errors", "Compile before save detect an error: \n" + ex.Message + "\n\n" + (isBecauseOfAccessibility ? "The initial errors may because of using a private class.\nWould you like to ignore the error and save it?" : "Would you like to ignore the error and save it?"), "Ok, save it", "No, don't save")) { Debug.Log("Compile errors: " + ex.Message); } else { Debug.Log("Temp script saved to: " + Path.GetFullPath(path)); throw ex; } } } if (EditorUtility.IsPersistent(graphObject)) //For prefab and asset { path = (Path.GetDirectoryName(AssetDatabase.GetAssetPath(graphObject)) + separator + fileName + ".cs"); if (informations != null) { uNodeEditor.SavedData.RegisterGraphInfos(informations, script.graphOwner, path); } using (FileStream stream = File.Open(path, FileMode.Create, FileAccess.Write)) { using (StreamWriter writer = new StreamWriter(stream)) { writer.Write(generatedScript); writer.Close(); } stream.Close(); } } else //For the scene object. { path = EditorUtility.SaveFilePanel("Save Script", "Assets", fileName + ".cs", "cs"); if (informations != null) { uNodeEditor.SavedData.RegisterGraphInfos(informations, script.graphOwner, path); } using (FileStream stream = File.Open(path, FileMode.Create, FileAccess.Write)) { using (StreamWriter writer = new StreamWriter(stream)) { writer.Write(generatedScript); writer.Close(); } stream.Close(); } } AssetDatabase.Refresh(); Debug.Log("Script saved to: " + Path.GetFullPath(path)); EditorUtility.ClearProgressBar(); } catch { EditorUtility.ClearProgressBar(); Debug.LogError("Aborting Generating C# Script because have error."); throw; } finally { if (renamedRoot) { renamedRoot.Name = ""; } if (prefabContent != null) { PrefabUtility.UnloadPrefabContents(prefabContent); } } }
public static void GenerateCSharpScriptForSceneGraphs() { DeleteGeneratedCSharpScriptForScenes(); //Removing previous files so there's no outdated scripts var scenes = EditorBuildSettings.scenes; var dir = generatedPath + Path.DirectorySeparatorChar + "Scripts.Scenes"; // uNodeEditorUtility.FindAssetsByType<SceneAsset>(); for (int i = 0; i < scenes.Length; i++) { var scene = scenes[i]; var sceneAsset = AssetDatabase.LoadAssetAtPath <SceneAsset>(scene.path); if (sceneAsset == null || !scene.enabled) { continue; } EditorUtility.DisplayProgressBar($"Loading Scene: {sceneAsset.name} {i+1}-{scenes.Length}", "", 0); while (uNodeThreadUtility.IsNeedUpdate()) { uNodeThreadUtility.Update(); } GraphUtility.DestroyTempGraph(); var currentScene = EditorSceneManager.OpenScene(scene.path); var graphs = GameObject.FindObjectsOfType <uNodeComponentSystem>().Select(item => item.gameObject).Distinct().ToArray(); var scripts = new List <CodeGenerator.GeneratedData>(); int count = 0; foreach (var graph in graphs) { count++; scripts.Add(GenerationUtility.GenerateCSharpScript(graph, true, (progress, info) => { EditorUtility.DisplayProgressBar($"Generating C# for: {sceneAsset.name} {i+1}-{scenes.Length} current: {count}-{graphs.Length}", info, progress); })); } while (uNodeThreadUtility.IsNeedUpdate()) { uNodeThreadUtility.Update(); } GraphUtility.DestroyTempGraph(); EditorSceneManager.SaveScene(currentScene); EditorUtility.DisplayProgressBar("Saving Scene Scripts", "", 1); Directory.CreateDirectory(dir); var startPath = Path.GetFullPath(dir) + Path.DirectorySeparatorChar; foreach (var script in scripts) { var path = startPath + currentScene.name + "_" + script.fileName + ".cs"; int index = 1; while (File.Exists(path)) //Ensure name to be unique { path = startPath + currentScene.name + "_" + script.fileName + index + ".cs"; index++; } using (StreamWriter sw = new StreamWriter(path)) { List <ScriptInformation> informations; var generatedScript = script.ToScript(out informations); if (informations != null) { uNodeEditor.SavedData.RegisterGraphInfos(informations, script.graphOwner, path); } sw.Write(GenerationUtility.ConvertLineEnding(generatedScript, false)); sw.Close(); } } } AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); EditorUtility.ClearProgressBar(); Debug.Log("Successful generating scenes script, existing scenes graphs will run with native c#." + "\nRemember to compiles the graph again if you made a changes to a graphs to keep the script up to date." + "\nRemoving generated scripts will makes the graph to run with reflection again." + "\nGenerated scenes script can be found on: " + dir); }