예제 #1
0
        private static void Import(IAkyuiImportSettings settings, IAkyuiLoader akyuiLoader, AkyuiLogger logger, IAkyuiProgress progress)
        {
            var stopWatch = Stopwatch.StartNew();

            CheckVersion(akyuiLoader);

            Settings = settings;
            using (Disposable.Create(() => Settings = null))
            {
                var pathGetter         = new PathGetter(settings, akyuiLoader.LayoutInfo.Name);
                var prevMetaGameObject = AssetDatabase.LoadAssetAtPath <GameObject>(pathGetter.MetaSavePath);
                var prevMeta           = prevMetaGameObject != null?prevMetaGameObject.GetComponent <AkyuiMeta>() : null;

                var prevAssets = prevMeta != null ? prevMeta.assets : new Object[] { };

                if (!settings.ReimportLayout && prevMeta != null && prevMeta.hash == akyuiLoader.LayoutInfo.Hash)
                {
                    logger.Log("Skip", ("hash", akyuiLoader.LayoutInfo.Hash));
                    return;
                }

                var assets = ImportAssets(settings, akyuiLoader, pathGetter, logger, progress);
                var(gameObject, hash) = ImportLayout(settings, akyuiLoader, pathGetter, logger);
                DeleteUnusedAssets(prevAssets, assets, logger);

                var metaGameObject = new GameObject(akyuiLoader.LayoutInfo.Name);
                gameObject.transform.SetParent(metaGameObject.transform);
                var akyuiMeta = metaGameObject.AddComponent <AkyuiMeta>();
                akyuiMeta.hash     = hash;
                akyuiMeta.root     = gameObject;
                akyuiMeta.assets   = assets;
                akyuiMeta.userData = akyuiLoader.LayoutInfo.UserData.Select(x => new AkyuiMetaUserData {
                    key = x.Key, value = x.Value
                }).ToArray();

                AkyuiEditorUtil.CreateDirectory(Path.GetDirectoryName(pathGetter.PrefabSavePath));
                AkyuiEditorUtil.CreateDirectory(Path.GetDirectoryName(pathGetter.MetaSavePath));

                PrefabUtility.SaveAsPrefabAssetAndConnect(gameObject, pathGetter.PrefabSavePath, InteractionMode.AutomatedAction);
                PrefabUtility.SaveAsPrefabAsset(metaGameObject, pathGetter.MetaSavePath);

                foreach (var trigger in settings.Triggers)
                {
                    trigger.OnPostprocessFile(akyuiLoader, pathGetter);
                }

                Object.DestroyImmediate(metaGameObject);
            }

            stopWatch.Stop();
            logger.Log($"Import Finish", ("time", $"{stopWatch.Elapsed.TotalSeconds:0.00}s"));
        }
예제 #2
0
        public static void Import(IAkyuiImportSettings settings, string[] filePaths)
        {
            var akyuiLoaders = filePaths
                               .Select(x => (IAkyuiLoader) new AkyuiLoader(x))
                               .ToArray();

            Import(settings, akyuiLoaders);

            foreach (var akyuiLoader in akyuiLoaders)
            {
                akyuiLoader.Dispose();
            }
        }
예제 #3
0
        public PathGetter(IAkyuiImportSettings settings, string fileName)
        {
            var assetOutputDirectoryPath = settings.AssetOutputDirectoryPath.Replace("{name}", fileName);

            if (!assetOutputDirectoryPath.EndsWith("/"))
            {
                assetOutputDirectoryPath += "/";
            }
            AssetOutputDirectoryPath = assetOutputDirectoryPath;

            PrefabSavePath    = settings.PrefabOutputPath.Replace("{name}", fileName) + ".prefab";
            MetaSavePath      = settings.MetaOutputPath.Replace("{name}", fileName) + ".prefab";
            FontDirectoryPath = settings.FontDirectoryPath;
        }
