This isn’t the precise method to clear up this downside. When you generate a construct, the “paths” that existed at edit time not exist in any significant approach. The info in your constructed recreation is not saved as png information in folders anymore – it has been pre-digested into codecs which might be extra environment friendly to load immediately into video reminiscence as your ranges load.
The paths that you just use with Assets.Load
are usually not truly paths at runtime, simply lookup keys in a dictionary, which map to offsets and spans inside opaque binary blobs. The sprite is not a definite named file “in” that location, so you possibly can’t look across the sprite to determine what its key “ought to” be – it is a utterly arbitrary string of characters so far as the runtime is worried.
What it is best to do as a substitute is create a knowledge construction that teams a set of icons, after which use that construction. A typical approach I am going to do that is with the kind object sample:
[CreateAssetMenu(filename = "NewItemType.asset", menuName = "Data/Item Type")]
public class InventoryItemType : ScriptableObject {
public string displayName;
public int maximumStackSize;
// Assign these at edit time.
public Sprite singleIcon;
public Sprite multipleIcon;
public Sprite GetSpriteForQuantity(int amount) {
return amount > 1 ? multipleIcon : singleIcon;
}
// Different stuff frequent to all gadgets of this sort.
}
You’ll be able to create an Apple.asset
that is an occasion of this sort for the “apple” stock merchandise, and one other occasion for one another merchandise kind you care about.
The runtime stock merchandise occasion (or the UI button representing it) references this sort object:
public class InventoryItem : MonoBehaviour {
public InventoryItemType itemType;
public int amount;
public void UpdateVisual(Picture uiImage, TextMeshProUGUI label) {
uiImage.sprite = itemType.GetSpriteForQuantity(amount);
label.textual content = amount.ToString();
}
// Different runtime stock merchandise knowledge/behaviours.
}
That is doubtlessly extra environment friendly than utilizing Assets.Load
, as a result of it would embrace in your construct solely the sprites which might be truly referenced someplace, whereas the Assets
folder has to incorporate all of its contents, even outdated/unused property, as a result of it may well’t inform whether or not some code will finally ask for his or her key string.
The kind object additionally doubtlessly reduces knowledge duplication between cases of the identical merchandise kind – a model of the flyweight sample.