Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
0
Question by TustinJimberlake · Nov 03, 2021 at 05:18 AM · editoreditor-scriptingscriptableobjectassetdatabaseicon

How to create custom asset icons for ScriptableObject instances

I have an item database consisting of several ScriptableObjects of type Item. These items have a public Sprite icon; field, and the behavior I want is for the icon for each asset to reflect the contents of the icon Sprite.


Currently, I am using EditorGUIUtility.SetIconForObject(asset, icon.texture); to accomplish this, but I am running into some strange issues with this that I can't pinpoint the cause of.


Here is my current script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEditor;

 namespace InventorySystem
 {
     [InitializeOnLoad]
     public class InventorySystemEditorUtils
     {
         static InventorySystemEditorUtils()
         {
             List<Item> items = FindAssetsByType<Item>();
             items.ForEach(item =>
             {
                 Item obj = AssetDatabase.LoadAssetAtPath<Item>(AssetDatabase.GetAssetPath(item));
                 UpdateAssetIcon(obj, item.icon);
             });
         }
 
         public static List<T> FindAssetsByType<T>() where T : Object
         {
             List<T> assets = new List<T>();
             string[] guids = AssetDatabase.FindAssets(string.Format("t:{0}", typeof(T)));
             for (int i = 0; i < guids.Length; i++)
             {
                 string assetPath = AssetDatabase.GUIDToAssetPath(guids[i]);
                 T asset = AssetDatabase.LoadAssetAtPath<T>(assetPath);
                 if (asset != null)
                 {
                     assets.Add(asset);
                 }
             }
             return assets;
         }
 
         public static void UpdateAssetIcon(Object asset, Sprite icon)
         {
             if (icon && icon.texture)
             {
                 EditorGUIUtility.SetIconForObject(asset, icon.texture);
             }
         }
     }
 }



Desired behavior: On load, take each Item and replace its asset icon with the appropriate item icon.

Actual behavior: On load, the icon for each item is set to whichever one was processed last.

Any idea what's going on here?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by oscarAbraham · Nov 03, 2021 at 09:08 PM

You can't. The icon is set on the MonoScript, so it's shared between all instances of the same class.

You can try using RenderStaticPreview, but it has some quirks, like it doesn't show for thumbnails in the object picker. The quirks change sometimes with the Unity Editor version. It could work for you, at least partially, depending on your use case.

Comment
Add comment · Show 1 · 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
avatar image iohouse · Nov 04, 2021 at 07:37 AM 0
Share

Yup, I was able to confirm this now. EditorGUIUtility.SetIconForObject() is applicable only for GameObjects and MonoScript - also custom icons for GameObjects are saved in the scene (so not compatible with AssetDatabase assets).

Meanwhile, Editor.RenderStaticPreview() worked just fine with the example code provided in the docs (however, no icon preview in the Inspector or Select dialog; only in the Project window). Thanks @oscarAbraham

avatar image
0

Answer by iohouse · Nov 03, 2021 at 06:22 AM

Try moving the [InitializeOnLoad] attribute to the static constructor.

From the manual "Static constructors with this attribute are called when scripts in the project are recompiled" (when Unity loads, and when your scripts are recompiled). https://docs.unity3d.com/ScriptReference/InitializeOnLoadAttribute.html

Comment
Add comment · Show 2 · 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
avatar image TustinJimberlake · Nov 03, 2021 at 02:29 PM 0
Share

Thanks for your quick input!

The [InitializeOnLoad] attribute is only valid on class definitions. You are correct, however, that this will tell Unity to call the static constructor for the class when scripts are recompiled.

The "when" of the behavior is not the issue here, but more so the "what". EditorGUIUtility.SetIconForObject seems to be statically setting the icon for the class overall, even though I am passing in an instance.

avatar image iohouse TustinJimberlake · Nov 03, 2021 at 03:25 PM 1
Share

Yeah, misplaced, it's been a while since I used this attribute and relied on the docs. Heh, I wasn't sure from your post if the code was actually verified to at least be executing.

Have you tried using AssetDatabase.GetCachedIcon() to verify the setting?

If I have some time I'll experiment with the code to see if I get a different result than what you're seeing.

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

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

Converting XML-files into assets 1 Answer

Unity recompilation time slowly increases each time it's recompiled. Why? 0 Answers

Override Asset Preview Thumbnail 1 Answer

ScriptableObject not saving data to asset 2 Answers

Serialization issue with ScriptableObject 0 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