Unity中为UGUI中的Image置空

首先需要一开始获取一张空的图片,然后进行记录,当想要给一个Image中的图片置空是时候就可以将其中的值置为其:

public class ImageChange : MonoBehaviour {
    //获取的是原来的空图片
    private Sprite FormatSprite;
    public Sprite ChangeSprite;
    private void Start()
    {
        FormatSprite = transform.GetComponent<Image>().sprite;
    }
    void Update () {
        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            transform.GetComponent<Image>().sprite = FormatSprite;
        }
        if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            transform.GetComponent<Image>().sprite = ChangeSprite;
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/baosong/p/9551422.html
今日推荐