0

In several of my Unity projects, I have editor scripts setup to import & configure external assets into prefabs. Typically I'm taking a model or sprite, some additional data like unit stats, and creating a new prefab with my unit class and configuring its values.

Most of these values should not be modified at runtime -- things like max health, unit stats, or unit type. However, the editor scripts need to be able to access them, but since they're automatically in the editor assembly, the best I can work with is public fields. Is there a better solution to this problem?

0

1 Answer 1

0
  1. Do not use public fields for exposing stuff in inspector. Use [SerializeField] attribute instead:

    [SerializeField] private Sprite myPrivateSpriteVariable;

  2. You can put all the assets under Resources folder, and load it via scripting at runtime, by name. In this case you only need something like config file - it can be a usual script, with names defined as constants, or it can be some text file - for example, in JSON format. (I personally dont recommend this, because it will increase loading time, in general. But for some cases it can be a deal)

  3. You can implement some ScriptableObject asset provider - you create an asset file, which located somewhere in project, not in scene. You drag all your needed assets into fields of this asset, in inspector. Then, if you need some prefap, you can assign this scriptable object, and get particular asset. Or you can use, for example, singleton, or dependency injection, to get this scriptable object from your code, without inspector.

  4. For settings, like "max height", I can recommend to use scriptable objects again. You can easily serialize it into JSON files, and store in PlayerPrefs, for example.

  5. Or, again, for settings, you can just implement a class with constants, which you will adjust from code editor.

  6. Or, you can mix 4 and 5 - create a settings class, and store it in PlayerPrefs as a single string, using unity's JSON serialization.

Not the answer you're looking for? Browse other questions tagged or ask your own question.