首页 > 学院 > 开发设计 > 正文

使用EasySpritesAnimation一键生成动画预制

2019-11-09 14:08:21
字体:
来源:转载
供稿:网友

本文所用到的插件下载地址: http://www.manew.com/forum.php?mod=viewthread&tid=100227&page=1#pid1322573

使用此脚本即可在unity中选中多个动画文件夹右键自动生成为预制 文件夹层级如下图: 这里写图片描述

public class AnimPRefabCreatorNew{ private static string ProjectPath = "D:/Project/Desinty/Engineer/Desinty/Desinty"; private static string AnimationPath = "Animation"; private static string AnimationControllerPath = "Animation"; private static string PrefabPath = "Assets/Resources/RolePrefabs"; private static string towerAmmoName = "飞行物"; private static string spellRenderLayer = "mapSpell"; // 1 private static string roleRenderLayer = "mapRole"; // 2 private static string towerRenderLayer = "mapTower"; // 3 // 法术 >人 >塔 private static string animationAssetPath = ""; private static void Create(DirectoryInfo dictorys, string renderLayer, string prefabName = "") { string rp = dictorys.FullName; rp = rp.Replace("//", "/"); rp = rp.Replace(application.dataPath, "Assets"); List<AnimationClip> clips = new List<AnimationClip>(); EditorUtility.DisplayProgressBar("create units...", dictorys.Name, 0.5f); SpriteAnimationAsset prefabAsset = null; BuildAnimationAsset(rp, dictorys, animationAsset => { foreach (DirectoryInfo directoryInfo in dictorys.GetDirectories()) { // 若文件夹名字是飞行物(单位的子弹) 跳过 子弹预制需要单独生成 if (directoryInfo.Name.Equals(towerAmmoName) || directoryInfo.Name.Equals(AnimationPath)) { continue; } var sprites = GetAllSprites(directoryInfo); var animationProperty = BuildAnimationProperty(animationAsset.animations, directoryInfo.Name.StrToPinyin()); BuildAnimation(animationProperty, sprites); } //BuildAnimationProperty(animationAsset.animations, "Normal"); prefabAsset = animationAsset; EditorUtility.DisplayProgressBar("create Animation...", dictorys.Name, 0.8f); }); BuildPrefab(dictorys, prefabAsset, renderLayer, prefabName); EditorUtility.DisplayProgressBar("create Unit Prefab...", dictorys.Name, 1f); Debug.LogFormat("buildPrefab {0} Done!", prefabName); AssetDatabase.Refresh(); EditorUtility.ClearProgressBar(); } [MenuItem("Assets/生成Prefab到自身路径", false, 3)] static void selectedBuildtoMyselfPath() { UnityEngine.Object[] selectObjs = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Assets); foreach (UnityEngine.Object selectObj in selectObjs) { string path = AssetDatabase.GetAssetPath(selectObj); PrefabPath = path; path = path.Remove(0, 6); DirectoryInfo baseDictory = new DirectoryInfo(Application.dataPath + path); Create(baseDictory, ""); } } [MenuItem("Assets/生成Prefab使用父级名字到BulletPrefabs", false, 4)] static void selectedBuildtoParentPath() { PrefabPath = "Assets/Resources/BulletPrefabs"; UnityEngine.Object[] selectObjs = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Assets); foreach (UnityEngine.Object selectObj in selectObjs) { string path = AssetDatabase.GetAssetPath(selectObj); path = path.Remove(0, 6); DirectoryInfo baseDictory = new DirectoryInfo(Application.dataPath + path); string prefabName = baseDictory.Parent.Name + "_bullet"; // 每个子弹文件夹在该角色动画文件夹下 prefabName = baseDictory.Parent.Parent.Name.Remove(baseDictory.Parent.Parent.Name.IndexOf("_"), baseDictory.Parent.Parent.Name.Length - baseDictory.Parent.Parent.Name.IndexOf("_")) + "_" + prefabName; Create(baseDictory, spellRenderLayer, prefabName); } } /// <summary> /// 所有图片文件修改为Sprite类型 /// </summary> /// <param name="path"></param> static void NormalizeTextureType(string path) { TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter; if (textureImporter.textureType != TextureImporterType.Sprite) { textureImporter.textureType = TextureImporterType.Sprite; TextureImporterSettings settings = new TextureImporterSettings(); textureImporter.ReadTextureSettings(settings); textureImporter.SetTextureSettings(settings); AssetDatabase.ImportAsset(path); } } /// <summary> /// 遍历文件夹内所有Sprite类型文件 /// 创建单个动画资源文件 /// </summary> /// <param name="path"></param> /// <param name="dictorys"></param> /// <returns></returns> private static void BuildAnimationAsset(string path, DirectoryInfo dictorys,System.Action<SpriteAnimationAsset> call ) { // StrToPinyin自定义命名空间 将字符串转换为拼音 string animationName = dictorys.Name.StrToPinyin(); DirectoryInfo direction = new DirectoryInfo(path); if (!string.IsNullOrEmpty(direction.Extension)) { // 当前项不是文件夹 return; } CreateAnimationAsset(path, animationName, call); } /// <summary> /// 获取该文件夹下所有Sprite /// </summary> /// <param name="directoryInfo"></param> /// <returns></returns> static List<Sprite> GetAllSprites(DirectoryInfo directoryInfo) { var files = directoryInfo.GetFiles("*", SearchOption.AllDirectories); var sprites = new List<Sprite>(); for (int i = 0; i < files.Length; i++) { // 切换为unity项目相对路径 var selFilePath = files[i].FullName.Replace("//", "/").Replace(Application.dataPath, "Assets"); TextureImporter textureImporter = AssetImporter.GetAtPath(selFilePath) as TextureImporter; if (files[i].Name.EndsWith(".meta") || textureImporter == null) continue; NormalizeTextureType(selFilePath); selFilePath = selFilePath.Remove(selFilePath.Length - 4, 4); // 移除.png后缀 selFilePath = selFilePath.Replace("Assets/Resources/", ""); Sprite sprite = Resources.Load<Sprite>(selFilePath); if (sprite != null) sprites.Add(sprite); } sprites.Sort((x1, x2) => { var XValue = int.Parse(System.Text.RegularExpressions.Regex.Match(x1.name, @"/d+").Value); var YValue = int.Parse(System.Text.RegularExpressions.Regex.Match(x2.name, @"/d+").Value); if (XValue == YValue) { return 0; } else if (XValue < YValue) { return -1; //返回-1,X排在Y的前面 } else { return 1; //返回1,X排在Y的后面 } }); return sprites; } /// <summary> /// 添加动画结构块 /// </summary> /// <param name="dataList"></param> /// <param name=""></param> static SpriteAnimationData BuildAnimationProperty(List<SpriteAnimationData> dataList,string animationName) { var animationData = new SpriteAnimationData() { newFramesTime = 0.1f, speedRatio = 1, name = animationName, loop = SpriteAnimationLoopMode.NOLOOP, frameToLoop = 0, selectedIndex = -1 }; dataList.Add(animationData); if (IsAnimationLoop(animationName)) animationData.loop = SpriteAnimationLoopMode.LOOPTOSTART; else animationData.loop = SpriteAnimationLoopMode.NOLOOP; //AssetDatabase.SaveAssets(); return animationData; } static void BuildPrefab(DirectoryInfo dictorys, SpriteAnimationAsset animationAsset, string renderLayer, string prefabName = "") { //生成Prefab 添加一张预览用的Sprite FileInfo images = dictorys.GetDirectories()[1].GetFiles("*.png")[0]; GameObject go = new GameObject(); go.name = !string.IsNullOrEmpty(prefabName) ? prefabName.StrToPinyin() : dictorys.Name.StrToPinyin(); // 添加控制动画播放脚本(自定义类) go.AddComponent<AnimController>(); SpriteRenderer spriteRender = go.AddComponent<SpriteRenderer>(); string imgPath = images.FullName.Replace("//", "/"); //imgPath = imgPath.Substring(imgPath.IndexOf("KingDomRush/")); imgPath = imgPath.Replace(Application.dataPath + "/Resources/", ""); imgPath = imgPath.Remove(imgPath.Length - 4, 4); spriteRender.sprite = Resources.Load<Sprite>(imgPath); if (!string.IsNullOrEmpty(renderLayer)) spriteRender.sortingLayerName = renderLayer; var spriteAnimation = go.AddComponent<SpriteAnimation>(); spriteAnimation.assets.Add(animationAsset); // 默认状态设置为Normal spriteAnimation.SetCurrentAnimation(0); go.AddComponent<BoxCollider2D>().isTrigger = true; go.AddComponent<Rigidbody2D>().isKinematic = true; var prefab = PrefabUtility.CreatePrefab(PrefabPath + "/" + go.name + ".prefab", go, ReplacePrefabOptions.ReplaceNameBased); Object.DestroyImmediate(go); AssetDatabase.SaveAssets(); } /// <summary> /// 检测是否有相同文件存在 有则删除 /// </summary> /// <param name="path"></param> private static void CheckIsFileExixst(string path) { FileUtil.DeleteFileOrDirectory(path); AssetDatabase.Refresh(); } /// <summary> /// 创建AnimationData /// </summary> /// <param name="animationData"></param> /// <param name="sprites"></param> private static void BuildAnimation(SpriteAnimationData animationData,List<Sprite> sprites) { for (int i = 0; i < sprites.Count; i++) { animationData.frameDatas.Add(new SpriteAnimationFrameData() { sprite = sprites[i], time = 0.1f }); } //AssetDatabase.SaveAssets(); } /// <summary> /// 动画是否需要循环 /// </summary> /// <param name="animationName"></param> /// <returns></returns> private static bool IsAnimationLoop(string animationName) { return (animationName.Contains("attack") || animationName.Contains("run") || animationName.Contains("move_") || animationName.Contains("FeiXing") ); } #region Copy From script "SpriteAnimationAssetCreation" public static void CreateAnimationAsset(string path,string assetName,System.Action<SpriteAnimationAsset> call) { var folderPath = ProjectPath + "/" + path + "/" + AnimationPath; var directoryInfo = new DirectoryInfo(folderPath); if (!directoryInfo.Exists) Directory.CreateDirectory(folderPath); var asset = ScriptableObject.CreateInstance<SpriteAnimationAsset>(); var filePath = path + "/" + AnimationPath + "/" + assetName + ".asset"; animationAssetPath = filePath; CheckIsFileExixst(filePath); call(asset); AssetDatabase.CreateAsset(EditorUtility.InstanceIDToObject(asset.GetInstanceID()), AssetDatabase.GenerateUniqueAssetPath(filePath)); AssetDatabase.SaveAssets(); } #endregion}

最后效果:

这里写图片描述


上一篇:APK动态加载框架(DL)解析

下一篇:VFL

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表