C# 在文本后显示UnityEngine.Sprite
这是我现在的代码:`使用System.CollectionsC# 在文本后显示UnityEngine.Sprite,c#,unity3d,C#,Unity3d,这是我现在的代码:`使用System.Collections using System.Collections.Generic; using UnityEngine; using TMPro; public class easyModeImageSpawner : MonoBehaviour { private int rand; private int newRand; private int randForButton; public List<Spr
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class easyModeImageSpawner : MonoBehaviour
{
private int rand;
private int newRand;
private int randForButton;
public List<Sprite> image_pic2 = new List<Sprite>();
public List<Sprite> image_pic3 = new List<Sprite>();
int previousresult;
public List<TMP_Text> optionBUttonText = new List<TMP_Text>();
private void Start()
{
rand = Random.Range(0, image_pic2.Count);
GetComponent<SpriteRenderer>().sprite = image_pic2[rand];
//image_pic2.RemoveAt(rand);
RandomizeText();
}
public void NextRound()
{
rand = Random.Range(0, image_pic2.Count);
GetComponent<SpriteRenderer>().sprite = image_pic2[rand];
}
public void RandomizeText()
{
newRand = Random.Range(0, image_pic3.Count);
randForButton = Random.Range(0, optionBUttonText.Count);
optionBUttonText[randForButton].text = image_pic2[rand].ToString(); // over here it also
shows
UnityEngine.Sprite
image_pic3.RemoveAt(rand);
optionBUttonText.RemoveAt(randForButton);
int i = 0;
while (i != 3)
{
Debug.Log("running other button");
newRand = Random.Range(0, image_pic3.Count);
randForButton = Random.Range(0, optionBUttonText.Count);
if (newRand == previousresult)
{
Randomize();
}
optionBUttonText[randForButton].text = image_pic3[newRand].ToString(); // over here it
also
shows UnityEngine.Sprite
optionBUttonText.RemoveAt(randForButton);
previousresult = newRand;
i++;
}
}
void Randomize()
{
newRand = Random.Range(0, image_pic3.Count);
randForButton = Random.Range(0, optionBUttonText.Count);
}
}`
使用System.Collections.Generic;
使用UnityEngine;
使用TMPro;
公共类easyModeImageSpawner:MonoBehavior
{
私人国际兰特;
纽兰德私人酒店;
私家车;
公共列表图像_pic2=新列表();
公共列表图像_pic3=新列表();
以前的结果;
公共列表选项ButtonText=新列表();
私有void Start()
{
rand=随机范围(0,图像2.计数);
GetComponent().sprite=image_pic2[rand];
//图2.RemoveAt(兰德);
RandomizeText();
}
public void NextRound()
{
rand=随机范围(0,图像2.计数);
GetComponent().sprite=image_pic2[rand];
}
public void RandomizeText()
{
newRand=Random.Range(0,图像3.Count);
randForButton=Random.Range(0,optionBUttonText.Count);
optionBUttonText[randForButton].text=image_pic2[rand].ToString();//这里也有
显示
雪碧
图3.RemoveAt(兰德);
optionBUttonText.RemoveAt(随机按钮);
int i=0;
而(i!=3)
{
Log(“运行其他按钮”);
newRand=Random.Range(0,图像3.Count);
randForButton=Random.Range(0,optionBUttonText.Count);
if(newRand==previousresult)
{
随机化();
}
optionBUttonText[randForButton].text=image_pic3[newRand].ToString();//在这里
也
显示UnityEngine。雪碧
optionBUttonText.RemoveAt(随机按钮);
previousresult=newRand;
i++;
}
}
void Randomize()
{
newRand=Random.Range(0,图像3.Count);
randForButton=Random.Range(0,optionBUttonText.Count);
}
}`
下面是该代码的结果:
问题是,当你玩游戏时,按钮上的文本会显示名称,但之后还会在上面添加“Unityengine.sprite”,我正在研究,但我不确定如何解决这个问题
我认为您必须在ToString()的括号中添加一些内容,但我不确定是什么
如果有人知道如何解决这个问题,请告诉我
提前感谢。如果您希望获得在层次结构中看到的
精灵
游戏对象的名称,请使用.name
而不是.ToString()
方法。使用.ToString()
方法不仅可以获取游戏对象的名称,还可以获取数据类型
在您的情况下,不要使用
image\u pic2[rand].ToString()
,而是使用image\u pic2[rand].name
非常感谢