国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

使用EasySpritesAnimation一鍵生成動畫預制

2019-11-09 15:05:10
字體:
來源:轉載
供稿:網友

本文所用到的插件下載地址: 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("http://", "/"); 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("http://", "/").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("http://", "/"); //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}

最后效果:

這里寫圖片描述


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 盖州市| 拉孜县| 泰安市| 胶南市| 遵义县| 赤峰市| 铅山县| 右玉县| 荃湾区| 沐川县| 吉林市| 佛山市| 盖州市| 滕州市| 西和县| 汾阳市| 乌审旗| 黎平县| 自贡市| 弥勒县| 正阳县| 青田县| 明星| 长宁县| 宿州市| 邯郸市| 隆德县| 兴安盟| 宜都市| 横山县| 阿克陶县| 新田县| 民和| 长兴县| 金华市| 花莲市| 陆河县| 通城县| 双辽市| 栾川县| 新竹市|