Last active
December 9, 2024 12:02
-
-
Save anatawa12/06751ee6e6b64cc30cee89b8dd2c3ea4 to your computer and use it in GitHub Desktop.
tool to save asset icon to png file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* SaveIconTextureOfAsset | |
* tool to save asset icon to png file | |
* | |
* | |
* https://gist.github.com/anatawa12/06751ee6e6b64cc30cee89b8dd2c3ea4 | |
* | |
* Click `Tools/anatawa12 gists/SaveIconTextureOfAsset` to open this window. | |
* | |
* MIT License | |
* | |
* Copyright (c) 2023 anatawa12 | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy | |
* of this software and associated documentation files (the "Software"), to deal | |
* in the Software without restriction, including without limitation the rights | |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
* copies of the Software, and to permit persons to whom the Software is | |
* furnished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in all | |
* copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
* SOFTWARE. | |
*/ | |
#if UNITY_EDITOR && (!ANATAWA12_GISTS_VPM_PACKAGE || GIST_06751ee6e6b64cc30cee89b8dd2c3ea4) | |
using System; | |
using System.Collections.Generic; | |
using UnityEditor; | |
using UnityEngine; | |
using UnityEngine.Rendering; | |
using Object = UnityEngine.Object; | |
namespace anatawa12.gists | |
{ | |
internal class SaveIconTextureOfAsset : EditorWindow | |
{ | |
private const string GIST_NAME = "SaveIconTextureOfAsset"; | |
[MenuItem("Tools/anatawa12 gists/" + GIST_NAME)] | |
static void Create() => GetWindow<SaveIconTextureOfAsset>(GIST_NAME); | |
Object target; | |
bool inProgress; | |
private void OnGUI() | |
{ | |
target = EditorGUILayout.ObjectField("Target", target, typeof(Object), false); | |
EditorGUI.BeginDisabledGroup(inProgress || target == null); | |
if (GUILayout.Button("Save")) | |
{ | |
if (target == null) | |
{ | |
EditorUtility.DisplayDialog("Error", "target is null", "OK"); | |
return; | |
} | |
var path = AssetDatabase.GetAssetPath(target); | |
if (string.IsNullOrEmpty(path)) | |
{ | |
EditorUtility.DisplayDialog("Error", "target is not asset", "OK"); | |
return; | |
} | |
var icon = AssetDatabase.GetCachedIcon(path) as Texture2D; | |
if (icon == null) | |
{ | |
EditorUtility.DisplayDialog("Error", "failed to get icon", "OK"); | |
return; | |
} | |
inProgress = true; | |
AsyncGPUReadback.Request(icon, 0, TextureFormat.RGBA32, (result) => | |
{ | |
inProgress = false; | |
if (result.hasError) | |
{ | |
EditorUtility.DisplayDialog("Error", "failed to read icon", "OK"); | |
return; | |
} | |
var saveTexture = new Texture2D(result.width, result.height, TextureFormat.RGBA32, 1, false); | |
saveTexture.LoadRawTextureData(result.GetData<byte>()); | |
saveTexture.Apply(); | |
var bytes = saveTexture.EncodeToPNG(); | |
var savePath = EditorUtility.SaveFilePanel("Save Icon Texture", "", target.name + ".png", "png"); | |
if (string.IsNullOrEmpty(savePath)) | |
return; | |
System.IO.File.WriteAllBytes(savePath, bytes); | |
}); | |
} | |
EditorGUI.EndDisabledGroup(); | |
} | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment