0

I'm finding myself inputting some nasty bits of hard-coded sprite asset string references in my code like this:

[SerializeField] TMP_Text _purchaseLabel;

private void UpdateState()
{
     _purchaseLabel.text = "<sprite name=\"coin\">" + _currency;
}

Naturally this Sprite Asset string could be used across many different areas in the game, and I could keep a bunch of constant that would maintain these references. I do wonder however if there's a smarter way about it, so if for example a Sprite Asset name changes, I wouldn't need to also update those constants in the code.

Thanks!

Dror
  • 2,548
  • 4
  • 33
  • 51
  • I think you have got all the ideas you need. For example, you could use a central script for managing the name references and then don't assign these names by hand but rather read the sprite names from the path they are stored in with e.g. ```Path.GetFileNameWithoutExtension(string path)```. Within the name managing script you could implement a mechanism which checks for name changes at desired points in time. Does that by any chance help? – anjelomerte Jan 25 '23 at 16:41
  • Thanks! I'm not sure Path.GetFileNameWithoutExtension will work on its own, as some sprites might be part of a big sprite sheet, and some could be in separate files, also each could have different indexes and so on. In the meantime, I just went with the static variables approach. – Dror Jan 30 '23 at 08:57
  • 1
    That's a fair point with big sprite sheets containing multiple sprites. Maybe some other clever people can come up with sth:) – anjelomerte Jan 31 '23 at 09:27

0 Answers0