- Home /
The question is answered
[SOLUTIONED] Display Sprite (from 4.3) in an EditorWindow
Hello,
I am building a little 2D level editor with Unity 4.3.3. So I have created a custom EditorWindow.
I loaded an atlas and crop the texture from the atlas to create texture representing the sprite.
Object[] objs = AssetDatabase.LoadAllAssetsAtPath(path);
Texture2D currentTex = null;
foreach(Object obj in objs) {
if (obj.GetType()==typeof(Texture2D)) {
currentTex = (Texture2D)obj;
// test if the texture is readable
try
{
currentTex.GetPixels32();
}
catch (UnityException e)
{
Debug.Log ("texture is not readable: " + path);
}
} else if (obj.GetType()==typeof(Sprite)) {
Sprite sprite = (Sprite)obj;
Color[] pixels = currentTex.GetPixels(
(int)sprite.rect.x,
(int)sprite.rect.y,
(int)sprite.rect.width,
(int)sprite.rect.height);
Texture2D tcropped = new Texture2D(
(int)sprite.rect.width,
(int)sprite.rect.height);
tcropped.SetPixels(pixels);
tcropped.Apply();
textures.Add (tcropped);
}
}
And then I display the sprites
scrollViewPos = GUILayout.BeginScrollView(scrollViewPos);
foreach (Texture2D tex in textures) {
rect = EditorGUILayout.BeginHorizontal();
if (GUILayout.Button(sitem.tex)) {
}
EditorGUILayout.EndHorizontal();
}
GUILayout.EndScrollView();
The problem with this solution is my texture atlas has to be readable. I do not want my texture to be readable during runtime for performance/memory issues and I did not find any solution to change it just for the texture I am loading in my editor. I try to use "processor" but it used during import processus not when I am loading files.
Another solution is to find another way to retrieve the preview texture of the sprite.
Any one has a solution ?
I found a solution Ok. The solution is simply to retrieve the texture importer, modify it and reimport the asset if necessary and restore it when the editor is closed.
TextureImporter textureImporter = AssetImporter.GetAtPath("Assets/Sprites/"+f.Name) as TextureImporter;
if (textureImporter.isReadable==false) {
Debug.Log (f.Name + " is not readable. Update it and reimport it.");
textureImporter.isReadable = true;
AssetDatabase.ImportAsset("Assets/Sprites/"+f.Name,ImportAssetOptions.ForceUpdate);
texturesReImported.Add ("Assets/Sprites/"+f.Name);
}
Regards,
Nicolas
Follow this Question
Related Questions
How to put multiple sprites in one click inside of a game object? 0 Answers
4.3 Sprite animation and animator 1 Answer
Sprite Animation 2 Answers
Sprite's alpha visible with lighting 0 Answers
Load Unity 4.3 Sprites with AssetBundles 5 Answers