Closed Akua12 closed 2 years ago
Hi, It's always nice to see some new person around.
You can swap image without changing the scene. You should have in your scene hierarchy the image itself. Instead of doing your scene change,
[SerializeField]
private Image _backgroundImage;
It will appear on the editor view when you select the script from your hierarchy viewer. drag and drop the image (I mean the hierarchy object that is holding the image)
b) get a reference to the new image content you want to have Same as before, I tend to do it by the editor window, as it is a bit easier and enough for a prototype. For a small project, you can do it "manually", and search later on how to load efficiently images for your game. You will have a better understanding of the technical aspect at that point. But for today, I think the easiest solution is the best. Out of your function, but still in your class, write the following
[SerializeField]
private Sprite[] _allBackgroundSprites;
[SerializeField]
private string[] _allBackgroundNames;
private Dictionary<string, Sprite> _allBackgrounds = new Dictionary<string, Sprite>();
After that, you can drag and drop from your "folder view" in the unity editor to your script list (the first parameter of this new snippet). It will accept only the image from the folder, and it will hold everyone.
The second parameter that I created is a way to store the background name.
IT SHOULD HAVE THE SAME ENTRIES ORDER AND NUMBER AS YOUR SPRITES ITSELF.
It will be the name the script will see (from your ink) to know which background it should load (in your case, bedroom
is the name for your bedroom image)
This step is still a bit longer, as you need to hold it together as you can see I put a third parameter that is a Dictionary. You won't see this one appears on the unity editor, but that the one you want to use to fetch the image.
So we will want to fill this dictionary (think as the Dictionary as a key value ... Dictionary. You want to get a Sprite (the visual image content you will be able to fill in your unity Image
container) from a text (or in cs language a string
) prompt.
You will need one more substep. To fill this new dictionary with the values from the unity editor. If you have an existing Awake function, just add the code to the bottom of that function. Else, create your Awake method (outside of any other method, but still inside your class)
void Awake()
{
}
and inside the body of that method, just add the following
if(_allBackgroundSprites.Length != _allBackgroundNames.Length) {
Debug.LogError("Missing some BackgroundSprites / BackgroundNames");
return;
}
for (int i = 0; I < _allBackgroundSprites.Length; i++)
{
_allBackground.Add(_allBackgroundNames[i], _allBackgroundSprites[i]);
}
foreach(var tag in _story.currentTags){
Sprite tagSprite = null;
if(_allBackgrounds.TryGetValue(tag, out tagSprite)){
Debug.Log(Loading "+tag+" as background "+tagSprite.Name);
_backgroundImage.Sprite = tagSprite;
}
}
That should do and update your background image. You can have the same idea for loading anything else in your scene. For example, the expression, and the portrait of the characters.
I hope my wall of text didn't discourage you. It's a bit verbose, but it's an easy way, and I tried to explain what I was doing.
Thank you so much! I really appreciate that you explained each step too! It works now :D
Hello!
I m trying to make a VN through Unity and it was going well until I needed to change the background image. The background image DOES change using the tag, but it also resets the dialogue and I am not sure why. I am wondering if I even need to change scenes or if there is a way to just change the image that's already set in the MainScene. I am pretty new to Unity and coding in general, so if anyone is able to help it would be highly appreciated!