CodingJar

Set Icons for MonoBehaviours in DLLs

Jun 22nd, 2014
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.55 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEditor;
  3.  
  4. public class ChangeIcon : MonoBehaviour
  5. {
  6.     [MenuItem("Coding Jar/Test Icon")]
  7.     static void TestIcon()
  8.     {
  9.         var assetPath = AssetDatabase.GetAssetPath( Selection.activeObject );
  10.         Texture2D icon = AssetDatabase.LoadAssetAtPath( "Assets/Icon.png", typeof(Texture2D) ) as Texture2D;
  11.  
  12. #if USE_SETICONFOROBJECT_NONPERSISTENT
  13.         // This will set the icon for every MonoBehaviour in the DLL, you can do this at start-up...
  14.         var setIconForObject = Types.GetType( "UnityEditor.EditorGUIUtility", "UnityEditor" ).GetMethod( "SetIconForObject", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static );
  15.         foreach (var partObj in AssetDatabase.LoadAllAssetRepresentationsAtPath(assetPath) )
  16.         {
  17.             setIconForObject.Invoke( null, new object[] { partObj, icon } );
  18.         }
  19. #endif
  20.  
  21.         AssetImporter importer = AssetImporter.GetAtPath(assetPath);
  22.         SerializedObject serObj = new SerializedObject( importer );
  23.         var iconMapProperty = serObj.FindProperty( "m_IconMap" );
  24.         if ( iconMapProperty.arraySize < 1 )
  25.         {
  26.             iconMapProperty.InsertArrayElementAtIndex(0);
  27.         }
  28.  
  29.         var iconElementProperty = iconMapProperty.GetArrayElementAtIndex(0);
  30.         var firstProperty = iconElementProperty.FindPropertyRelative( "first" );
  31.         //firstProperty.stringValue = "THis should be the class name";
  32.  
  33.         var secondProperty = iconElementProperty.FindPropertyRelative( "second" );
  34.         secondProperty.objectReferenceValue = icon;
  35.  
  36.         serObj.ApplyModifiedProperties();
  37.         AssetDatabase.WriteImportSettingsIfDirty( assetPath );
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment