public static void Import(DefaultAsset defaultAsset, ImportSettings settings) { var path = AssetDatabase.GetAssetPath(defaultAsset); var context = new ImportContext { // file = file, settings = settings, fileDirectory = Path.GetDirectoryName(path), fileName = Path.GetFileName(path), fileNameNoExt = Path.GetFileNameWithoutExtension(path) }; ImportStage(context, Stage.LoadFile); context.file = ASEParser.Parse(File.ReadAllBytes(path)); context.atlasPath = Path.Combine(settings.atlasOutputDirectory, context.fileNameNoExt + ".png"); if (settings.controllerPolicy == AnimControllerOutputPolicy.CreateOrOverride) { context.animControllerPath = settings.animControllerOutputPath + "/" + settings.baseName + ".controller"; } context.animClipDirectory = settings.clipOutputDirectory; // Create paths in advance Directory.CreateDirectory(settings.atlasOutputDirectory); Directory.CreateDirectory(context.animClipDirectory); if (context.animControllerPath != null) { Directory.CreateDirectory(Path.GetDirectoryName(context.animControllerPath)); } // ImportStage(context, Stage.GenerateAtlas); AtlasGenerator.GenerateAtlas(context); ImportStage(context, Stage.GenerateClips); GenerateAnimClips(context); ImportStage(context, Stage.GenerateController); GenerateAnimController(context); ImportStage(context, Stage.InvokeMetaLayerProcessor); context.file.layers .Where(layer => layer.type == LayerType.Meta) .ToList() .ForEach(layer => { MetaLayerProcessor processor; layerProcessors.TryGetValue(layer.actionName, out processor); if (processor == null) { Debug.LogWarning(string.Format("No processor for meta layer {0}", layer.layerName)); } else { processor.Process(context, layer); } }); ImportEnd(context); }
public static void Import(DefaultAsset defaultAsset, ImportSettings settings) { var path = AssetDatabase.GetAssetPath(defaultAsset); var context = new ImportContext { // file = file, settings = settings, fileDirectory = Path.GetDirectoryName(path), fileName = Path.GetFileName(path), fileNameNoExt = Path.GetFileNameWithoutExtension(path) }; try { ImportStage(context, Stage.LoadFile); context.file = ASEParser.Parse(File.ReadAllBytes(path)); if (settings.controllerPolicy != AnimControllerOutputPolicy.Skip) { context.animControllerPath = settings.animControllerOutputPath + "/" + context.fileNameNoExt + ".controller"; } context.animClipDirectory = settings.clipOutputDirectory; context.prefabDirectory = Path.Combine(settings.prefabsDirectory, context.fileNameNoExt + "_Origin.prefab"); // Create paths in advance Directory.CreateDirectory(settings.atlasOutputDirectory); Directory.CreateDirectory(context.animClipDirectory); if (context.animControllerPath != null) { Directory.CreateDirectory(Path.GetDirectoryName(context.animControllerPath)); } if (settings.generatePrefab) { Directory.CreateDirectory(settings.prefabsDirectory); } ImportStage(context, Stage.GenerateAtlas); foreach (var group in context.file.name2Group.Values) { if (group.contentLayers.Count == 0) { continue; } string atlasPath = Path.Combine(settings.atlasOutputDirectory, context.fileNameNoExt + "_" + group.name + ".png"); var sprites = AtlasGenerator.GenerateAtlas(context, group.contentLayers, atlasPath); context.mapSprite.Add(group.name, sprites); } ImportStage(context, Stage.GenerateClips); GenerateAnimClips(context); ImportStage(context, Stage.GenerateController); GenerateAnimController(context); ImportStage(context, Stage.GeneratePrefab); GeneratePrefab(context); ImportStage(context, Stage.InvokeMetaLayerProcessor); MetaProcess(context); } catch (Exception e) { Debug.LogException(e); EditorUtility.ClearProgressBar(); } ImportEnd(context); }
/// <summary> /// /// </summary> /// <param name="path"></param> /// <param name="initTagName">null for random tag</param> /// <returns></returns> public static AsepriteAnimation Import(string path, string initTagName) { var context = new ImportContext { // file = file, fileDirectory = Path.GetDirectoryName(path), fileName = Path.GetFileName(path), fileNameNoExt = Path.GetFileNameWithoutExtension(path) }; // parse file context.file = ASEParser.Parse(System.IO.File.ReadAllBytes(path)); // gen default tag for no tag file GenerateFrameTagForNoTagFile(context); // generate sprite context.generatedSprites = AtlasGenerator.GenerateAtlas(context, context.file.layers.Where(it => it.type == LayerType.Content).ToList()); // generate animation clips GenerateAnimClips(context); // process other ... context.file.layers .Where(layer => layer.type == LayerType.Meta) .Select(layer => { MetaLayerProcessor processor; layerProcessors.TryGetValue(layer.actionName, out processor); return(new LayerAndProcessor { layer = layer, processor = processor }); }) .OrderBy(it => it.processor != null ? it.processor.executionOrder : 0) .ToList() .ForEach(it => { var layer = it.layer; var processor = it.processor; if (processor != null) { processor.Process(context, layer); } else { Log.Warnning($"No processor for meta layer {layer.layerName} \t {path}"); } }); Dictionary <string, AnimationClip> dict = new Dictionary <string, AnimationClip>(); foreach (var kv in context.generatedClips) { dict[kv.Key.name] = kv.Value; } Dictionary <string, AnimationClip> revsrse_dict = new Dictionary <string, AnimationClip>(); foreach (var kv in context.generatedReversedClips) { revsrse_dict[kv.Key.name] = kv.Value; } if (initTagName == null) { initTagName = dict.Keys.FirstOrDefault(); } return(new AsepriteAnimation(dict, revsrse_dict, context.file.width, context.file.height, initTagName)); }
public static void Import(string path, ImportSettings settings) { // var path = AssetDatabase.GetAssetPath(defaultAsset); var context = new ImportContext { // file = file, settings = settings, fileDirectory = Path.GetDirectoryName(path), fileName = Path.GetFileName(path), fileNameNoExt = Path.GetFileNameWithoutExtension(path) }; try { ImportStage(context, Stage.LoadFile); context.file = ASEParser.Parse(File.ReadAllBytes(path)); context.atlasPath = Path.Combine(settings.atlasOutputDirectory, context.fileNameNoExt + ".png"); if (settings.controllerPolicy == AnimControllerOutputPolicy.CreateOrOverride) { context.animControllerPath = settings.animControllerOutputPath + "/" + settings.baseName + ".controller"; } context.animClipDirectory = settings.clipOutputDirectory; // Create paths in advance Directory.CreateDirectory(settings.atlasOutputDirectory); Directory.CreateDirectory(context.animClipDirectory); if (context.animControllerPath != null) { Directory.CreateDirectory(Path.GetDirectoryName(context.animControllerPath)); } // ImportStage(context, Stage.GenerateAtlas); context.generatedSprites = AtlasGenerator.GenerateAtlas(context, context.file.layers.Where(it => it.type == LayerType.Content).ToList(), context.atlasPath); ImportStage(context, Stage.GenerateClips); GenerateAnimClips(context); ImportStage(context, Stage.GenerateController); GenerateAnimController(context); ImportStage(context, Stage.InvokeMetaLayerProcessor); context.file.layers .Where(layer => layer.type == LayerType.Meta) .Select(layer => { MetaLayerProcessor processor; layerProcessors.TryGetValue(layer.actionName, out processor); return(new LayerAndProcessor { layer = layer, processor = processor }); }) .OrderBy(it => it.processor != null ? it.processor.executionOrder : 0) .ToList() .ForEach(it => { var layer = it.layer; var processor = it.processor; if (processor != null) { processor.Process(context, layer); } else { Debug.LogWarning(string.Format("No processor for meta layer {0}", layer.layerName)); } }); } catch (Exception e) { Debug.LogException(e); } ImportEnd(context); }
public static void Import(DefaultAsset defaultAsset, ImportSettings settings) { var path = AssetDatabase.GetAssetPath(defaultAsset); var context = new ImportContext { // file = file, settings = settings, fileDirectory = Path.GetDirectoryName(path), fileName = Path.GetFileName(path), fileNameNoExt = Path.GetFileNameWithoutExtension(path), animData = ScriptableObject.CreateInstance <AnimData>(), }; try { // context.animData.data = new Dictionary<string, Dictionary<string, List<FrameData>>>(); ImportStage(context, Stage.LoadFile); context.file = ASEParser.Parse(File.ReadAllBytes(path)); context.atlasPath = Path.Combine(settings.atlasOutputDirectory, context.fileNameNoExt + ".png"); if (settings.controllerPolicy == AnimControllerOutputPolicy.CreateOrOverride) { context.animControllerPath = settings.animControllerOutputPath + "/" + settings.baseName + ".controller"; } context.animClipDirectory = settings.clipOutputDirectory; context.animDataDirectory = settings.dataOutputDirectory; // Create paths in advance Directory.CreateDirectory(settings.atlasOutputDirectory); Directory.CreateDirectory(context.animClipDirectory); Directory.CreateDirectory(context.animDataDirectory); if (context.animControllerPath != null) { Directory.CreateDirectory(Path.GetDirectoryName(context.animControllerPath)); } ImportStage(context, Stage.GenerateAtlas); context.generatedSprites = AtlasGenerator.GenerateAtlas(context, context.file.layers.Where(it => it.type == LayerType.Content).ToList(), context.atlasPath); ImportStage(context, Stage.GenerateClips); GenerateAnimClips(context); ImportStage(context, Stage.GenerateController); GenerateAnimController(context); ImportStage(context, Stage.InvokeMetaLayerProcessor); context.file.layers .Where(layer => layer.type == LayerType.Meta) .Select(layer => { MetaLayerProcessor processor; layerProcessors.TryGetValue(layer.actionName, out processor); return(new LayerAndProcessor { layer = layer, processor = processor }); }) .OrderBy(it => it.processor != null ? it.processor.executionOrder : 0) .ToList() .ForEach(it => { var layer = it.layer; var processor = it.processor; if (processor != null) { processor.Process(context, layer); } else { Debug.LogWarning(string.Format("No processor for meta layer {0}", layer.layerName)); } }); // calc num frames for each animation, save to animData foreach (var tag in context.file.frameTags) { string animName = tag.name; int numFrames = tag.to - tag.from + 1; if (context.animData.animDict.ContainsKey(animName)) { context.animData.animDict[animName].numFrames = numFrames; } else { context.animData.animDict.Add(animName, new AnimList { numFrames = numFrames, frameDict = new FrameDictionary(), }); } } // save each frame's pivot and dimensions in animData foreach (var tag in context.file.frameTags) { string animName = tag.name; var pivotDataList = new FrameDataList { frames = new List <FrameData>() }; var dimsDataList = new FrameDataList { frames = new List <FrameData>() }; for (int i = tag.from, j = 0; i <= tag.to; i++, j++) { pivotDataList.frames.Add(new FrameData { frame = j, coords = new List <Vector2> { context.spritePivots[i] } }); dimsDataList.frames.Add(new FrameData { frame = j, coords = new List <Vector2> { context.spriteDimensions[i] } }); } context.animData.animDict[animName].frameDict.Add("pivot", pivotDataList); context.animData.animDict[animName].frameDict.Add("dims", dimsDataList); } /* * var importer = AssetImporter.GetAtPath(context.atlasPath) as TextureImporter; * var spriteSheet = importer.spritesheet; * Debug.Log("== SPRITESHEET =="); * Debug.Log($"{spriteSheet[0].rect}"); * Debug.Log($"{spriteSheet[0].pivot}"); * Debug.Log(ObjectDumper.Dump(context.spriteDimensions)); * Debug.Log(ObjectDumper.Dump(context.spritePivots)); */ ImportStage(context, Stage.SaveAnimData); var filePath = context.animDataDirectory + "/" + context.settings.baseName + " data.asset"; AnimData asset = (AnimData)AssetDatabase.LoadAssetAtPath(filePath, typeof(AnimData)); if (!asset) { asset = ScriptableObject.CreateInstance <AnimData>(); asset = context.animData; asset.ppu = context.settings.ppu; asset.pixelOrigin = context.settings.pixelOrigin.ToString(); AssetDatabase.CreateAsset(asset, filePath); } else { asset.ppu = context.settings.ppu; asset.pixelOrigin = context.settings.pixelOrigin.ToString(); // merge any new animations with old (and overwrite matching old) foreach (KeyValuePair <string, AnimList> item in context.animData.animDict) { asset.animDict[item.Key] = item.Value; } EditorUtility.SetDirty(asset); AssetDatabase.SaveAssets(); } } catch (Exception e) { Debug.LogException(e); } ImportEnd(context); }