Search Unity

Resolved Generated code and partial class

Discussion in 'Scripting' started by LeMecEnPyjama, May 11, 2022.

  1. LeMecEnPyjama

    LeMecEnPyjama

    Joined:
    Jun 1, 2021
    Posts:
    13
    I'll try to be as clear as possible

    I am making Localization Manager and try to keep it as auto-sufficient as possible as it will be used in various different project at the same time
    The localization part is working great but I am trying to add some sort of "auto completion" system to help when typing localization key
    (Here at the bottom of the script component)
    LocalizerAuto.png

    To display the auto completion, I'm looping trough a list of string all generated from a .csv file, along with some "LocalizerKey" that are used for other things, in a script called "LocalizerBank" that is unique to the project, like this :

    Code (CSharp):
    1. // THIS FILE IS GENERATED AUTOMATICALY IN LocalizerManager
    2.  
    3. namespace Polonium
    4. {
    5.     public static partial class LocalizerBank
    6.     {
    7.         public static LocalizerKey MainMenuPlay = new LocalizerKey("MainMenuPlay");
    8.         public static LocalizerKey MainMenuOptions = new LocalizerKey("MainMenuOptions");
    9.         public static LocalizerKey MainMenuCredits = new LocalizerKey("MainMenuCredits");
    10.         public static LocalizerKey MainMenuQuit = new LocalizerKey("MainMenuQuit");
    11.  
    12.         public static string[] allKey = {"MainMenuPlay","MainMenuOptions","MainMenuCredits","MainMenuQuit"};
    13.     }
    14. }
    The list "allKey" is used in the LocalizerAuto as such :

    Code (CSharp):
    1.             string previsual = "";
    2.             foreach (string key in LocalizerBank.allKey)
    3.             {
    4.                 if (key.StartsWith(myTarget.locKey.key))
    5.                 {
    6.                     previsual = key;
    7.                     break;
    8.                 }
    9.             }
    10.             GUILayout.Label(previsual, EditorStyles.centeredGreyMiniLabel);
    The thing is, when I'm importing this code in a new project, where the LocalizerBank was never generated, of course it bugs because allKey does not exist as well
    So is there a way to fix this ? Like using a #define in LocalizerBank that could be global to the project and putting the problematic code between #if #end (if that's possible) or something else ?
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    How about using a partial method instead? The non-generated file would contain:
    Code (csharp):
    1.  
    2. public partial static string[] GetAllKeys();
    3.  
    and the generated file would fill in the body:
    Code (csharp):
    1.  
    2. private static string[] allKeys = {"MainMenuPlay","MainMenuOptions","MainMenuCredits","MainMenuQuit"};
    3.  
    4. public partial static string[] GetAllKeys()
    5. {
    6.    return allKeys;
    7. }
    8.  
    You'd just have to change how you access the arrays slightly in your consumer code.
     
    Kurt-Dekker and Bunny83 like this.
  3. LeMecEnPyjama

    LeMecEnPyjama

    Joined:
    Jun 1, 2021
    Posts:
    13
    I didn't know you could do that, this could be usefull

    However the problem still remain, as when the generated part is not yet generated it displays an error that says "GetAllKeys() need an implementation because it has accessibility modificator"
    upload_2022-5-12_12-12-41.png
    (sorry the error is in french)
     
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Ah, right. Sorry about that. You'll have to generate the data inside the method instead. This works:

    Code (csharp):
    1.  
    2. // file 1
    3. public static partial class LocalizerBank
    4. {
    5.    private static List<string> _allKeys = new List<string>();
    6.  
    7.    public static List<string> allKeys
    8.    {
    9.        get
    10.        {
    11.            if (_allKeys.Count == 0)
    12.                GenerateKeys();
    13.  
    14.            return _allKeys;
    15.        }
    16.    }
    17.  
    18.    static partial void GenerateKeys();
    19. }
    20.  
    21. /*
    22. // file 2
    23. // remove the comment block to test
    24. public partial class LocalizerBank
    25. {
    26.    static partial void GenerateKeys()
    27.    {
    28.        _allKeys.Add("Main Menu");
    29.        _allKeys.Add("Pause Menu");
    30.        _allKeys.Add("Options");
    31.    }
    32. }
    33. */
    34.  
    35. // test
    36. public sealed class Help_PartialStaticClass : MonoBehaviour
    37. {
    38.    private void Start()
    39.    {
    40.        // prints 0 if commented out, 3 if uncommented
    41.        Debug.LogFormat
    42.        (
    43.            "all keys count: {0}, content: {1}",
    44.            LocalizerBank.allKeys.Count,
    45.            string.Join(",", LocalizerBank.allKeys)
    46.        );
    47.    }
    48. }
    49.  
    50.  
     
    Kurt-Dekker and LeMecEnPyjama like this.
  5. LeMecEnPyjama

    LeMecEnPyjama

    Joined:
    Jun 1, 2021
    Posts:
    13
    Thanks a lot, it works perfectly !