You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
	
	
		
			185 lines
		
	
	
		
			7.2 KiB
		
	
	
	
		
			C#
		
	
		
		
			
		
	
	
			185 lines
		
	
	
		
			7.2 KiB
		
	
	
	
		
			C#
		
	
| 
											2 years ago
										 | using UnityEngine; | ||
|  | using System.Collections; | ||
|  | using System.Collections.Generic; | ||
|  | using UnityEditor; | ||
|  | using UnityEditor.Animations; | ||
|  | using System.IO; | ||
|  | 
 | ||
|  | public class AdjustTexture : MonoBehaviour | ||
|  | { | ||
|  |     [MenuItem("Tools/AdjustTexture/AdjustBasic")] | ||
|  |     static void AdjustBasic() | ||
|  |     { | ||
|  |         // Adjust("/Resources/Character/basic/"); | ||
|  |         DealBasicAnimatior(); | ||
|  |         Debug.Log("AdjustBasic Done"); | ||
|  |     } | ||
|  |     [MenuItem("Tools/AdjustTexture/AdjustCharacter")] | ||
|  |     static void AdjustCharacter() | ||
|  |     { | ||
|  |         Adjust("/Resources/Character/hero/"); | ||
|  |         Adjust("/Resources/Character/monster/"); | ||
|  |         Debug.Log("AdjustCharacter Done"); | ||
|  |     } | ||
|  |     [MenuItem("Tools/AdjustTexture/AdjustMonster")] | ||
|  |     static void AdjustMonster() | ||
|  |     { | ||
|  |         Adjust("/Resources/Character/monster/"); | ||
|  |         Debug.Log("AdjustMonster Done"); | ||
|  |     } | ||
|  |     [MenuItem("Tools/AdjustTexture/AdjustHero01Long")] | ||
|  |     static void AdjustHero01Long() | ||
|  |     { | ||
|  |         Adjust("/Resources/Character/hero/", "Character/hero/hero01\\hero01_long"); | ||
|  |         Debug.Log("AdjustHero01Long Done"); | ||
|  |     } | ||
|  |     [MenuItem("Tools/AdjustTexture/AdjustHero01Short")] | ||
|  |     static void AdjustHero01Short() | ||
|  |     { | ||
|  |         Adjust("/Resources/Character/hero/", "Character/hero/hero01\\hero01_short"); | ||
|  |         Debug.Log("AdjustHero01Short Done"); | ||
|  |     } | ||
|  |     static void Adjust(string pathRoot, string prefix = "") | ||
|  |     { | ||
|  |         List<string> dirs = new List<string>(); | ||
|  |         GetDirs(Application.dataPath + pathRoot, ref dirs); | ||
|  | 
 | ||
|  |         var FRAME_RATE = 12; | ||
|  | 
 | ||
|  |         foreach (var fileDir in dirs) | ||
|  |         { | ||
|  |             var resPath = fileDir.Replace("Assets/Resources/", "").Replace(".png", ""); | ||
|  |             if (!string.IsNullOrEmpty(prefix) && !resPath.StartsWith(prefix)) | ||
|  |             { | ||
|  |                 continue; | ||
|  |             } | ||
|  |             Debug.Log(fileDir); | ||
|  |             var textures = Resources.Load<Texture2D>(resPath); | ||
|  |             var path = AssetDatabase.GetAssetPath(textures); | ||
|  | 
 | ||
|  |             var ti = AssetImporter.GetAtPath(path) as TextureImporter; | ||
|  | 
 | ||
|  |             //调整资源参数 | ||
|  |             ti.isReadable = true; | ||
|  |             ti.spritePixelsPerUnit = 64; | ||
|  |             ti.filterMode = FilterMode.Point; | ||
|  |             ti.textureCompression = TextureImporterCompression.Uncompressed; | ||
|  |             ti.textureType = TextureImporterType.Sprite; | ||
|  |             ti.spriteImportMode = SpriteImportMode.Multiple; | ||
|  | 
 | ||
|  |             //切片 | ||
|  |             var textureSettings = new TextureImporterSettings(); | ||
|  |             ti.ReadTextureSettings(textureSettings); | ||
|  |             textureSettings.spriteMeshType = SpriteMeshType.FullRect; | ||
|  |             textureSettings.spriteExtrude = 0; | ||
|  |             ti.SetTextureSettings(textureSettings); | ||
|  | 
 | ||
|  |             int SliceWidth = 128; | ||
|  |             int SliceHeight = 64; | ||
|  | 
 | ||
|  |             var newData = new List<SpriteMetaData>(); | ||
|  |             for (int i = 0; i < textures.width; i += SliceWidth) | ||
|  |             { | ||
|  |                 for (int j = textures.height; j > 0; j -= SliceHeight) | ||
|  |                 { | ||
|  |                     var smd = new SpriteMetaData(); | ||
|  |                     smd.alignment = 9; | ||
|  |                     smd.pivot = new Vector2(0.5f, 0.5f); | ||
|  |                     smd.name = (textures.height - j) / SliceHeight + ", " + i / SliceWidth; | ||
|  |                     smd.rect = new Rect(i, j - SliceHeight, SliceWidth, SliceHeight); | ||
|  | 
 | ||
|  |                     newData.Add(smd); | ||
|  |                 } | ||
|  |             } | ||
|  |             ti.spritesheet = newData.ToArray(); | ||
|  |             AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate); | ||
|  | 
 | ||
|  |             //生成动画文件 | ||
|  |             var animPath = fileDir.Replace(".png", ".anim"); | ||
|  |             var spriteNum = newData.Count; | ||
|  |             var timeTotal = (float)spriteNum / FRAME_RATE; | ||
|  |             var sprites = Resources.LoadAll<Sprite>(resPath); | ||
|  | 
 | ||
|  |             var animClip = Resources.Load<AnimationClip>(animPath); | ||
|  |             var isCreateNew = animClip == null; | ||
|  |             if (isCreateNew) | ||
|  |             { | ||
|  |                 animClip = new AnimationClip(); | ||
|  |             } | ||
|  |             animClip.frameRate = FRAME_RATE; | ||
|  |             var clipSetting = AnimationUtility.GetAnimationClipSettings(animClip); | ||
|  |             clipSetting.loopTime = resPath.EndsWith("_loop"); | ||
|  |             AnimationUtility.SetAnimationClipSettings(animClip, clipSetting); | ||
|  | 
 | ||
|  |             var spriteBinding = new EditorCurveBinding(); | ||
|  |             spriteBinding.type = typeof(SpriteRenderer); | ||
|  |             spriteBinding.path = ""; | ||
|  |             spriteBinding.propertyName = "m_Sprite"; | ||
|  |             var spriteKeyFrames = new ObjectReferenceKeyframe[sprites.Length]; | ||
|  |             for (int i = 0; i < (sprites.Length); i++) | ||
|  |             { | ||
|  |                 spriteKeyFrames[i] = new ObjectReferenceKeyframe(); | ||
|  |                 spriteKeyFrames[i].time = i / animClip.frameRate; | ||
|  |                 spriteKeyFrames[i].value = sprites[i]; | ||
|  |             } | ||
|  |             AnimationUtility.SetObjectReferenceCurve(animClip, spriteBinding, spriteKeyFrames); | ||
|  |             AssetDatabase.CreateAsset(animClip, animPath); | ||
|  |         } | ||
|  |         AssetDatabase.SaveAssets(); | ||
|  |     } | ||
|  |     static void DealBasicAnimatior() | ||
|  |     { | ||
|  |         var res = Resources.Load<AnimatorController>("Character/_common/animator"); | ||
|  |         string path = AssetDatabase.GetAssetPath(res); | ||
|  |         var ac = AssetDatabase.LoadAssetAtPath<AnimatorController>(path); | ||
|  |         var acLayers = ac.layers; | ||
|  | 
 | ||
|  |         foreach (AnimatorControllerLayer i in acLayers) //for each layer | ||
|  |         { | ||
|  |             var stateMachine = i.stateMachine; | ||
|  |             var ch_animStates = new List<ChildAnimatorState>(stateMachine.states); | ||
|  |             foreach (var sm in stateMachine.stateMachines) | ||
|  |             { | ||
|  |                 ch_animStates.AddRange(sm.stateMachine.states); | ||
|  |             } | ||
|  |             foreach (ChildAnimatorState j in ch_animStates) //for each state | ||
|  |             { | ||
|  |                 AnimationClip clip = null; | ||
|  |                 if (j.state.name.StartsWith("timeline")) | ||
|  |                 { | ||
|  |                     clip = Resources.Load<AnimationClip>($"Character/_common/{j.state.name}"); | ||
|  |                 } | ||
|  |                 else | ||
|  |                 { | ||
|  |                     clip = Resources.Load<AnimationClip>($"Character/basic/basic_{j.state.name}"); | ||
|  |                 } | ||
|  |                 j.state.motion = clip; | ||
|  |                 ac.SetStateEffectiveMotion(j.state, clip); | ||
|  |                 Debug.Log(i.name + "." + j.state.name + "."); | ||
|  |             } | ||
|  |         } | ||
|  |         AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate); | ||
|  |         AssetDatabase.SaveAssets(); | ||
|  |         AssetDatabase.Refresh(); | ||
|  |     } | ||
|  |     //参数1 为要查找的总路径, 参数2 保存路径 | ||
|  |     private static void GetDirs(string dirPath, ref List<string> dirs) | ||
|  |     { | ||
|  |         foreach (string path in Directory.GetFiles(dirPath)) | ||
|  |         { | ||
|  |             //获取所有文件夹中包含后缀为 .png 的路径 | ||
|  |             if (System.IO.Path.GetExtension(path) == ".png") | ||
|  |             { | ||
|  |                 dirs.Add(path.Substring(path.IndexOf("Assets"))); | ||
|  |             } | ||
|  |         } | ||
|  | 
 | ||
|  |         if (Directory.GetDirectories(dirPath).Length > 0)  //遍历所有文件夹 | ||
|  |         { | ||
|  |             foreach (string path in Directory.GetDirectories(dirPath)) | ||
|  |             { | ||
|  |                 GetDirs(path, ref dirs); | ||
|  |             } | ||
|  |         } | ||
|  |     } | ||
|  | } |