- Home /
[Solved] How can I change Texture2D format in Editor Script?
I want to change a texture format to RGBA32 so I can then call Texture2D.GetRawTextureData and expect 4 byte pixels. This is for an editor script. I know about getting the TextureImporter and the GetPlatformTextureSettings and SetPlatformTextureSettings methods. But these methods ask for a platform string and "Default" is not a legal option. How can I change the default platform texture format through code?
Answer by joshrs926 · Feb 09 at 09:52 PM
Ok I figured it out! You simply call TextureImporter.GetDefaultPlatformTextureSettings() to get a TextureImporterPlatformSettings instance with the current settings from the texture2d, adjust its fields how you want, then feed it back into TextureImporter.SetPlatformTextureSettings(TextureImporterPlatformSettings platformSettings). Here is a useful class for adjusting settings of a Texture2D asset in the Editor.
using UnityEngine;
using UnityEditor;
public class TextureReimporter
{
TexAsset[] assets;
class TexAsset
{
public Texture2D texture;
public TextureImporter importer;
public TextureImporterSettings original;
public TextureImporterSettings copy;
public TextureImporterPlatformSettings originalPS;
public TextureImporterPlatformSettings copyPS;
public bool isValid;
public TexAsset(Texture2D texture)
{
isValid = AssetDatabase.IsMainAsset(texture);
if (isValid)
{
this.texture = texture;
importer = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(texture)) as TextureImporter;
original = new TextureImporterSettings();
copy = new TextureImporterSettings();
originalPS = new TextureImporterPlatformSettings();
copyPS = new TextureImporterPlatformSettings();
importer.ReadTextureSettings(original);
original.CopyTo(copy);
originalPS = importer.GetDefaultPlatformTextureSettings();
originalPS.CopyTo(copyPS);
}
}
}
public TextureReimporter(params Texture2D[] textures)
{
assets = new TexAsset[textures.Length];
for (int i = 0; i < textures.Length; i++)
{
assets[i] = new TexAsset(textures[i]);
}
}
public void ImportWithSettings(
bool isReadable = true,
TextureImporterType type = TextureImporterType.Default,
TextureImporterFormat format = TextureImporterFormat.RGBA32)
{
foreach (var asset in assets)
{
if (asset.isValid)
{
asset.copy.readable = isReadable;
asset.copy.textureType = type;
asset.copyPS.format = format;
asset.importer.SetTextureSettings(asset.copy);
asset.importer.SetPlatformTextureSettings(asset.copyPS);
if (AssetDatabase.WriteImportSettingsIfDirty(asset.importer.assetPath))
{
asset.importer.SaveAndReimport();
}
}
}
}
public void Reset()
{
foreach (var asset in assets)
{
if (asset.isValid)
{
asset.importer.SetTextureSettings(asset.original);
asset.importer.SetPlatformTextureSettings(asset.originalPS);
if (AssetDatabase.WriteImportSettingsIfDirty(asset.importer.assetPath))
{
asset.importer.SaveAndReimport();
}
}
}
}
}
Your answer
Follow this Question
Related Questions
Editor class "Texture Importer" question (applying settings to multiple texture assets). 2 Answers
How do I solve a BuildAssetBundles Compilation Error that only occurs in the Editor? 1 Answer
How to get the currently selected object in the project window only 2 Answers
How to use an Editor script? 0 Answers