首页 > 代码库 > [编辑器]Unity批量处理图片
[编辑器]Unity批量处理图片
加载图片,将其画在一张RenderTexture中,再新建一张Texture2D,从rt中读出来处理过的数据,再存进原路径中,
比如要将所有图片扩大1.5倍:
public static void ChangeSize(params string[] paths) { var texture2Ds = AssetDatabase.FindAssets ("t:Texture2D", paths); var retTexture = new Texture2D (1, 1, TextureFormat.ARGB32, false); int count = textures.Length; for(int i = 0; i < count; ++i) { var path = AssetDatabase.GUIDToAssetPath (textures[i]); var texture = AssetDatabase.LoadAssetAtPath<Texture2D> (path); var rt = RenderTexture.GetTemporary ((int)(texture.width * 1.5f), (int)(texture.height * 1.5f), 0, RenderTextureFormat.ARGB32); texture.DiscardContents (); Graphics.Blit (texture, rt); RenderTexture.active = texture; retTexture.Resize (rt.width, rt.height); retTexture.ReadPixels (new Rect (0, 0, rt.width, rt.height), 0, 0); retTexture.Apply (); File.WriteAllBytes (path, retTexture.EncodeToPNG ()); RenderTexture.ReleaseTemporary (rt); } AssetDatabase.SaveAssets (); AssetDatabase.Refresh (); }
为防止重复编辑,可以在userdata中添加标志:
var im = AssetImporter.GetAtPath (path); if (im.userData.Contains ("R2;")) continue; im.userData += "R2;";
[编辑器]Unity批量处理图片
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。