bakman
October 19, 2014, 1:31pm
1
Hello,
I would like to configure a sprite in multiple mode on runtime by this way:
public void ConfigTexture()
{
TextureImporter importer = TextureImporter.GetAtPath("Assets/Resources/idle.png") as TextureImporter;
importer.textureType = TextureImporterType.Advanced;
importer.maxTextureSize = 2048;
importer.textureFormat = TextureImporterFormat.AutomaticTruecolor;
importer.isReadable = true;
importer.filterMode = FilterMode.Bilinear;
importer.npotScale = TextureImporterNPOTScale.None;
importer.wrapMode = TextureWrapMode.Clamp;
importer.mipmapEnabled = false;
importer.spriteImportMode = SpriteImportMode.Multiple;
UnityEngine.Object asset = AssetDatabase.LoadAssetAtPath(importer.assetPath, typeof(Texture2D));
if (asset)
{
EditorUtility.SetDirty(asset);
}
else
{
importer.textureType = TextureImporterType.Advanced;
}
}
The problem is, the asset in Resource folder look like Texture2D not a multiple Sprite with left arrow who permit to see sprites. How can I configure an image to a sprite properly in runtime ?
Thank you.
bakman
October 19, 2014, 5:15pm
2
I found it!
I added this :
AssetDatabase.ImportAsset(importer.assetPath, ImportAssetOptions.ForceUpdate);
complete code if anybody want to use it :
public void ConfigTexture()
{
TextureImporter importer = TextureImporter.GetAtPath("Assets/Resources/idle.png") as TextureImporter;
importer.textureType = TextureImporterType.Advanced;
importer.maxTextureSize = 2048;
importer.textureFormat = TextureImporterFormat.AutomaticTruecolor;
importer.isReadable = true;
importer.filterMode = FilterMode.Bilinear;
importer.npotScale = TextureImporterNPOTScale.None;
importer.wrapMode = TextureWrapMode.Clamp;
importer.mipmapEnabled = false;
importer.spriteImportMode = SpriteImportMode.Multiple;
UnityEngine.Object asset = AssetDatabase.LoadAssetAtPath(importer.assetPath, typeof(Texture2D));
AssetDatabase.ImportAsset(importer.assetPath, ImportAssetOptions.ForceUpdate);
if (asset)
{
EditorUtility.SetDirty(asset);
Debug.Log("dirty");
}
else
{
importer.textureType = TextureImporterType.Advanced;
Debug.Log("not dirty");
}
}
See ya
ibyte
November 12, 2014, 6:53am
3
Hi I have an issue where the importer always comes back null even though the file is in the directory . Any suggestions?
Also can it only run in Unity Editor script? or at normal run time as well?
ibyte
November 17, 2014, 2:35am
4
I figured out my issue. I was using the full path to my asset when the path should be relative to your project.