- Home /
The question is answered, right answer was accepted
Change 2D background image across all scenes.
I'm making a 2D app with Unity (using only UI elements) that has multiple scenes that all use the same background image. I want the user to be able to change this image in options. I've made options scene and can change the image in this options scene by clicking a corresponding button but how do I make it use this image as a new default background image for all scenes?
Answer by miraisen754 · Jul 19, 2020 at 08:39 AM
Figured it out by myself, used PlayerPrefs for it. In Options script for each button that sets specific image I've created a method that loads background from resources:
public void bkg01()
{
Image.AddComponent(typeof(Image));
Background01 = Resources.Load<Sprite>("Background01");
Image.GetComponent<Image>().sprite = Background01;
PlayerPrefs.SetInt("background", 1); // this sets int in PlayerPrefs
}
Then I've created a separate background script to attach to Image in each scene.
{
public class BackgroundScript : MonoBehaviour
{
public GameObject Image = null;
private Sprite Background01;
private void Awake()
{
int bkg = PlayerPrefs.GetInt("background"); //get int from PlayerPrefs
if (bkg == 1) //set corresponding background as Image
{
Image.AddComponent(typeof(Image));
Background01 = Resources.Load<Sprite>("Background01");
Image.GetComponent<Image>().sprite = Background01;
}
}
It ended up a bit bloated it seems (been learning Unity for a bit over a month) and there's probably a better way to do this, but it worked.
Follow this Question
Related Questions
Image overlay in center of screen 2 Answers
Change size of an UI object ( Javascript ( Unityscript) ) 0 Answers
How to change the end of image with type "Filled" 1 Answer
What is the best method to display different pictures? 0 Answers
Changing multiple UI.Image source image downloaded via WWW class 0 Answers