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