AssetBundleGraph.Asset.DuplicateAssetWithNewType C# (CSharp) Method

DuplicateAssetWithNewType() public static method

public static DuplicateAssetWithNewType ( Asset asset, Type newAssetType ) : Asset
asset Asset
newAssetType System.Type
return Asset
        public static Asset DuplicateAssetWithNewType(Asset asset, Type newAssetType)
        {
            return new Asset(
                guid:asset.guid,
                assetDatabaseId:asset.assetDatabaseId,
                absoluteAssetPath:asset.absoluteAssetPath,
                importFrom:asset.importFrom,
                exportTo:asset.exportTo,
                assetType:newAssetType,
                isNew:asset.isNew,
                isBundled:asset.isBundled,
                variantName:asset.variantName
            );
        }

Usage Example

        public void Setup(string nodeName, string nodeId, string connectionIdToNextNode, Dictionary <string, List <Asset> > groupedSources, List <string> alreadyCached, Action <string, string, Dictionary <string, List <Asset> >, List <string> > Output)
        {
            // reserve importSetting type for limit asset.
            var importSettingSampleType = string.Empty;


            var outputDict = new Dictionary <string, List <Asset> >();

            var first = true;

            if (groupedSources.Keys.Count == 0)
            {
                return;
            }

            // ImportSetting merges multiple incoming groups into one, so warn this
            if (1 < groupedSources.Keys.Count)
            {
                Debug.LogWarning(nodeName + " ImportSetting merges incoming group into \"" + groupedSources.Keys.ToList()[0]);
            }

            var inputSources = new List <Asset>();

            foreach (var groupKey in groupedSources.Keys)
            {
                inputSources.AddRange(groupedSources[groupKey]);
            }

            var importedAssets = new List <Asset>();


            var samplingDirectoryPath = FileUtility.PathCombine(AssetBundleGraphSettings.IMPORTER_SETTINGS_PLACE, nodeId);

            ValidateImportSample(samplingDirectoryPath,
                                 (string samplePath) => {
                // do nothing. keep importing new asset for sampling.
            },
                                 (string samplePath) => {
                // do nothing. keep importing new asset for sampling.
            },
                                 (string samplePath) => {
                importSettingSampleType = AssetImporter.GetAtPath(samplePath).GetType().ToString();
                first = false;
            },
                                 (string samplePath) => {
                throw new NodeException(
                    String.Format("Too many sample file found for this import setting node. Delete files in {0} or use \"Clear Saved ImportSettings\" menu.", samplePath),
                    nodeId);
            }
                                 );

            var alreadyImported = new List <string>();
            var ignoredResource = new List <string>();


            foreach (var asset in inputSources)
            {
                if (string.IsNullOrEmpty(asset.absoluteAssetPath))
                {
                    if (!string.IsNullOrEmpty(asset.importFrom))
                    {
                        alreadyImported.Add(asset.importFrom);
                        continue;
                    }

                    ignoredResource.Add(asset.fileNameAndExtension);
                    continue;
                }

                var assetType       = AssetImporter.GetAtPath(asset.importFrom).GetType();
                var importerTypeStr = assetType.ToString();

                /*
                 *      only texture, model and audio importer is acceptable.
                 */
                switch (importerTypeStr)
                {
                case "UnityEditor.TextureImporter":
                case "UnityEditor.ModelImporter":
                case "UnityEditor.AudioImporter": {
                    break;
                }

                default: {
                    throw new NodeException("unhandled importer type:" + importerTypeStr, nodeId);
                }
                }

                var newData = Asset.DuplicateAssetWithNewType(asset, assetType);
                importedAssets.Add(newData);

                if (first)
                {
                    if (!Directory.Exists(samplingDirectoryPath))
                    {
                        Directory.CreateDirectory(samplingDirectoryPath);
                    }

                    var absoluteFilePath = asset.absoluteAssetPath;
                    var targetFilePath   = FileUtility.PathCombine(samplingDirectoryPath, asset.fileNameAndExtension);

                    EditorUtility.DisplayProgressBar("AssetBundleGraph ImportSetting generating ImporterSetting...", targetFilePath, 0);
                    FileUtility.CopyFileFromGlobalToLocal(absoluteFilePath, targetFilePath);
                    first = false;
                    AssetDatabase.Refresh(ImportAssetOptions.ImportRecursive);
                    EditorUtility.ClearProgressBar();

                    importSettingSampleType = AssetImporter.GetAtPath(targetFilePath).GetType().ToString();
                }
                else
                {
                    if (importerTypeStr != importSettingSampleType)
                    {
                        throw new NodeException("Multiple asset type is given to Importer Settings. ImporterSetting Takes only 1 asset type." + nodeName + " is configured for " + importSettingSampleType + ", but " + importerTypeStr + " found.", nodeId);
                    }
                }


                if (alreadyImported.Any())
                {
                    Debug.LogError("importSetting:" + string.Join(", ", alreadyImported.ToArray()) + " are already imported.");
                }
                if (ignoredResource.Any())
                {
                    Debug.LogError("importSetting:" + string.Join(", ", ignoredResource.ToArray()) + " are ignored.");
                }

                outputDict[groupedSources.Keys.ToList()[0]] = importedAssets;
            }

            Output(nodeId, connectionIdToNextNode, outputDict, new List <string>());
        }