Unity C#无法更新Ui文本标签CYOA:基于文本。试图理解引用,初始化和更新

时间:2016-05-25 18:00:24

标签: c# unity3d

更新:2016年5月27日

https://github.com/Linkman214/DawnLake/blob/master/Text101/Assets/TextController.cs

肾病,你得到了很大的帮助。我有一个问题,那就是假设扣除钱-2,然后更新标签的方法。但是,我的代码似乎将健康和金钱(金钱)都拉到负面,我的猜测是因为当前状态在update()方法中不断更新。以下是调用它们的所有实例。

public class TextController : MonoBehaviour {
public Text text;
public Text Healthlabel;
public Text OrensLabel;

private enum States {Start,Gossip,Survey_0,Drink_0,Witcher,Drink_1,TakeJob,Sleep};
private States myState;
int money= 2;
int health=100;
// Use this for initialization


void Start () {
 myState=States.Start;
 update_health();
 update_money();
}

public void update_health(){ Healthlabel.text=health.ToString(); Healthlabel=gameObject.GetComponent<Text>(); } 



public void update_money(){ OrensLabel.text=money.ToString(); OrensLabel=gameObject.GetComponent<Text>(); }

void state_TakeJob(){
text.text= "The Alderman has collected a pool among the fisherman. Come dawn, they'll be out by the dock," +
" hollering for brave souls to join another hunting party." +  
" If you're interested, we have vacant rooms upstairs for 2 orens a night\n\n" +
" Times are tough indeed, press S to rent a room and sleep for the night";
money= money-2;
update_money();


if(Input.GetKeyDown(KeyCode.S))  {myState=States.Sleep;}



and this is what the update method looks like



void Update () {

print(myState);
if (myState==States.Start) {state_Start();}
else if (myState==States.Drink_0)    {state_Drink_0();}
else if (myState==States.Survey_0)   {state_Survey_0();}
else if (myState==States.Gossip)     {state_Gossip();}
else if (myState==States.Witcher)    {state_Witcher();}
else if (myState==States.Sleep)      {state_Sleep();}
else if (myState==States.TakeJob)    {state_TakeJob();}

1 个答案:

答案 0 :(得分:0)

有很多方法可以做到这一点。

对于您的情况,我建议您创建2个附加文本,这样您就可以在场景中重新定位UI。

另一种方法是创建一个这样的函数:

private void UpdateOutputText(string output){
    this.text.text = "money: " + money + " health: " + health + "\n" +
                     output;
}
相关问题