예제 #4
0
 private static (GameObject, long Hash) ImportLayout(IAkyuiImportSettings settings, IAkyuiLoader akyuiLoader, PathGetter pathGetter, AkyuiLogger logger)
 {
     using (logger.SetCategory("Layout"))
     {
         logger.Log($"Import Start");
         var layoutInfo = akyuiLoader.LayoutInfo;
         var triggers   = settings.Triggers.Select(x => (IAkyuiGenerateTrigger)x).ToArray();
         var(gameObject, hash) = AkyuiGenerator.GenerateGameObject(new EditorAssetLoader(pathGetter, logger, settings.Triggers), layoutInfo, triggers);
         foreach (var trigger in settings.Triggers)
         {
             trigger.OnPostprocessPrefab(akyuiLoader, ref gameObject);
         }
         logger.Log($"Import Finish");
         return(gameObject, hash);
     }
 }
예제 #5
0
        private static (GameObject, long Hash) ImportLayout(IAkyuiImportSettings settings, IAkyuiLoader akyuiLoader, PathGetter pathGetter, AkyuiLogger logger)
        {
            var stopWatch = Stopwatch.StartNew();

            using (logger.SetCategory("Layout"))
            {
                var layoutInfo = akyuiLoader.LayoutInfo;
                var triggers   = settings.Triggers.Select(x => (IAkyuiGenerateTrigger)x).ToArray();
                var(gameObject, hash) = AkyuiGenerator.GenerateGameObject(new EditorAssetLoader(pathGetter, logger, settings.Triggers), layoutInfo, triggers);
                foreach (var trigger in settings.Triggers)
                {
                    trigger.OnPostprocessPrefab(akyuiLoader, ref gameObject);
                }
                logger.Log($"Import Finish", ("time", $"{stopWatch.Elapsed.TotalSeconds:0.00}s"));
                return(gameObject, hash);
            }
        }
예제 #6
0
        public static void Import(IAkyuiImportSettings settings, IAkyuiLoader[] loaders)
        {
            var logger = new AkyuiLogger("Akyui");

            using (var progressBar = new AkyuiProgressBar("Akyui"))
            {
                progressBar.SetTotal(loaders.Length);
                foreach (var loader in loaders)
                {
                    using (logger.SetCategory(loader.LayoutInfo.Name))
                        using (var progress = progressBar.TaskStart($"Importing {loader.LayoutInfo.Name}"))
                        {
                            Import(settings, loader, logger, progress);
                        }
                }

                AssetDatabase.SaveAssets();
                AssetDatabase.Refresh();
            }
        }
예제 #7
0
        private static void ImportAsset(IAsset asset, string savePath, string saveFullPath, byte[] bytes, Dictionary <string, object> userData, IAkyuiImportSettings importSettings, AkyuiLogger logger)
        {
            PostProcessImportAsset.ProcessingFile = savePath.ToUniversalPath();
            PostProcessImportAsset.Asset          = asset;
            PostProcessImportAsset.UserData       = userData;
            PostProcessImportAsset.Triggers       = importSettings.Triggers;

            using (Disposable.Create(() =>
            {
                PostProcessImportAsset.ProcessingFile = null;
                PostProcessImportAsset.Asset = null;
                PostProcessImportAsset.UserData = null;
                PostProcessImportAsset.Triggers = null;
            }))
            {
                if (asset is SpriteAsset)
                {
                    File.WriteAllBytes(saveFullPath, bytes);
                    AssetDatabase.ImportAsset(savePath);
                    return;
                }
            }

            logger.Error($"Unknown asset type {asset}");
        }
