Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEditor;
- public class ChangeIcon : MonoBehaviour
- {
- [MenuItem("Coding Jar/Test Icon")]
- static void TestIcon()
- {
- var assetPath = AssetDatabase.GetAssetPath( Selection.activeObject );
- Texture2D icon = AssetDatabase.LoadAssetAtPath( "Assets/Icon.png", typeof(Texture2D) ) as Texture2D;
- #if USE_SETICONFOROBJECT_NONPERSISTENT
- // This will set the icon for every MonoBehaviour in the DLL, you can do this at start-up...
- var setIconForObject = Types.GetType( "UnityEditor.EditorGUIUtility", "UnityEditor" ).GetMethod( "SetIconForObject", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static );
- foreach (var partObj in AssetDatabase.LoadAllAssetRepresentationsAtPath(assetPath) )
- {
- setIconForObject.Invoke( null, new object[] { partObj, icon } );
- }
- #endif
- AssetImporter importer = AssetImporter.GetAtPath(assetPath);
- SerializedObject serObj = new SerializedObject( importer );
- var iconMapProperty = serObj.FindProperty( "m_IconMap" );
- if ( iconMapProperty.arraySize < 1 )
- {
- iconMapProperty.InsertArrayElementAtIndex(0);
- }
- var iconElementProperty = iconMapProperty.GetArrayElementAtIndex(0);
- var firstProperty = iconElementProperty.FindPropertyRelative( "first" );
- //firstProperty.stringValue = "THis should be the class name";
- var secondProperty = iconElementProperty.FindPropertyRelative( "second" );
- secondProperty.objectReferenceValue = icon;
- serObj.ApplyModifiedProperties();
- AssetDatabase.WriteImportSettingsIfDirty( assetPath );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment