获取Unity内置图标
前言
突然发现自己也是闲的无聊,为了做文章的封面。所以我想拿到Unity的自带的一些图标。但是我找不到这些图片的位置。这时候我找到了一篇文章,这里面有方法可以获取到这些内置的图标。但是我们只能得到Texture
类型的数据,且其是不可读。幸好我们可以通过一些操作让其可读并转为Texture2D
类型的数据,最终得到一个.png
的图片。这个操作我也是从网上找到的文章中看到的。以上所有的文章我都会放在下面的参考文章中。
主要代码实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| var source = EditorGUIUtility.IconContent("Shader Icon").image as Texture2D; RenderTexture renderTex = RenderTexture.GetTemporary( source.width, source.height, 0, RenderTextureFormat.Default, RenderTextureReadWrite.Linear); Graphics.Blit(source, renderTex); RenderTexture previous = RenderTexture.active; RenderTexture.active = renderTex; Texture2D readableText = new Texture2D(source.width, source.height); readableText.ReadPixels(new Rect(0, 0, renderTex.width, renderTex.height), 0, 0); readableText.Apply();
RenderTexture.active = previous; RenderTexture.ReleaseTemporary(renderTex); var path = Path.Combine(Application.dataPath, "UnityIcon.png"); if (!File.Exists(path)) { File.Create(path).Dispose(); } File.WriteAllBytesAsync(path, readableText.EncodeToPNG());
|
参考文章:
Unity内建图标列表:
https://www.cnblogs.com/CloudLiu/p/9957335.html
Unity 通过代码实现Texture2D可读写:
https://blog.csdn.net/lizhenxiqnmlgb/article/details/129265011