Beispiel #1
0
        /// <summary>
        /// 指定したベースフォルダをunitypackageにエクスポートする。
        /// </summary>
        /// <param name="baseFolderPath">ベースフォルダパス</param>
        /// <param name="setting">エクスポートに使用する設定</param>
        /// <param name="forceExport">ファイルが既に存在していた場合に上書きするかどうか</param>
        /// <returns>エクスポート結果</returns>
        public static ExportResult Export(string baseFolderPath, ExportSetting setting, bool forceExport = false)
        {
            if (setting == null)
            {
                throw new ArgumentNullException("Argument `setting` is null.");
            }
            if (baseFolderPath == null)
            {
                throw new ArgumentNullException("Argument `baseFolderPath` is null.");
            }
            var exportFolderPath = setting.ExportFolderPath;

            if (string.IsNullOrEmpty(exportFolderPath) || !exportFolderPath.StartsWith("Assets"))
            {
                throw new ArgumentNullException("Invalid export folder path:" + exportFolderPath);
            }

            var fileName   = string.IsNullOrEmpty(setting.fileNameFormat) ? "export.unitypackage" : setting.fileNameFormat;
            var outputPath = exportFolderPath + Path.AltDirectorySeparatorChar + fileName;
            var result     = new ExportResult();

            //check export folder path
            if (!Directory.Exists(exportFolderPath))
            {
                Directory.CreateDirectory(exportFolderPath);
            }

            //export
            Debug.Log(string.Format("Export base folder:{0} export file:{1}", baseFolderPath, outputPath));
            var assetPaths = GetExportAssetPaths(baseFolderPath);
            var assetList  = "# 対象アセット" + System.Environment.NewLine + String.Join(System.Environment.NewLine, assetPaths);

            Debug.Log(assetList);
            result.log += assetList + System.Environment.NewLine;
            try
            {
                if (!forceExport && File.Exists(outputPath))
                {
                    throw new IOException("既にファイルが存在しています。:" + outputPath);
                }
                AssetDatabase.ExportPackage(assetPaths, outputPath);
                //SHA-1 string rename
                if (fileName.IndexOf("{SHA-1}") != -1)
                {
                    var hash    = GetHash(outputPath);
                    var newPath = exportFolderPath + Path.AltDirectorySeparatorChar + fileName.Replace("{SHA-1}", hash);
                    if (forceExport || !File.Exists(newPath))
                    {
                        File.Move(outputPath, newPath);
                    }
                    else
                    {
                        throw new IOException("リネーム先のファイルが存在しています。");
                    }
                    outputPath = newPath;
                }
                result.log           += "以下のunitypackageをエクスポートしました。" + System.Environment.NewLine + outputPath;
                result.exportFilePath = outputPath;
                result.exportResult   = true;
            }
            catch (Exception e)
            {
                Debug.LogError(e);
                result.log         += "エクスポート中に問題が発生しました。:" + e.Message + System.Environment.NewLine;
                result.exportResult = false;
            }
            return(result);
        }
        /// <summary>
        /// 指定したベースフォルダをunitypackageにエクスポートする。
        /// </summary>
        /// <param name="baseFolderPath">ベースフォルダパス</param>
        /// <param name="setting">エクスポートに使用する設定</param>
        /// <param name="forceExport">ファイルが既に存在していた場合に上書きするかどうか</param>
        /// <returns>エクスポート結果</returns>
        public static ExportResult Export(string baseFolderPath, ExportSetting setting, bool forceExport = false)
        {
            if (setting == null)
            {
                throw new ArgumentNullException("Argument `setting` is null.");
            }
            if (baseFolderPath == null)
            {
                throw new ArgumentNullException("Argument `baseFolderPath` is null.");
            }
            var exportFolderPath = setting.ExportFolderPath;

            if (string.IsNullOrEmpty(exportFolderPath) || !exportFolderPath.StartsWith("Assets"))
            {
                throw new ArgumentNullException("Invalid export folder path:" + exportFolderPath);
            }

            var fileName   = string.IsNullOrEmpty(setting.fileNameFormat) ? "export.unitypackage" : setting.fileNameFormat;
            var outputPath = exportFolderPath + Path.AltDirectorySeparatorChar + fileName;
            var result     = new ExportResult();

            //check export folder path
            if (!Directory.Exists(exportFolderPath))
            {
                Directory.CreateDirectory(exportFolderPath);
            }

            //export
            Debug.Log(string.Format("Export base folder:{0} export file:{1}", baseFolderPath, outputPath));
            var assetPaths = GetExportAssetPaths(baseFolderPath);
            var assetList  = "# " + LocalizedMessage.Get("Exporter.TargetAsset") + System.Environment.NewLine + String.Join(System.Environment.NewLine, assetPaths);

            Debug.Log(assetList);
            result.log += assetList + System.Environment.NewLine;
            try
            {
                if (!forceExport && File.Exists(outputPath))
                {
                    throw new IOException(LocalizedMessage.Get("Exporter.DestinationAlreadyExists", outputPath));
                }
                AssetDatabase.ExportPackage(assetPaths, outputPath);
                if (new[] { "{SHA-1}", "{ID}", "{DATETIME}" }.Any(placeholder => fileName.Contains(placeholder)))
                {
                    var newPath = exportFolderPath + Path.AltDirectorySeparatorChar + fileName
                                  .Replace("{SHA-1}", GetHash(outputPath))
                                  .Replace("{ID}", Path.GetFileName(baseFolderPath))
                                  .Replace("{DATETIME}", DateTimeOffset.Now.ToString("yyyyMMddTHHmmsszzz").Replace(":", "")); // ファイルシステムに依っては「:」を使用できない
                    if (forceExport || !File.Exists(newPath))
                    {
                        File.Move(outputPath, newPath);
                    }
                    else
                    {
                        throw new IOException(LocalizedMessage.Get("Exporter.RenameDestinationAlreadyExists"));
                    }
                    outputPath = newPath;
                }
                result.log           += LocalizedMessage.Get("Exporter.Succeeded") + System.Environment.NewLine + outputPath;
                result.exportFilePath = outputPath;
                result.exportResult   = true;
            }
            catch (Exception e)
            {
                Debug.LogError(e);
                result.log         += LocalizedMessage.Get("Exporter.Failed", e.Message) + System.Environment.NewLine;
                result.exportResult = false;
            }
            return(result);
        }