C# 如何在代码C中更改背景图像#

C# 如何在代码C中更改背景图像#,c#,unity3d,C#,Unity3d,我有一个unity项目,我使用了五个切换,设置了一个图像作为背景,另一个图像作为复选标记。 我想使用代码(C#)设置这两个图像 我相信这就是您想要的: Image myImageComponent; public Sprite myImage; //Drag your first sprite here in inspector. void Start() //Lets start by getting a reference to your image component. { my

我有一个unity项目,我使用了五个切换,设置了一个图像作为背景,另一个图像作为复选标记。 我想使用代码(C#)设置这两个图像


我相信这就是您想要的:

Image myImageComponent;
public Sprite myImage; //Drag your first sprite here in inspector.

void Start() //Lets start by getting a reference to your image component.
{
    myImageComponent = GetComponent<Image>(); //Your image component is the one attached to this gameObject.
}

public void SetImage() //method to set our first image
{
    myImageComponent.sprite = myImage;
}
imagemyimagecomponent;
公众形象//在inspector中将您的第一个精灵拖到这里。
void Start()//让我们从获取图像组件的引用开始。
{
myImageComponent=GetComponent();//您的图像组件就是附加到此游戏对象的组件。
}
public void SetImage()//用于设置第一个映像的方法
{
myImageComponent.sprite=myImage;
}

谢谢大家对我的支持,我找到了这个问题的答案。 答案是:

首先,我们需要声明变量(用于引用图像),然后将图像从unity设置为变量

public Sprite MondayRW;
public Sprite MondayRG;
public Sprite TuesRW;
public Sprite TuesRG;
public Sprite WendRW;
public Sprite WendRG;
public Sprite ThurRW;

//If Phone language is Russian, Set Russian value images to tonggles.
        if(Application.systemLanguage == SystemLanguage.Russian)
        { 
            GameObject.Find("BackgroundMon").GetComponent<Image>().sprite = MondayRW;
            GameObject.Find("CheckmarkMon").GetComponent<Image>().sprite = MondayRG;
            GameObject.Find("BackgroundTue").GetComponent<Image>().sprite = TuesRW;
            GameObject.Find("CheckmarkTue").GetComponent<Image>().sprite = TuesRG;
            GameObject.Find("BackgroundWed").GetComponent<Image>().sprite = WendRW;
            GameObject.Find("CheckmarkWed").GetComponent<Image>().sprite = WendRG;
            GameObject.Find("BackgroundThu").GetComponent<Image>().sprite = ThurRW;

        }
公共精灵周一;
公共雪碧星期一;
公共雪碧酒庄;
公共雪碧;
公共精灵;
公共雪碧温德尔格;
公共雪碧星期四;
//如果电话语言为俄语,请将俄语值图像设置为tonggles。
if(Application.systemLanguage==systemLanguage.俄语)
{ 
GameObject.Find(“BackgroundMon”).GetComponent().sprite=MondayRW;
GameObject.Find(“CheckmarkMon”).GetComponent().sprite=MondayRG;
GameObject.Find(“BackgroundTue”).GetComponent().sprite=TuesRW;
GameObject.Find(“CheckmarkTue”).GetComponent().sprite=TuesRG;
GameObject.Find(“backgrounded”).GetComponent().sprite=WendRW;
GameObject.Find(“CheckmarkWed”).GetComponent().sprite=WendRG;
GameObject.Find(“BackgroundThu”).GetComponent().sprite=ThurRW;
}

我从
GameObject.find(“BackgroundMon”)
中找到替换图像的位置,然后将图像设置到该位置。

Hi。你试过什么。因此,这不是一个代码编写服务,请说明你已经做了什么,解释它做了什么,它应该做什么,并提供我不知道,所以我需要一步一步。我有两个条件,因此,如果一个条件为真,设置一个图像,否则设置另一个图像。这就是为什么我需要用c#编码来设置一个图像。你实际上回答了你自己的问题。if(condition_1){setimage1();}else{setimage2();}..我需要清除如何为Toggle>background.设置图像。。我有WeeklyDayPanel>MonToggle>BackGround…(我已经在unity中设置了图像源代码),我想使用代码设置该图像。