Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question How do you write a custom TextMeshPro Component?

Discussion in 'UGUI & TextMesh Pro' started by pioj, Dec 24, 2023.

  1. pioj

    pioj

    Joined:
    Nov 5, 2012
    Posts:
    34

    Hi, I'm trying to add a few options more to UGUI controls by feeding them data from an ScriptableObject. This way I don't need extra scripts to send messages to the UI.

    My script inherits from TextMeshProUGUI :
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using TMPro;
    6. using UnityEngine.EventSystems;
    7.  
    8. public enum TowerPlantBindType {level, rentTime, currentCost, currentRate}
    9.  
    10. public class TowerPlantText : TextMeshProUGUI
    11. {
    12.     public SO_TowerPlant dataSource;
    13.     public TowerPlantBindType bindedTo;
    14.    
    15.     void Update()
    16.     {
    17.         if (dataSource == null)    return;
    18.        
    19.         switch(bindedTo)
    20.         {
    21.         case TowerPlantBindType.level:
    22.             text = $"Level: {dataSource.level}";      
    23.             break;
    24.         case TowerPlantBindType.rentTime:
    25.             text = $"Rent Time: {dataSource.rentTime}";
    26.             break;
    27.         case TowerPlantBindType.currentCost:
    28.             text = $"Current Cost: {dataSource.currentCost}";
    29.             break;
    30.         case TowerPlantBindType.currentRate:
    31.             text = $"Current Rate: {dataSource.currentRate}";
    32.             break;
    33.         default:
    34.             text = $"Current Cost: {dataSource.currentCost}";
    35.             break;
    36.         }
    37.     }
    38. }
    39.  
    and the CustomEditor is from TMPro.EditorUtilities.TMP_EditorPlanelUI :
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4. using TMPro;
    5. using System.Collections;
    6.  
    7. [CustomEditor(typeof(TowerPlantText))]
    8. public class TowerPlantTextEditor :  TMPro.EditorUtilities.TMP_EditorPanelUI
    9. {
    10.     public override void OnInspectorGUI() {
    11.  
    12.         TowerPlantText component = (TowerPlantText)target;
    13.  
    14.         base.OnInspectorGUI();
    15.        
    16.         component.dataSource = (SO_TowerPlant)EditorGUILayout.ObjectField("Data Source",component.dataSource, typeof(SO_TowerPlant));
    17.        
    18.         if (component.dataSource == null) return;
    19.        
    20.         component.bindedTo = (TowerPlantBindType)EditorGUILayout.EnumPopup("Binded To",component.bindedTo);
    21.     }
    22. }
    23.  
    .

    I think I got the Editor Class wrong, and there's anorther one I must use instead. But I can't find anything related in the manual, sorry.

    Also, I have this error where the SO_var field resets its value to null everytime I enter Play mode....

    What's the proper way to write a custom editor from TextMeshPro's?

    Thank you.
     
    Last edited: Jan 2, 2024