Last active
August 3, 2022 04:41
-
-
Save Ooseykins/8fcb097fc3c9710b4ae1bffc15c2f09b to your computer and use it in GitHub Desktop.
Override of RenderStaticPreview for tinting an asset preview icon to a scriptable object's color
This file contains 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
using UnityEngine; | |
using UnityEditor; | |
[CustomEditor(typeof(ScriptableObjectWithColor))] | |
public class ScriptableObjectWithColorEditor : Editor | |
{ | |
public override Texture2D RenderStaticPreview(string assetPath, Object[] subAssets, int width, int height) | |
{ | |
ScriptableObjectWithColor targetObj = (ScriptableObjectWithColor)target; | |
if (targetObj == null) | |
{ | |
return null; | |
} | |
var s = Shader.Find("UI/Default"); | |
if(s == null) | |
{ | |
return null; | |
} | |
var m = new Material(s); | |
m.color = targetObj.color; | |
var renderTex = new RenderTexture(width, height, 0); | |
var prevRT = RenderTexture.active; | |
Graphics.Blit(AssetPreview.GetMiniThumbnail(target), renderTex, m); | |
var outputTex = new Texture2D(width, height); | |
outputTex.ReadPixels(new Rect(0, 0, renderTex.width, renderTex.height), 0, 0); | |
outputTex.Apply(); | |
RenderTexture.active = prevRT; | |
renderTex.Release(); | |
return outputTex; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment