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); Import(path, settings); }
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); }