This isn’t the precise option to remedy this drawback. When you generate a construct, the “paths” that existed at edit time not exist in any significant approach. The information on your constructed sport is not saved as png information in folders anymore – it has been pre-digested into codecs which might be extra environment friendly to load straight into video reminiscence as your ranges load.
The paths that you just use with Assets.Load
are usually not really 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 may’t look across the sprite to determine what its key “ought to” be – it is a fully arbitrary string of characters so far as the runtime is anxious.
What it’s best to do as an alternative is create an information construction that teams a group of icons, after which use that construction. A standard 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 widespread to all gadgets of this kind.
}
You’ll be able to create an Apple.asset
that is an occasion of this kind 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 kind 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 information/behaviours.
}
That is probably extra environment friendly than utilizing Assets.Load
, as a result of it would embody in your construct solely the sprites which might be really referenced someplace, whereas the Assets
folder has to incorporate all of its contents, even outdated/unused property, as a result of it will possibly’t inform whether or not some code will ultimately ask for his or her key string.
The kind object additionally probably reduces information duplication between cases of the identical merchandise kind – a model of the flyweight sample.