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. Dismiss Notice

Question LocalizedString in custom GraphView Editor Window

Discussion in 'Localization Tools' started by Estema_TheWoz, Mar 24, 2022.

  1. Estema_TheWoz

    Estema_TheWoz

    Joined:
    Aug 5, 2021
    Posts:
    7
    Hello everyone,

    I'm building a custom Dialogue Nodes based system using the GraphView API similar to https://github.com/Wafflus/unity-dialogue-system

    I would like to replace basics TextFields by LocalizedStrings.

    The problem is that I don't know if there is a UIElement that I can add to my visual tree implementing all functions, basically like the Editor one :
    upload_2022-3-24_14-40-20.png

    Does anyone knows a simple way to do it?
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
    Estema_TheWoz likes this.
  3. Estema_TheWoz

    Estema_TheWoz

    Joined:
    Aug 5, 2021
    Posts:
    7
    Thanks for your quick answer.

    However, I'm not able to access any SerializedProperty in my context.
    My class is inheriting from the Node class under 'UnityEditor.Experimental.GraphView', I'm drawing manually the UI in a custom Editor Window. Like the following attachment shows
    upload_2022-3-24_17-16-13.png
    I tried to add a LocalizedString SerializeField in my Node Class,
    upload_2022-3-24_17-15-14.png
    then write a custom Editor script for my Node but it doesn't make any effect cause I'm not drawing an inspector window I think.
    upload_2022-3-24_17-15-41.png
    What I want is to replace the highlighted TextField above by a LocalizedString.

    Thanks in advance for any help :)
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
    What does your code that draw the node contents look like?
     
  5. Estema_TheWoz

    Estema_TheWoz

    Joined:
    Aug 5, 2021
    Posts:
    7
    In my GraphView inheriting class I call Draw() on my node
    upload_2022-3-24_20-3-1.png
    which add some VisualElements to different containers upload_2022-3-24_20-8-29.png
    I Add lists, button or texfield to access all my variables by implementing their callback and this is fine cause these are "simple" variables as string or int.
    [EDIT] Forgot to attach the CreateTextField Utility I use sorry
    upload_2022-3-24_20-12-9.png
     
  6. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
    You should be able to do something like this:
    Wrap the value in a ScriptableObject then use the property drawer.

    Code (csharp):
    1. public class SerializedInstance : ScriptableObject
    2.     {
    3.         public LocalizedString localizedString;
    4.     }
    5.  
    6.     public LocalizedString myString = new LocalizedString();
    7.     SerializedInstance m_Instance;
    8.     SerializedObject m_SerializedObject;
    9.     SerializedProperty m_SerializedProperty;
    10.  
    11.     VisualElement CreateLocalizedStringNode()
    12.     {
    13.         if (m_Instance == null)
    14.         {
    15.             m_Instance = ScriptableObject.CreateInstance<SerializedInstance>();
    16.             m_Instance.localizedString = myString;
    17.             m_SerializedObject = new SerializedObject(m_Instance);
    18.             m_SerializedProperty = m_SerializedObject.FindProperty("localizedString");
    19.         }
    20.  
    21.         return new IMGUIContainer(() =>
    22.         {
    23.             m_SerializedObject.Update();
    24.             EditorGUILayout.PropertyField(m_SerializedProperty);
    25.             if (m_SerializedObject.ApplyModifiedProperties())
    26.             {
    27.                 myString = m_Instance.localizedString;
    28.             }
    29.         });
    30.     }