Unity Texture Handling
- Ashwani Shahrawat
- Jun 25, 2024
- 2 min read
Will explain
2D, 3D, Cube, 2D Array, Atlas, Sprite Sheet
Create
Scale
Compress Editor, Runtime
Save
Load
[MenuItem("CONTEXT/RenderTexture/Load from Texture")]
public static void Load_FromTexture()
{
if (!(Selection.activeObject is RenderTexture)) return;
var l_RT = (RenderTexture)Selection.activeObject;
// Show OpenFilePanel wilth last Loaded Path & File Name
var l_LoadPath = EditorPrefs.GetString("RT_EXT_LoadPath", Application.dataPath + "\\Textures");
var m_FileName = EditorPrefs.GetString("RT_EXT_FileName", "TexMix_" + Time.time);
var l_Path = EditorUtility.OpenFilePanel("Load Splatmap", l_LoadPath + m_FileName, "png");
if (string.IsNullOrEmpty(l_Path)) return;
EditorPrefs.SetString("RT_EXT_LoadPath", l_Path.Remove(l_Path.LastIndexOf('/') + 1));
EditorPrefs.SetString("RT_EXT_FileName", l_Path.Remove(0, l_Path.LastIndexOf('/') + 1));
var l_RelativePath = l_Path.Replace(Application.dataPath, "Assets");
var l_Tex = (Texture2D)AssetDatabase.LoadAssetAtPath(l_RelativePath, typeof(Texture2D));
Graphics.Blit(l_Tex, l_RT);
}
[MenuItem("CONTEXT/RenderTexture/Save as File")]
public static void Save_AsFile()
{
if (!(Selection.activeObject is RenderTexture)) return;
var l_RT = (RenderTexture)Selection.activeObject;
// Show SaveFilePanel wilth last Loaded Path & File Name
var m_SavePath = EditorPrefs.GetString("RT_EXT_SavePath", Application.dataPath + "\\Textures");
var m_FileName = EditorPrefs.GetString("RT_EXT_FileName", "TexMix_" + Time.time);
var l_Path = EditorUtility.SaveFilePanel("Save Splatmap", m_SavePath, m_FileName, "png");
if (string.IsNullOrEmpty(l_Path)) return;
EditorPrefs.SetString("RT_EXT_SavePath", l_Path.Remove(l_Path.LastIndexOf('/') + 1));
EditorPrefs.SetString("RT_EXT_FileName", l_Path.Remove(0, l_Path.LastIndexOf('/') + 1));
var l_OldTarget = RenderTexture.active;
RenderTexture.active = l_RT;
Texture2D l_Tex = new Texture2D(l_RT.width, l_RT.height, TextureFormat.ARGB32, false, !l_RT.sRGB);
l_Tex.ReadPixels(new Rect(0, 0, l_RT.width, l_RT.height), 0, 0);
l_Tex.Apply();
RenderTexture.active = l_OldTarget;
File.WriteAllBytes(l_Path, l_Tex.EncodeToPNG());
}
[MenuItem("CONTEXT/TextureImporter/Generate Point MipMaps")]
public static void Generate_PointMipMaps()
{
if (!(Selection.activeObject is Texture2D)) return;
var l_SelTex2D = (Texture2D)Selection.activeObject;
var l_Tex2D = new Texture2D(l_SelTex2D.width, l_SelTex2D.height, l_SelTex2D.format, l_SelTex2D.mipmapCount, true);
if (!l_Tex2D.isReadable)
{
Debug.LogError("Unable to Read Pixels, Please set Enable Read/Write");
return;
}
int l_Width = l_Tex2D.width;
int l_Height = l_Tex2D.height;
int l_MipCount = l_Tex2D.mipmapCount;
var l_Colors = l_SelTex2D.GetPixels32();
for (int i = 0; i < l_MipCount; i++)
{
int l_ResDivisor = (int)Math.Pow(2, i);
int l_MipWidth = Mathf.Max(1, l_Width / l_ResDivisor);
int l_MipHeight = Mathf.Max(1, l_Height / l_ResDivisor);
int l_MipPixels = l_MipWidth * l_MipHeight;
var l_MipCols = new Color32[l_MipPixels];
for (int v = 0; v < l_MipHeight; v++)
{
for (int u = 0; u < l_MipWidth; u++)
{
int l_SrcIdx = v * l_Width * l_ResDivisor + u * l_ResDivisor;
int l_DstIdx = v * l_MipWidth + u;
l_MipCols[l_DstIdx] = l_Colors[l_SrcIdx];
}
}
l_Tex2D.SetPixels32(l_MipCols, i);
}
l_Tex2D.Apply(false, false);
EditorUtility.CompressTexture(l_Tex2D, TextureFormat.ASTC_6x6, TextureCompressionQuality.Best);
var m_SavePath = EditorPrefs.GetString("TEX_EXT_SavePath", Application.dataPath + "\\Textures");
var m_FileName = EditorPrefs.GetString("TEX_EXT_FileName", "TexMix_" + Time.time);
var l_Path = EditorUtility.SaveFilePanel("Save PointMipMaps", m_SavePath, m_FileName, "asset");
if (string.IsNullOrEmpty(l_Path)) return;
EditorPrefs.SetString("TEX_EXT_SavePath", l_Path.Remove(l_Path.LastIndexOf('/') + 1));
EditorPrefs.SetString("TEX_EXT_FileName", l_Path.Remove(0, l_Path.LastIndexOf('/') + 1));
AssetDatabase.CreateAsset(l_Tex2D, l_Path);
}



Comments