Exemplo n.º 1
0
        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);
        }