Esempio n. 1
0
        /// <summary>
        /// Save the temporary graph object into the original prefab
        /// </summary>
        /// <param name="graph"></param>
        public static void SaveTemporaryGraph(GameObject graph)
        {
            var asset = GraphUtility.GetOriginalObject(graph);

            if (asset != null)
            {
                SaveGraphAsset(asset, graph);
            }
            else
            {
                Debug.Log("Cannot save temporary graph: " + graph.name + " because the original graph cannot be found.");
            }
        }
Esempio n. 2
0
        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);
                }
            }
        }