Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Mar 01, 2014 at 08:30 PM by Jamora for the following reason:

The question is answered

avatar image
0
Question by NicolasHognon · Feb 28, 2014 at 11:28 PM · spriteeditorwindow4.3

[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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges