无法在Unity中单击使图像出现

时间:2019-01-11 18:04:43

标签: c# visual-studio unity3d

我要使图像在单击按钮时出现。假设我在输入字段中输入了男孩这个词,则在按下按钮时,将出现男孩的图像。正因为如此,整夜熬夜。这是我的代码示例

    using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;

public class GetAndSetText : MonoBehaviour
{
    public InputField name;
    public InputField fname;
    public Text fText;
    public Image boy;

    public void sum()
    {
        boy = GetComponent<Image>();

        if (name.text == "hello") 
        {
            boy.sprite = (boy.jpg);
        }
    }
}

2 个答案:

答案 0 :(得分:0)

Image组件没有字段或属性jpg ...

但是我想您要存档的是

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;

public class GetAndSetText : MonoBehaviour
{
    public InputField name;
    public InputField fname;
    public Text fText;
    public Image boy;
    public Sprite sprite;

    public void sum()
    {
        boy = GetComponent<Image>();

        if (name.text == "hello") 
        {
            boy.sprite = sprite;
        }
    }
}

在Unity编辑器中,转到导入的图像文件的检查器,并将其TextureType设置为Sprite(2D and UI),然后点击Apply(请参阅Sprite Manual)。比在Sprite字段中引用(拖放)。

答案 1 :(得分:0)

简单来说,您可以创建一个>gcloud config get-value account abc@def.com 并将男孩图像添加到其中。然后禁用此游戏对象。

Image1

现在,创建一个ImageButton以及类似这样的脚本:

InputField

最后,单击按钮,将脚本设置为此,并设置using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class SetImage : MonoBehaviour { public GameObject imageBoy; public InputField inputFieldName; public void ButtonOnClick() { if (inputFieldName.text == "boy") imageBoy.SetActive(true); else imageBoy.SetActive(false); } } 属性:

Image2