Z
Z
Zefirot2021-06-14 18:54:08
Unity
Zefirot, 2021-06-14 18:54:08

How to set prefab images correctly?

In my prefab (of which there are many), the appearance (pictures) will change during the game, I asked the prebuff a script of this kind

public Image ImageTest;
private Sprite Image1, Image2, Image3, Image4, Image5;

private void Awake(){
  Image1 = Resources.Load <Sprite>("Images/Image1");
  Image2 = Resources.Load <Sprite>("Images/Image2");
  Image3 = Resources.Load <Sprite>("Images/Image3");
  Image4 = Resources.Load <Sprite>("Images/Image4");
  Image5 = Resources.Load <Sprite>("Images/Image5");
  }

public void ReplacePrefab(int t){
  switch(t){
    case 1: ImageTest.GetComponent<Image>().sprite = Image1; break;
    case 2: ImageTest.GetComponent<Image>().sprite = Image2; break;
    case 3: ImageTest.GetComponent<Image>().sprite = Image3; break;
    case 4: ImageTest.GetComponent<Image>().sprite = Image4; break;
    case 5: ImageTest.GetComponent<Image>().sprite = Image5; break;
    }
  }

(the names are naturally different and not ordinal, this is so for an example)
The question is - am I setting pictures correctly for the prefab?
It seems to me that this is how every prefab is created when it loads these pictures into itself, it seems to me that in the future it will load memory more, I think I can create one object and put pictures in it, and then take links to them from it, or how right?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GavriKos, 2021-06-14
@Zefirot

You think correctly. And even if you add LINKS to already loaded pictures into another object, nothing will change.
But maybe there is nothing wrong with that. We must proceed from how quickly the pictures should change. If we allow a lag and loading there, then what prevents them from loading them only if a change is necessary?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question