Пример #1
0
 /// <summary>
 /// Save the temporary graph object into the original prefab
 /// </summary>
 /// <param name="graphAsset"></param>
 /// <param name="graph"></param>
 public static void SaveGraphAsset(GameObject graphAsset, GameObject graph)
 {
     if (graph != null && graphAsset != null)
     {
         uNodeEditorUtility.UnlockPrefabInstance(EditorBinding.getPrefabParent(graph) as GameObject);
         if (graphAsset.name != graph.name)                  //Ensure the name is same.
         {
             graph.name = graphAsset.name;
         }
         {                //Reset cache data
             var roots     = (graphAsset as GameObject).GetComponents <uNodeRoot>();
             var tempRoots = (graph as GameObject).GetComponents <uNodeRoot>();
             if (roots.Length != tempRoots.Length)
             {
                 UGraphView.ClearCache();
             }
             else
             {
                 for (int i = 0; i < roots.Length; i++)
                 {
                     if (roots[i].Name != tempRoots[i].Name)
                     {
                         UGraphView.ClearCache();
                         break;
                     }
                 }
             }
         }
         uNodeEditorUtility.SavePrefabAsset(graph, graphAsset);
         var graphs = (graphAsset as GameObject).GetComponents <uNodeRoot>();
         //Reset the cache data
         foreach (var r in graphs)
         {
             if (r == null)
             {
                 continue;
             }
             var rType = ReflectionUtils.GetRuntimeType(r);
             if (rType is RuntimeGraphType graphType)
             {
                 graphType.RebuildMembers();
             }
         }
     }
 }
Пример #2
0
 internal static void CompileAndPatchProjectGraphs()
 {
     if (CanCompileScript() && EditorBinding.patchType != null)
     {
         var scripts = GenerateProjectScripts(true);
         var db      = GetDatabase();
         var dir     = "TempScript" + Path.DirectorySeparatorChar + "GeneratedOnPlay";
         Directory.CreateDirectory(dir);
         List <Type>      types     = new List <Type>();
         HashSet <string> fileNames = new HashSet <string>();
         List <string>    paths     = new List <string>();
         foreach (var script in scripts)
         {
             var fileName = script.fileName;
             int index    = 2;
             while (!fileNames.Add(fileName))
             {
                 fileName = script.fileName + index;
             }
             var path = Path.GetFullPath(dir) + Path.DirectorySeparatorChar + script.fileName + ".cs";
             using (StreamWriter sw = new StreamWriter(path)) {
                 var generatedScript = script.ToScript(out var 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);
             }
             paths.Add(path);
             var ns = script.Namespace;
             foreach (var pair in script.classNames)
             {
                 if (string.IsNullOrEmpty(ns))
                 {
                     types.Add(pair.Value.ToType(false));
                 }
                 else
                 {
                     types.Add((ns + "." + pair.Value).ToType(false));
                 }
             }
         }
         var assembly = CompileFromFile(paths.ToArray());
         if (assembly == null)
         {
             return;
         }
         for (int i = 0; i < types.Count; i++)
         {
             if (types[i] == null)
             {
                 continue;
             }
             var type = assembly.GetType(types[i].FullName);
             if (type != null)
             {
                 EditorUtility.DisplayProgressBar("Patching", "Patch generated c# into existing script.", (float)i / types.Count);
                 EditorBinding.patchType(types[i], type);
             }
         }
         ReflectionUtils.RegisterRuntimeAssembly(assembly);
         ReflectionUtils.UpdateAssemblies();
         ReflectionUtils.GetAssemblies();
     }
 }