Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
2
Question by domiii · Feb 08, 2016 at 05:41 PM · texture2dassetassetdatabasepreview

How to set Sprite or Texture2D AssetPath in editor script?

I have an editor script that creates buttons in a menu. Each button displays an asset preview, loaded with AssetPreview.GetAssetPreview. I create a new Sprite from the returned Texture2D, attach it to a SpriteRenderer of a newly created button GameObject, and it displays just right.

Problem is that because it is not an actual asset, it does not show up in the actual game. In game mode, the preview sprite is gone, and I see the following error in the console:

rd->texture.IsValid()

UnityEditor.DockArea:OnGUI()

When leaving play mode, the preview also does not return. I would say that editor script changes should be permanent (especially since it looks just fine in editor mode). I am assuming however that Unity 5 does not yet save these memory-backed assets to the scene file or some other part of the asset database, and thus is not persisted in any form or shape, resulting in oddities.

So I was wondering if there is any way, I can use an editor script to tell the importer where to look for a Texture or Sprite, after I persisted it to a file?

Of course, I could use a run-time script to load the asset on start-up, but I would rather have Unity take care of all the asset loading, to keep things simple and save myself a lot of unnecessary trouble. After all, why should things be any different, just because I use a script to set a sprite, instead of setting it manually?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by domiii · Feb 08, 2016 at 07:34 PM

I finally produced a solution, mostly based on this answer (which I found in the "Related Questions" on the right).

All the magic lies in AssetDatabase.CreateAsset. However, it's rather tricky and is quick to unapologetically spew out non-explanatory error messages. So here are my lessons learned and the code I put together in the past hour:

  1. When creating an asset from an AssetPreview, first create a copy, else it will consider the original Texture as an existing asset. This might actually work in your favor (i.e. you don't have to re-create it if it's already recognized as an asset), but it could also quickly create more issues, so making a copy is the safest way.

  2. The expected path must be relative to the CWD, which is your project folder. I.e. in almost all cases, you want the path to be prefixed with Assets/.

  3. The resulting file apparently is an asset file, and not just a plain *.png file.

  4. To be 100% safe, you want to re-load the asset and use it's Texture2D object, so the editor fully understands the connection to the asset and not lose it again.

  5. For some reason, with this script, when inspecting the result in the editor (e.g. in my case, in SpriteRenderer), it will not show a name nor will it recognize the connection to the actual file, and yet everything else works fine.

Code:

 public static readonly string PreviewFileName = "MenuButtonPreview";
 public static readonly string PreviewFileFolder = "AutoGenerated";

 Texture2D StorePreviewAsAsset(Object asset, int index) {
     var folder = "Assets/" + PreviewFileFolder;
     var fileNamePrefix = PreviewFileName + index;
     var fileName = fileNamePrefix + ".asset";
     var completePath = folder + '/' + fileName;

     // get preview texture from cache
     var textureOrig = AssetPreview.GetAssetPreview(asset);

     // create new texture that is copy of original
     var texture = new Texture2D (textureOrig.width, textureOrig.height, textureOrig.format, false);
     texture.SetPixels32 (textureOrig.GetPixels32 ());
     texture.name = folder + '/' + fileNamePrefix;

     // create folders
     if (!Directory.Exists (folder)) {
         Directory.CreateDirectory (folder);
     }

     // delete any existing version of it (if any)
     AssetDatabase.DeleteAsset (completePath);

     // add to AssetDatabase
     AssetDatabase.CreateAsset(texture, completePath);

     // load and from here on only reference the texture from the AssetDatabase
     var previewAsset = AssetDatabase.LoadAssetAtPath(completePath, typeof(Texture2D));
     return (Texture2D)EditorUtility.InstanceIDToObject (previewAsset.GetInstanceID ());
 }



Comment
Add comment · Share
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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

34 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 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

Create an asset and keep reference? 1 Answer

assign texture to asset material via script 1 Answer

Prefab as a subasset of a ScriptableObject 0 Answers

AssetDatabase.CreateAsset() - Value cannot be null 2 Answers

Addressables - Use Asset Database (faster) - Not loading assets within a folder 1 Answer


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