Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Component icon change

Discussion in 'Editor & General Support' started by tribaleur, Apr 12, 2020.

  1. tribaleur

    tribaleur

    Joined:
    Jan 19, 2017
    Posts:
    34
    Hello,

    I'm trying to change my component's icon via script. With reflection it's possible and it works well when i have juste one component on my gameobject.

    But when i add a second component of the same type in the same gameobject, the icon change is weird. Each component change the icone of the second one. I don't understand why.

    Here what it does :

    upload_2020-4-12_22-19-57.png

    MyCustomClass.cs :
    Code (CSharp):
    1. [Serializable]
    2.     public class MyCustomClass : MonoBehaviour{
    3.         public Texture2D t1 ;
    4.         public Texture2D t2 ;
    5.         public int selectedIcon = 0;
    6.     }
    My custom editor :
    Code (CSharp):
    1. [CustomEditor(typeof(MyCustomClass), true)]
    2.     public class testeditor : Editor{
    3.         private MyCustomClass _myClass;
    4.  
    5.         public void OnEnable () {
    6.             this._myClass = (MyCustomClass) target;
    7.         }
    8.  
    9.         public override void OnInspectorGUI () {
    10.             if(this._myClass.selectedIcon > 1) { this.SetUnityObjectIcon(this._myClass, this._myClass.t2); }
    11.             else{ this.SetUnityObjectIcon(this._myClass, this._myClass.t1); }
    12.             this._myClass.selectedIcon = EditorGUILayout.IntField("Selected Icon", this._myClass.selectedIcon);
    13.             this._myClass.t1 = EditorGUILayout.ObjectField("T1", this._myClass.t1, typeof(Texture2D), false) as Texture2D;
    14.             this._myClass.t2 = EditorGUILayout.ObjectField("T2", this._myClass.t2, typeof(Texture2D), false) as Texture2D;
    15.         }
    16.        
    17.         public void SetUnityObjectIcon(UnityEngine.Object unityObject, Texture2D icon) {
    18.             if(unityObject != null) {
    19.                 Type editorGUIUtilityType = typeof(EditorGUIUtility);
    20.                 BindingFlags bindingFlags = BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.NonPublic;
    21.                 object[] args = new object[] { unityObject, icon };
    22.                 editorGUIUtilityType.InvokeMember("SetIconForObject", bindingFlags, null, null, args);
    23.             }
    24.         }
    25.     }
     
  2. tribaleur

    tribaleur

    Joined:
    Jan 19, 2017
    Posts:
    34
    So after few test, i updated my Editor code to change the icon only when the int value is modified. And it's interesting because now, all component's icon are changed in same time with the same value.

    Here my new editor code :
    Code (CSharp):
    1. [CustomEditor(typeof(MyCustomClass), true)]
    2.     public class testeditor : Editor{
    3.         private MyCustomClass _myClass;
    4.  
    5.         public void OnEnable () {
    6.             this._myClass = (MyCustomClass) target;
    7.         }
    8.  
    9.         public override void OnInspectorGUI () {          
    10.             int newInt = EditorGUILayout.IntField("Selected Icon", this._myClass.selectedIcon);
    11.             this._myClass.t1 = EditorGUILayout.ObjectField("T1", this._myClass.t1, typeof(Texture2D), false) as Texture2D;
    12.             this._myClass.t2 = EditorGUILayout.ObjectField("T2", this._myClass.t2, typeof(Texture2D), false) as Texture2D;
    13.  
    14.             if(newInt != this._myClass.selectedIcon) {
    15.                 this._myClass.selectedIcon = newInt;
    16.                 if(this._myClass.selectedIcon > 1) { this.SetUnityObjectIcon(this._myClass, this._myClass.t2); }
    17.                 else{ this.SetUnityObjectIcon(this._myClass, this._myClass.t1); }
    18.             }
    19.         }
    20.        
    21.         public void SetUnityObjectIcon(UnityEngine.Object unityObject, Texture2D icon) {
    22.             if(unityObject != null) {
    23.                 Type editorGUIUtilityType = typeof(EditorGUIUtility);
    24.                 BindingFlags bindingFlags = BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.NonPublic;
    25.                 object[] args = new object[] { unityObject, icon };
    26.                 editorGUIUtilityType.InvokeMember("SetIconForObject", bindingFlags, null, null, args);
    27.             }
    28.         }
    29.     }
    Here the new result :
    upload_2020-4-13_10-31-55.gif
     
  3. tribaleur

    tribaleur

    Joined:
    Jan 19, 2017
    Posts:
    34
    Ok so i tried to create an other component of the same type in an other gameobject and it's icon is change in the same time than the other component.
    So i guess that the icon change is apply to the type of the component instead of to the component itself. I think that what i want to do is not possible.