Search Unity

Question Automation: Type Options / Node Library

Discussion in 'Visual Scripting' started by Deathwing, Nov 1, 2022.

  1. Deathwing

    Deathwing

    Joined:
    May 8, 2013
    Posts:
    25
    Hey everyone,

    is there a way to add node libraries and type options via code? Or is there maybe a pre-existing attribute I can add to an assembly/class in order to add it... The manual adding is sooo annoying :)

    Cheers
     
    REDACT3D_ likes this.
  2. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    2,063
    Use [IncludeInSettings(true)] attribute from Unity.VisualScripting namespace on your custom class. Still have to manually regen after.
     
    REDACT3D_ likes this.
  3. Deathwing

    Deathwing

    Joined:
    May 8, 2013
    Posts:
    25
    Because I use assembly references, and I have somewhere in my assembly a #if UNITY_EDITOR, all the included settings code is not working :D That library is annoying me more and more each day xD
     
  4. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    2,063
    Yea, they F***ed it up after UVS 1.76. #if UNITY_EDITOR shouldn't mark assemblies as editor only, it's already stripped from the final build.
     
  5. Deathwing

    Deathwing

    Joined:
    May 8, 2013
    Posts:
    25
    Well I just wrote my own node generation, and its working flawlessly.
    Basically don't use the generate button from them if you need custom assembly, and rather use mine, after of course adjusting it to your own needs :)

    Basically just replace 'YourGame.' with your Root assembly name to get it working.

    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Reflection;
    4.  
    5. using Unity.VisualScripting;
    6.  
    7. using UnityEditor;
    8. using UnityEngine;
    9.  
    10. namespace YourGame.Core.VisualScripting.Editor
    11. {
    12.     [InitializeOnLoad()]
    13.     public static class NodeGeneration
    14.     {
    15.         enum Operation
    16.         {
    17.             Rebuild,
    18.             Update
    19.         }
    20.  
    21.         static NodeGeneration() => CheckNodeGeneration(Operation.Update);
    22.  
    23.         [MenuItem("YourGame/Rebuild Nodes")]
    24.         static void ForceCheckNodeGeneration() => CheckNodeGeneration(Operation.Rebuild);
    25.  
    26.         static void CheckNodeGeneration(Operation operation)
    27.         {
    28.             UpdateCodebaseSettings();
    29.             UpdateUnitBase(operation);
    30.         }
    31.  
    32.         static void UpdateCodebaseSettings()
    33.         {
    34.             var editorAssemblyCacheField = typeof(Codebase).GetField("_editorAssemblyCache", BindingFlags.NonPublic | BindingFlags.Static);
    35.             var editorAssemblyCache = (Dictionary<Assembly, bool>)editorAssemblyCacheField.GetValue(null);
    36.  
    37.             foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
    38.                 if (assembly.FullName.StartsWith("YourGame."))
    39.                     editorAssemblyCache[assembly] = assembly.FullName.Contains("Editor");
    40.  
    41.             Codebase.UpdateSettings();
    42.         }
    43.  
    44.         static void UpdateUnitBase(Operation operation)
    45.         {
    46.             try
    47.             {
    48.                 ProgressUtility.DisplayProgressBar("Checking for codebase changes...", null, 0);
    49.  
    50.                 switch (operation)
    51.                 {
    52.                     case Operation.Rebuild:
    53.                         UnitBase.Rebuild();
    54.                         break;
    55.                     case Operation.Update:
    56.                         var requiresUpdateProperty = typeof(UnitBase).GetProperty("requiresUpdate", BindingFlags.NonPublic | BindingFlags.Static);
    57.                         var requiresUpdate = (bool)requiresUpdateProperty.GetValue(null);
    58.                         if (requiresUpdate)
    59.                             UnitBase.Update();
    60.                         break;
    61.                 }
    62.             }
    63.             catch (Exception ex)
    64.             {
    65.                 Debug.LogError($"Failed to update node options.\nRetry with '{UnitOptionUtility.GenerateUnitDatabasePath}'.\n{ex}");
    66.             }
    67.             finally
    68.             {
    69.                 ProgressUtility.ClearProgressBar();
    70.             }
    71.         }
    72.     }
    73. }
     
    Last edited: Nov 1, 2022