예제 #8
0
        private static Object[] ImportAssets(IAkyuiImportSettings settings, IAkyuiLoader akyuiLoader, PathGetter pathGetter, AkyuiLogger logger, IAkyuiProgress progress)
        {
            var stopWatch = Stopwatch.StartNew();

            using (logger.SetCategory("Assets"))
            {
                var assets = new List <Object>();
                var unityAssetsParentPath = Path.GetDirectoryName(Application.dataPath) ?? "";

                var assetOutputDirectoryFullPath = Path.Combine(unityAssetsParentPath, pathGetter.AssetOutputDirectoryPath);
                if (!Directory.Exists(assetOutputDirectoryFullPath))
                {
                    Directory.CreateDirectory(assetOutputDirectoryFullPath);
                }

                var importAssetNames = new List <string>();
                var skipAssetNames   = new List <string>();

                var allAssets = akyuiLoader.AssetsInfo.Assets.ToList();
                foreach (var trigger in settings.Triggers)
                {
                    trigger.OnPreprocessAllAssets(akyuiLoader, ref allAssets);
                }

                progress.SetTotal(allAssets.Count);
                foreach (var tmp in allAssets)
                {
                    var asset = tmp;
                    using (progress.TaskStart(asset.FileName))
                    {
                        var savePath     = Path.Combine(pathGetter.AssetOutputDirectoryPath, asset.FileName);
                        var saveFullPath = Path.Combine(unityAssetsParentPath, savePath);

                        if (!settings.ReimportAsset && File.Exists(saveFullPath))
                        {
                            var import       = AssetImporter.GetAtPath(savePath);
                            var prevUserData = JsonSerializer.Deserialize <Dictionary <string, object> >(import.userData);
                            if (prevUserData["hash"].JsonLong() == asset.Hash)
                            {
                                skipAssetNames.Add(asset.FileName);
                                assets.Add(AssetDatabase.LoadAssetAtPath <Object>(import.assetPath));
                                continue;
                            }
                        }

                        var bytes    = akyuiLoader.LoadAsset(asset.FileName); // Hashチェック後に初めて呼ぶ
                        var userData = new Dictionary <string, object>();
                        userData["hash"] = asset.Hash;

                        if (asset is SpriteAsset)
                        {
                            var texture = new Texture2D(2, 2);
                            texture.LoadImage(bytes);

                            userData["source_width"]  = texture.width;
                            userData["source_height"] = texture.height;
                        }

                        foreach (var trigger in settings.Triggers)
                        {
                            trigger.OnPreprocessAsset(akyuiLoader, ref bytes, ref asset, ref userData);
                        }
                        ImportAsset(asset, savePath, saveFullPath, bytes, userData, settings, logger);
                        assets.Add(AssetDatabase.LoadAssetAtPath <Object>(savePath));
                        importAssetNames.Add(asset.FileName);
                    }
                }

                var importAssets = assets.ToArray();
                foreach (var trigger in settings.Triggers)
                {
                    trigger.OnPostprocessAllAssets(akyuiLoader, importAssets);
                }

                logger.Log($"Import Finish", ("import", importAssetNames.Count), ("skip", skipAssetNames.Count), ("time", $"{stopWatch.Elapsed.TotalSeconds:0.00}s"));

                return(importAssets);
            }
        }
예제 #9
0
        private static (GameObject, uint Hash, IDictionary <uint, GameObject> EidMap, ImportLayoutLog) ImportLayout(IAkyuiImportSettings settings, IAkyuiLoader akyuiLoader, PathGetter pathGetter, AkyuiLogger logger)
        {
            var stopWatch  = Stopwatch.StartNew();
            var layoutInfo = akyuiLoader.LayoutInfo;
            var triggers   = settings.Triggers.Select(x => (IAkyuiGenerateTrigger)x).ToArray();

            var(gameObject, hash, eidMap) = AkyuiGenerator.GenerateGameObject(new EditorAssetLoader(pathGetter, logger, settings.Triggers), layoutInfo, triggers);
            foreach (var trigger in settings.Triggers)
            {
                trigger.OnPostprocessPrefab(akyuiLoader, ref gameObject);
            }
            return(gameObject, hash, eidMap, new ImportLayoutLog {
                Time = stopWatch.Elapsed.TotalSeconds
            });
        }