static public void SavePrefabBuildInfo(string buildPath, PrefabBuilder builder, Model.NodeData node, BuildTarget target, string groupKey, List <AssetReference> assets) { var prefabCacheDir = FileUtility.EnsureCacheDirExists(target, node, PrefabBuilder.kCacheDirName); var buildInfoPath = FileUtility.PathCombine(prefabCacheDir, groupKey + ".asset"); var version = PrefabBuilderUtility.GetPrefabBuilderVersion(builder.Builder.ClassName); var buildInfo = ScriptableObject.CreateInstance <PrefabBuildInfo>(); buildInfo.Initialize(buildPath, groupKey, builder.Builder.ClassName, builder.Builder[target], version, builder.Options, assets); AssetDatabase.CreateAsset(buildInfo, buildInfoPath); }
public override void OnInspectorGUI(NodeGUI node, AssetReferenceStreamManager streamManager, NodeGUIEditor editor, Action onValueChanged) { EditorGUILayout.HelpBox("Create Prefab From Group: Create prefab from incoming group of assets, using assigned script.", MessageType.Info); editor.UpdateNodeName(node); var builder = m_instance.Get <IPrefabBuilder>(editor.CurrentEditingGroup); using (new EditorGUILayout.VerticalScope(GUI.skin.box)) { var map = PrefabBuilderUtility.GetAttributeAssemblyQualifiedNameMap(); if (map.Count > 0) { using (new GUILayout.HorizontalScope()) { GUILayout.Label("PrefabBuilder"); var guiName = PrefabBuilderUtility.GetPrefabBuilderGUIName(m_instance.ClassName); if (GUILayout.Button(guiName, "Popup", GUILayout.MinWidth(150f))) { var builders = map.Keys.ToList(); if (builders.Count > 0) { NodeGUI.ShowTypeNamesMenu(guiName, builders, (string selectedGUIName) => { using (new RecordUndoScope("Change PrefabBuilder class", node, true)) { builder = PrefabBuilderUtility.CreatePrefabBuilder(selectedGUIName); m_instance.Set(editor.CurrentEditingGroup, builder); onValueChanged(); } } ); } } MonoScript s = TypeUtility.LoadMonoScript(m_instance.ClassName); using (new EditorGUI.DisabledScope(s == null)) { if (GUILayout.Button("Edit", GUILayout.Width(50))) { AssetDatabase.OpenAsset(s, 0); } } } ReplacePrefabOptions opt = (ReplacePrefabOptions)EditorGUILayout.EnumPopup("Prefab Replace Option", m_replacePrefabOptions, GUILayout.MinWidth(150f)); if (m_replacePrefabOptions != opt) { using (new RecordUndoScope("Change Prefab Replace Option", node, true)) { m_replacePrefabOptions = opt; onValueChanged(); } opt = m_replacePrefabOptions; } } else { if (!string.IsNullOrEmpty(m_instance.ClassName)) { EditorGUILayout.HelpBox( string.Format( "Your PrefabBuilder script {0} is missing from assembly. Did you delete script?", m_instance.ClassName), MessageType.Info); } else { string[] menuNames = Model.Settings.GUI_TEXT_MENU_GENERATE_PREFABBUILDER.Split('/'); EditorGUILayout.HelpBox( string.Format( "You need to create at least one PrefabBuilder script to use this node. To start, select {0}>{1}>{2} menu and create new script from template.", menuNames[1], menuNames[2], menuNames[3] ), MessageType.Info); } } GUILayout.Space(10f); editor.DrawPlatformSelector(node); using (new EditorGUILayout.VerticalScope()) { var disabledScope = editor.DrawOverrideTargetToggle(node, m_instance.ContainsValueOf(editor.CurrentEditingGroup), (bool enabled) => { if (enabled) { m_instance.CopyDefaultValueTo(editor.CurrentEditingGroup); m_outputDir[editor.CurrentEditingGroup] = m_outputDir.DefaultValue; m_outputOption[editor.CurrentEditingGroup] = m_outputOption.DefaultValue; } else { m_instance.Remove(editor.CurrentEditingGroup); m_outputDir.Remove(editor.CurrentEditingGroup); m_outputOption.Remove(editor.CurrentEditingGroup); } onValueChanged(); }); using (disabledScope) { OutputOption opt = (OutputOption)m_outputOption[editor.CurrentEditingGroup]; var newOption = (OutputOption)EditorGUILayout.EnumPopup("Output Option", opt); if (newOption != opt) { using (new RecordUndoScope("Change Output Option", node, true)){ m_outputOption[editor.CurrentEditingGroup] = (int)newOption; onValueChanged(); } opt = newOption; } using (new EditorGUI.DisabledScope(opt == OutputOption.CreateInCacheDirectory)) { var newDirPath = editor.DrawFolderSelector("Output Directory", "Select Output Folder", m_outputDir[editor.CurrentEditingGroup], Application.dataPath, (string folderSelected) => { string basePath = Application.dataPath; if (basePath == folderSelected) { folderSelected = string.Empty; } else { var index = folderSelected.IndexOf(basePath); if (index >= 0) { folderSelected = folderSelected.Substring(basePath.Length + index); if (folderSelected.IndexOf('/') == 0) { folderSelected = folderSelected.Substring(1); } } } return(folderSelected); } ); if (newDirPath != m_outputDir[editor.CurrentEditingGroup]) { using (new RecordUndoScope("Change Output Directory", node, true)){ m_outputDir[editor.CurrentEditingGroup] = newDirPath; onValueChanged(); } } var dirPath = Path.Combine(Application.dataPath, m_outputDir [editor.CurrentEditingGroup]); if (opt == OutputOption.CreateInSelectedDirectory && !string.IsNullOrEmpty(m_outputDir [editor.CurrentEditingGroup]) && !Directory.Exists(dirPath)) { using (new EditorGUILayout.HorizontalScope()) { EditorGUILayout.LabelField(m_outputDir[editor.CurrentEditingGroup] + " does not exist."); if (GUILayout.Button("Create directory")) { Directory.CreateDirectory(dirPath); AssetDatabase.Refresh(); } } EditorGUILayout.Space(); string parentDir = Path.GetDirectoryName(m_outputDir[editor.CurrentEditingGroup]); if (Directory.Exists(parentDir)) { EditorGUILayout.LabelField("Available Directories:"); string[] dirs = Directory.GetDirectories(parentDir); foreach (string s in dirs) { EditorGUILayout.LabelField(s); } } EditorGUILayout.Space(); } var outputDir = PrepareOutputDirectory(BuildTargetUtility.GroupToTarget(editor.CurrentEditingGroup), node.Data); using (new EditorGUI.DisabledScope(!Directory.Exists(outputDir))) { using (new EditorGUILayout.HorizontalScope()) { GUILayout.FlexibleSpace(); if (GUILayout.Button("Highlight in Project Window", GUILayout.Width(180f))) { var folder = AssetDatabase.LoadMainAssetAtPath(outputDir); EditorGUIUtility.PingObject(folder); } } } } GUILayout.Space(8f); if (builder != null) { Action onChangedAction = () => { using (new RecordUndoScope("Change PrefabBuilder Setting", node)) { m_instance.Set(editor.CurrentEditingGroup, builder); onValueChanged(); } }; builder.OnInspectorGUI(onChangedAction); } } } } }
public override void Build(BuildTarget target, Model.NodeData node, IEnumerable <PerformGraph.AssetGroups> incoming, IEnumerable <Model.ConnectionData> connectionsToOutput, PerformGraph.Output Output, Action <Model.NodeData, string, float> progressFunc) { if (incoming == null) { return; } var builder = m_instance.Get <IPrefabBuilder>(target); UnityEngine.Assertions.Assert.IsNotNull(builder); var prefabOutputDir = PrepareOutputDirectory(target, node); Dictionary <string, List <AssetReference> > output = null; if (Output != null) { output = new Dictionary <string, List <AssetReference> >(); } var aggregatedGroups = new Dictionary <string, List <AssetReference> >(); foreach (var ag in incoming) { foreach (var key in ag.assetGroups.Keys) { if (!aggregatedGroups.ContainsKey(key)) { aggregatedGroups[key] = new List <AssetReference>(); } aggregatedGroups[key].AddRange(ag.assetGroups[key].AsEnumerable()); } } var anyPrefabCreated = false; foreach (var key in aggregatedGroups.Keys) { var assets = aggregatedGroups[key]; var allAssets = LoadAllAssets(assets); GameObject previousPrefab = null; //TODO var prefabFileName = builder.CanCreatePrefab(key, allAssets, previousPrefab); var prefabSavePath = FileUtility.PathCombine(prefabOutputDir, prefabFileName + ".prefab"); if (!Directory.Exists(Path.GetDirectoryName(prefabSavePath))) { Directory.CreateDirectory(Path.GetDirectoryName(prefabSavePath)); } if (!File.Exists(prefabSavePath) || PrefabBuildInfo.DoesPrefabNeedRebuilding(prefabOutputDir, this, node, target, key, assets)) { UnityEngine.GameObject obj = builder.CreatePrefab(key, allAssets, previousPrefab); if (obj == null) { throw new AssetGraphException(string.Format("{0} :PrefabBuilder {1} returned null in CreatePrefab() [groupKey:{2}]", node.Name, builder.GetType().FullName, key)); } LogUtility.Logger.LogFormat(LogType.Log, "{0} is (re)creating Prefab:{1} with {2}({3})", node.Name, prefabFileName, PrefabBuilderUtility.GetPrefabBuilderGUIName(m_instance.ClassName), PrefabBuilderUtility.GetPrefabBuilderVersion(m_instance.ClassName)); if (progressFunc != null) { progressFunc(node, string.Format("Creating {0}", prefabFileName), 0.5f); } PrefabUtility.CreatePrefab(prefabSavePath, obj, m_replacePrefabOptions); PrefabBuildInfo.SavePrefabBuildInfo(prefabOutputDir, this, node, target, key, assets); GameObject.DestroyImmediate(obj); anyPrefabCreated = true; AssetProcessEventRecord.GetRecord().LogModify(AssetDatabase.AssetPathToGUID(prefabSavePath)); } UnloadAllAssets(assets); if (anyPrefabCreated) { AssetDatabase.SaveAssets(); } if (output != null) { output[key] = new List <AssetReference> () { AssetReferenceDatabase.GetPrefabReference(prefabSavePath) }; } } if (Output != null) { var dst = (connectionsToOutput == null || !connectionsToOutput.Any())? null : connectionsToOutput.First(); Output(dst, output); } }
public override void Prepare(BuildTarget target, Model.NodeData node, IEnumerable <PerformGraph.AssetGroups> incoming, IEnumerable <Model.ConnectionData> connectionsToOutput, PerformGraph.Output Output) { ValidatePrefabBuilder(node, target, incoming, () => { throw new NodeException(node.Name + ":Output directory not found.", node); }, () => { throw new NodeException(node.Name + " :PrefabBuilder is not configured. Please configure from Inspector.", node); }, () => { throw new NodeException(node.Name + " :Failed to create PrefabBuilder from settings. Please fix settings from Inspector.", node); }, (string groupKey) => { throw new NodeException(string.Format("{0} :Can not create prefab with incoming assets for group {1}.", node.Name, groupKey), node); }, (AssetReference badAsset) => { throw new NodeException(string.Format("{0} :Can not import incoming asset {1}.", node.Name, badAsset.fileNameAndExtension), node); } ); if (incoming == null) { return; } var prefabOutputDir = PrepareOutputDirectory(target, node); var builder = m_instance.Get <IPrefabBuilder>(target); UnityEngine.Assertions.Assert.IsNotNull(builder); Dictionary <string, List <AssetReference> > output = null; if (Output != null) { output = new Dictionary <string, List <AssetReference> >(); } var aggregatedGroups = new Dictionary <string, List <AssetReference> >(); foreach (var ag in incoming) { foreach (var key in ag.assetGroups.Keys) { if (!aggregatedGroups.ContainsKey(key)) { aggregatedGroups[key] = new List <AssetReference>(); } aggregatedGroups[key].AddRange(ag.assetGroups[key].AsEnumerable()); } } foreach (var key in aggregatedGroups.Keys) { var assets = aggregatedGroups[key]; var thresold = PrefabBuilderUtility.GetPrefabBuilderAssetThreshold(m_instance.ClassName); if (thresold < assets.Count) { var guiName = PrefabBuilderUtility.GetPrefabBuilderGUIName(m_instance.ClassName); throw new NodeException(string.Format("{0} :Too many assets passed to {1} for group:{2}. {3}'s threshold is set to {4}", node.Name, guiName, key, guiName, thresold), node); } GameObject previousPrefab = null; //TODO List <UnityEngine.Object> allAssets = LoadAllAssets(assets); var prefabFileName = builder.CanCreatePrefab(key, allAssets, previousPrefab); if (output != null && prefabFileName != null) { output[key] = new List <AssetReference> () { AssetReferenceDatabase.GetPrefabReference(FileUtility.PathCombine(prefabOutputDir, prefabFileName + ".prefab")) }; } UnloadAllAssets(assets); } if (Output != null) { var dst = (connectionsToOutput == null || !connectionsToOutput.Any())? null : connectionsToOutput.First(); Output(dst, output); } }
static public bool DoesPrefabNeedRebuilding(string buildPath, PrefabBuilder builder, Model.NodeData node, BuildTarget target, string groupKey, List <AssetReference> assets) { var buildInfo = GetPrefabBuildInfo(builder, node, target, groupKey); // need rebuilding if no buildInfo found if (buildInfo == null) { return(true); } // need rebuilding if build path is changed if (buildInfo.m_buildDir != buildPath) { return(true); } // need rebuilding if given builder is changed if (buildInfo.m_builderClass != builder.Builder.ClassName) { return(true); } // need rebuilding if given builder is changed if (buildInfo.m_instanceData != builder.Builder[target]) { return(true); } // need rebuilding if replace prefab option is changed if (buildInfo.m_replacePrefabOptions != (int)builder.Options) { return(true); } var builderVersion = PrefabBuilderUtility.GetPrefabBuilderVersion(builder.Builder.ClassName); // need rebuilding if given builder version is changed if (buildInfo.m_prefabBuilderVersion != builderVersion) { return(true); } // need rebuilding if given groupKey changed if (buildInfo.m_groupKey != groupKey) { return(true); } if (!Enumerable.SequenceEqual( buildInfo.m_usedAssets.Select(v => v.importFrom).OrderBy(s => s), assets.Select(v => v.importFrom).OrderBy(s => s))) { return(true); } // If any asset is modified from last time, then need rebuilding foreach (var usedAsset in buildInfo.m_usedAssets) { if (usedAsset.IsAssetModifiedFromLastTime) { return(true); } } return(false); }
public override void Build(BuildTarget target, Model.NodeData node, IEnumerable <PerformGraph.AssetGroups> incoming, IEnumerable <Model.ConnectionData> connectionsToOutput, PerformGraph.Output Output, Action <Model.NodeData, string, float> progressFunc) { if (incoming == null) { return; } var builder = m_instance.Get <IPrefabBuilder>(target); Assert.IsNotNull(builder); var prefabOutputDir = PrepareOutputDirectory(target, node); Dictionary <string, List <AssetReference> > output = null; if (Output != null) { output = new Dictionary <string, List <AssetReference> >(); } var aggregatedGroups = new Dictionary <string, List <AssetReference> >(); foreach (var ag in incoming) { foreach (var key in ag.assetGroups.Keys) { if (!aggregatedGroups.ContainsKey(key)) { aggregatedGroups[key] = new List <AssetReference>(); } aggregatedGroups[key].AddRange(ag.assetGroups[key].AsEnumerable()); } } var anyPrefabCreated = false; foreach (var key in aggregatedGroups.Keys) { var assets = aggregatedGroups[key]; var allAssets = LoadAllAssets(assets); try { m_createDescription.Reset(); var canCreatePrefab = builder.CanCreatePrefab(key, allAssets, ref m_createDescription); Assert.IsTrue(canCreatePrefab, "CanCreatePrefab() should not fail at Build phase."); } catch (Exception e) { throw new NodeException(e.Message, "See reason for detail.", node); } var prefabSavePath = FileUtility.PathCombine(prefabOutputDir, m_createDescription.prefabName + ".prefab"); if (!Directory.Exists(Path.GetDirectoryName(prefabSavePath))) { Directory.CreateDirectory(Path.GetDirectoryName(prefabSavePath)); } if (!File.Exists(prefabSavePath) || PrefabBuildInfo.DoesPrefabNeedRebuilding(prefabOutputDir, this, node, target, key, assets, m_createDescription)) { GameObject obj; GameObject previous = null; try { if (m_loadPreviousPrefab && File.Exists(prefabSavePath)) { previous = PrefabUtility.LoadPrefabContents(prefabSavePath); } obj = builder.CreatePrefab(key, allAssets, previous); } catch (Exception e) { throw new NodeException(e.Message, "See reason for detail.", node); } if (obj == null) { throw new AssetGraphException( $"{node.Name} :PrefabBuilder {builder.GetType().FullName} returned null in CreatePrefab() [groupKey:{key}]"); } LogUtility.Logger.LogFormat(LogType.Log, "{0} is (re)creating Prefab:{1} with {2}({3})", node.Name, m_createDescription.prefabName, PrefabBuilderUtility.GetPrefabBuilderGUIName(m_instance.ClassName), PrefabBuilderUtility.GetPrefabBuilderVersion(m_instance.ClassName)); progressFunc?.Invoke(node, $"Creating {m_createDescription.prefabName}", 0.5f); var isPartOfAsset = PrefabUtility.IsPartOfPrefabAsset(obj); PrefabUtility.SaveAsPrefabAsset(obj, prefabSavePath); if (previous != obj && isPartOfAsset) { PrefabUtility.UnloadPrefabContents(obj); } else { Object.DestroyImmediate(obj); } if (previous) { PrefabUtility.UnloadPrefabContents(previous); } PrefabBuildInfo.SavePrefabBuildInfo(prefabOutputDir, this, node, target, key, assets, m_createDescription); anyPrefabCreated = true; AssetProcessEventRecord.GetRecord().LogModify(AssetDatabase.AssetPathToGUID(prefabSavePath)); } UnloadAllAssets(assets); if (anyPrefabCreated) { AssetDatabase.SaveAssets(); } if (output != null) { output[key] = new List <AssetReference> () { AssetReferenceDatabase.GetPrefabReference(prefabSavePath) }; } } if (Output != null) { var dst = (connectionsToOutput == null || !connectionsToOutput.Any())? null : connectionsToOutput.First(); Output(dst, output); } }
public override void Prepare(BuildTarget target, Model.NodeData node, IEnumerable <PerformGraph.AssetGroups> incoming, IEnumerable <Model.ConnectionData> connectionsToOutput, PerformGraph.Output Output) { ValidatePrefabBuilder(node, target, incoming, () => throw new NodeException("Output directory not found.", "Create output directory or set a valid directory path.", node), () => throw new NodeException("PrefabBuilder is not configured.", "Configure PrefabBuilder from inspector.", node), () => throw new NodeException("Failed to create PrefabBuilder from settings.", "Fix settings from inspector.", node), (string groupKey) => throw new NodeException( $"Can not create prefab with incoming assets for group {groupKey}.", "Fix group input assets for selected PrefabBuilder.", node), (AssetReference badAsset) => throw new NodeException( $"Can not import incoming asset {badAsset.fileNameAndExtension}.", "", node)); if (incoming == null) { return; } var prefabOutputDir = PrepareOutputDirectory(target, node); var builder = m_instance.Get <IPrefabBuilder>(target); UnityEngine.Assertions.Assert.IsNotNull(builder); Dictionary <string, List <AssetReference> > output = null; if (Output != null) { output = new Dictionary <string, List <AssetReference> >(); } var aggregatedGroups = new Dictionary <string, List <AssetReference> >(); foreach (var ag in incoming) { foreach (var key in ag.assetGroups.Keys) { if (!aggregatedGroups.ContainsKey(key)) { aggregatedGroups[key] = new List <AssetReference>(); } aggregatedGroups[key].AddRange(ag.assetGroups[key].AsEnumerable()); } } foreach (var key in aggregatedGroups.Keys) { var assets = aggregatedGroups[key]; var threshold = PrefabBuilderUtility.GetPrefabBuilderAssetThreshold(m_instance.ClassName); if (threshold < assets.Count) { var guiName = PrefabBuilderUtility.GetPrefabBuilderGUIName(m_instance.ClassName); throw new NodeException( string.Format("Too many assets passed to {0} for group:{1}. {2}'s threshold is set to {4}", guiName, key, guiName, threshold), string.Format("Limit number of assets in a group to {4}", threshold), node); } List <UnityEngine.Object> allAssets = LoadAllAssets(assets); bool canCreatePrefab; if (m_createDescription == null) { m_createDescription = new PrefabCreateDescription(); } try { m_createDescription.Reset(); canCreatePrefab = builder.CanCreatePrefab(key, allAssets, ref m_createDescription); } catch (Exception e) { throw new NodeException(e.Message, "See reason for detail.", node); } if (output != null && canCreatePrefab) { output[key] = new List <AssetReference> () { AssetReferenceDatabase.GetPrefabReference(FileUtility.PathCombine(prefabOutputDir, m_createDescription.prefabName + ".prefab")) }; } UnloadAllAssets(assets); } if (Output != null) { var dst = (connectionsToOutput == null || !connectionsToOutput.Any())? null : connectionsToOutput.First(); Output(dst, output); } }
public static bool DoesPrefabNeedRebuilding(string buildPath, PrefabBuilder builder, Model.NodeData node, BuildTarget target, string groupKey, List <AssetReference> assets, PrefabCreateDescription createDescription) { var buildInfo = GetPrefabBuildInfo(builder, node, target, groupKey); // need rebuilding if no buildInfo found if (buildInfo == null) { return(true); } // need rebuilding if build path is changed if (buildInfo.m_buildDir != buildPath) { return(true); } // need rebuilding if given builder is changed if (buildInfo.m_builderClass != builder.Builder.ClassName) { return(true); } // need rebuilding if given builder is changed if (buildInfo.m_instanceData != builder.Builder[target]) { return(true); } var builderVersion = PrefabBuilderUtility.GetPrefabBuilderVersion(builder.Builder.ClassName); // need rebuilding if given builder version is changed if (buildInfo.m_prefabBuilderVersion != builderVersion) { return(true); } // need rebuilding if given groupKey changed if (buildInfo.m_groupKey != groupKey) { return(true); } var hash1 = MD5.Create(); assets.ForEach(a => hash1.ComputeHash(System.Text.Encoding.UTF8.GetBytes(a.importFrom))); createDescription.additionalAssetPaths.ForEach(path => hash1.ComputeHash(System.Text.Encoding.UTF8.GetBytes(path))); if (buildInfo.m_usedAssetsHash != hash1.ToString()) { return(true); } // If any asset is modified from last time, then need rebuilding foreach (var usedAsset in buildInfo.m_usedAssets) { if (usedAsset.IsAssetModifiedFromLastTime) { return(true); } } return(false); }