Exemple #1
0
        private static (int Imported, int Skipped) ImportedArtboards(XdImportSettings xdSettings, AkyuiLogger logger, string xdFilePath, IAkyuiProgress progress, List <IAkyuiLoader> loaders)
        {
            logger.Log($"Xd Import Start");
            var file     = new XdFile(xdFilePath);
            var imported = 0;
            var skipped  = 0;

            var targets = new List <XdArtboard>();

            foreach (var artwork in file.Artworks)
            {
                if (artwork.Artboard.Children.Length == 0)
                {
                    continue;
                }
                var markForExport = artwork.Artboard.Children[0].Meta?.Ux?.MarkedForExport ?? false;
                if (!markForExport)
                {
                    continue;
                }
                targets.Add(artwork);
            }

            progress.SetTotal(targets.Count);
            foreach (var artwork in targets)
            {
                using (progress.TaskStart(artwork.Name))
                {
                    var akyuiXdObjectParsers = xdSettings.ObjectParsers ?? new AkyuiXdObjectParser[] { };
                    var akyuiXdGroupParsers  = xdSettings.GroupParsers ?? new AkyuiXdGroupParser[] { };
                    var triggers             = xdSettings.XdTriggers ?? new AkyuiXdImportTrigger[] { };

                    var name     = artwork.Name;
                    var xdHash   = FastHash.CalculateHash(JsonConvert.SerializeObject(artwork.Artboard) + JsonConvert.SerializeObject(artwork.Resources));
                    var userData = new Dictionary <string, string>
                    {
                        { "xd_hash", xdHash.ToString() }
                    };

                    var pathGetter         = new PathGetter(xdSettings, name);
                    var prevMetaGameObject = AssetDatabase.LoadAssetAtPath <GameObject>(pathGetter.MetaSavePath);
                    var prevMeta           = prevMetaGameObject != null?prevMetaGameObject.GetComponent <AkyuiMeta>() : null;

                    var prevMetaUserData = prevMeta != null?prevMeta.FindUserData("xd_hash") : null;

                    if (!xdSettings.ReimportLayout && !xdSettings.ReimportAsset && prevMetaUserData != null && prevMetaUserData.value == xdHash.ToString())
                    {
                        logger.Log("Skip", ("hash", xdHash));
                        skipped++;
                        continue;
                    }

                    loaders.Add(new XdAkyuiLoader(file, artwork, name, userData, akyuiXdObjectParsers, akyuiXdGroupParsers, triggers));
                    imported++;
                }
            }

            logger.Log($"Xd Import Finish", ("imported", imported), ("skipped", skipped));
            return(imported, skipped);
        }
Exemple #2
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 #3
0
        private static (int Imported, int Skipped) ImportedArtboards(XdImportSettings xdSettings, AkyuiLogger logger, string xdFilePath, IAkyuiProgress progress, List <IAkyuiLoader> loaders)
        {
            var stopWatch = Stopwatch.StartNew();
            var file      = new XdFile(xdFilePath);

            XdFile = file;
            using (Disposable.Create(() => XdFile = null))
            {
                var imported = 0;
                var skipped  = 0;

                var targets = new List <XdArtboard>();
                foreach (var artwork in file.Artworks)
                {
                    if (artwork.Artboard.Children.Length == 0)
                    {
                        continue;
                    }
                    var markForExport = artwork.Artboard.Children[0].Meta?.Ux?.MarkedForExport ?? false;
                    if (!markForExport)
                    {
                        continue;
                    }
                    targets.Add(artwork);
                }

                progress.SetTotal(targets.Count);

                var akyuiXdObjectParsers = xdSettings.ObjectParsers ?? new AkyuiXdObjectParser[] { };
                var akyuiXdGroupParsers  = xdSettings.GroupParsers ?? new AkyuiXdGroupParser[] { };
                var triggers             = xdSettings.XdTriggers ?? new AkyuiXdImportTrigger[] { };

                var expandTargets = new List <XdArtboard>();
                foreach (var artwork in targets)
                {
                    if (!artwork.HasParameter("expand"))
                    {
                        expandTargets.Add(artwork);
                        continue;
                    }

                    foreach (var child in artwork.Artboard.Children.SelectMany(x => x.Artboard.Children))
                    {
                        var childArtboardJson = new XdArtboardJson
                        {
                            Version   = artwork.Artboard.Version,
                            Artboards = artwork.Artboard.Artboards,
                            Resources = artwork.Artboard.Resources,
                            Children  = new[]
                            {
                                new XdArtboardChildJson
                                {
                                    Artboard = new XdArtboardChildArtboardJson
                                    {
                                        Children = new[] { child }
                                    }
                                }
                            }
                        };

                        var childArtboard = new XdArtboard($"{artwork.GetSimpleName()}{child.GetSimpleName()}", artwork.Manifest, childArtboardJson, artwork.Resources, artwork.Hash);
                        expandTargets.Add(childArtboard);
                    }
                }

                foreach (var artwork in expandTargets)
                {
                    using (progress.TaskStart(artwork.Name))
                        using (var _ = logger.SetCategory(artwork.Name))
                        {
                            var name   = artwork.Name.ToSafeString();
                            var xdHash = artwork.Hash;

                            var userData = new Dictionary <string, string>
                            {
                                { "xd_hash", xdHash.ToString() }
                            };

                            var pathGetter         = new PathGetter(xdSettings, name);
                            var prevMetaGameObject = AssetDatabase.LoadAssetAtPath <GameObject>(pathGetter.MetaSavePath);
                            var prevMeta           = prevMetaGameObject != null?prevMetaGameObject.GetComponent <AkyuiMeta>() : null;

                            var prevMetaUserData = prevMeta != null?prevMeta.FindUserData("xd_hash") : null;

                            if (!xdSettings.ReimportLayout && !xdSettings.ReimportAsset && prevMetaUserData != null && prevMetaUserData.value == xdHash.ToString())
                            {
                                logger.Log("Skip", ("Name", artwork.Name), ("Hash", xdHash));
                                skipped++;
                                continue;
                            }

                            loaders.Add(new XdAkyuiLoader(file, artwork, name, userData, akyuiXdObjectParsers, akyuiXdGroupParsers, triggers));
                            imported++;
                        }
                }

                stopWatch.Stop();
                logger.Log($"Xd Parse Finish", ("ImportArtboard", imported), ("SkipArtboard", skipped), ("Time", $"{stopWatch.Elapsed.TotalSeconds:0.00}s"));
                return(imported, skipped);
            }
        }