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); } }
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); } }