Search Unity

Utilities Automatically generate helper class for Resources.Load

Discussion in 'Tools In Progress' started by KuraiAndras, Oct 15, 2021.

  1. KuraiAndras

    KuraiAndras

    Joined:
    Aug 23, 2021
    Posts:
    10
    I am proud to announce our company's first open-source Unity project, the UnityResourceGenerator

    Repo: https://github.com/AutSoft/UnityResourceGenerator
    Documentation: https://autsoft.github.io/UnityResourceGenerator/

    The library is a Unity Editor tool which lets you generate a helper class for resource and scene loading, with the press of a button. The library also lets users create custom code to run during the generation. Given the following folder structure:

    Code (CSharp):
    1. Assets/
    2. ├─ Resources/
    3. │  ├─ Coin.prefab
    4. │  ├─ Coin.mp3
    5. ├─ Scenes/
    6. │  ├─ CoinRain.unity
    The following source file will be generated:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.SceneManagement;
    3.  
    4. namespace Sample
    5. {
    6.     // ReSharper disable PartialTypeWithSinglePart
    7.     // ReSharper disable InconsistentNaming
    8.     // ReSharper disable IncorrectBlankLinesNearBraces
    9. #pragma warning disable IDE0079 // Remove unnecessary suppression
    10. #pragma warning disable RCS1036 // Remove redundant empty line.
    11.     public static partial class ResourcePaths
    12.     {
    13.  
    14.         public static partial class Scenes
    15.         {
    16.             public const string CoinRain = "Scenes/CoinRain";
    17.  
    18.             public static void LoadCoinRain(LoadSceneMode mode = LoadSceneMode.Single) =>
    19.                 SceneManager.LoadScene(CoinRain, mode);
    20.  
    21.             public static AsyncOperation LoadAsyncCoinRain(LoadSceneMode mode = LoadSceneMode.Single) =>
    22.                 SceneManager.LoadSceneAsync(CoinRain, mode);
    23.         }
    24.  
    25.         public static partial class Prefabs
    26.         {
    27.             public const string Coin = "Coin";
    28.             public static GameObject LoadCube() => Resources.Load<GameObject>(Coin);
    29.         }
    30.  
    31.         public static partial class AudioClips
    32.         {
    33.             public const string Coin = "Coin";
    34.             public static AudioClip LoadCoin() => Resources.Load<AudioClip>(Coin);
    35.         }
    36.     }
    37. }
    The recommended way of installation is with OpenUPM

    Code (CSharp):
    1. openupm add com.autsoft.unityresourcegenerator
    If you have any problems with the library pleasre create an issue in the repository