Hoow to set image of an imagebox in unity 2d
Hello to everyone :)
I have a very simple question to do, I search for some guide, but I don't found something helpfull for me.This is what I need :
There are 2 (or more) images in a scene, and a button. I want to set the image of the first imagebox as the image of the second imagebox, and reset che image of the first. in visualc# (I know I am a newbie at programming :) ) the code will be this :
private void button1_Click(object sender, EventArgs e)
{
image2.BackgroundImage = image1.BackgroundImage;
image1.BackgroundImage = null;
}
I don't want to import an image from a file.
I see that there is a button OnClick() funtion
But I want to set what there is in the image1 and not a fixed resource. Thanks for the help.
Answer by say_forever · Nov 09, 2015 at 08:06 AM
The simple way is importing an limpid image and drag to the Image.sprite.
but also you can write a script like this, and drag this script to the button.
using UnityEngine;
using UnityEngine.UI;
public class resetImage : MonoBehaviour {
public Image img1, img2;
// Use this for initialization
void Start()
{
Button btn = GameObject.Find("Button").GetComponent<Button>();
btn.onClick.AddListener(delegate ()
{
img1.sprite = img2.sprite;
img2.sprite = null;
});
}
}
Answer by Durlo · Nov 09, 2015 at 12:35 PM
Thanks you for your answer, but how I say I am very newbie and I need a some other specifications.
You use img1/img2.sprite, this means that I need manually load an image with the code, there is a possiblity to use an image loaded by project assets ?
Probably is better if I put an image of my test program :) How you see, I need to move the image of image1 to the last image and complete the sentence this is an egg. But the image2 can be either A or AN
you create a folder named Resources below Assets and use following code to load and assign image.
Texture texture = Resources.Load("filename"); img.texture = texture;
Your answer
![](https://koobas.hobune.stream/wayback/20220612044251im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to add a picture 0 Answers
How can I change one canvas image to another? 1 Answer
Movement Animation Looping when it Shouldn't 0 Answers