Exemple #1
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();
            }
        }
Exemple #2
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"));
        }
Exemple #3
0
 public EditorAssetLoader(PathGetter pathGetter, AkyuiLogger logger, IAkyuiImportTrigger[] triggers)
 {
     _pathGetter = pathGetter;
     _logger     = logger;
     _triggers   = triggers;
 }
Exemple #4
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}");
        }
Exemple #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);
            }
        }
Exemple #6
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);
            }
        }
Exemple #7
0
        private static string[] DeleteUnusedAssets(Object[] prevAssets, Object[] newAssets, AkyuiLogger logger)
        {
            var deletedUnusedAssets = new List <string>();

            foreach (var prevAsset in prevAssets)
            {
                if (prevAsset == null)
                {
                    continue;
                }
                if (newAssets.Any(x => x.name == prevAsset.name))
                {
                    continue;
                }

                var prevAssetPath = AssetDatabase.GetAssetPath(prevAsset);
                deletedUnusedAssets.Add(Path.GetFileName(prevAssetPath));
                AssetDatabase.DeleteAsset(prevAssetPath);
            }

            return(deletedUnusedAssets.ToArray());
        }
Exemple #8
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);
     }
 }
Exemple #9
0
        private static void DeleteUnusedAssets(Object[] prevAssets, Object[] newAssets, AkyuiLogger logger)
        {
            var deletedUnusedAssets = new List <string>();

            foreach (var prevAsset in prevAssets)
            {
                if (prevAsset == null)
                {
                    continue;
                }
                if (newAssets.Any(x => x.name == prevAsset.name))
                {
                    continue;
                }

                var prevAssetPath = AssetDatabase.GetAssetPath(prevAsset);
                deletedUnusedAssets.Add(Path.GetFileName(prevAssetPath));
                AssetDatabase.DeleteAsset(prevAssetPath);
            }

            if (deletedUnusedAssets.Count > 0)
            {
                logger.Log($"Delete unused asset", ("assets", string.Join(", ", deletedUnusedAssets)));
            }
        }
Exemple #10
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
            });
        }