为什么我的代码在C#中的第二个鼠标按钮失败



我的代码有问题,迫切需要您的帮助。

我的画布上有文本(以UI为单位),我希望它在我的第一个鼠标单击并重新出现第二个文字,但由于某种原因,它不会重新出现。

void Update() 
{
    Debug.Log ("start                      " + isHide);
    if (Input.GetMouseButtonDown (1) && isHide == true) {
        Debug.Log ("after enter 1 and no change   " + isHide);
        text.gameObject.SetActive (false); 
        isHide = false;
        Debug.Log ("after enter 1 and change    " + isHide);
    } else {
        if (Input.GetMouseButtonDown (1) && isHide == false) {
            Debug.Log ("after enter 2 and no change    " + isHide);
            text.gameObject.SetActive (true); 
            isHide = true;
            Debug.Log ("after enter 2 and change   " + isHide);
        }
    }
}

谢谢

您无法单击不活动的东西。因此,您需要创建一个空的标签OP在同一位置,或使.TEXT值空而不是禁用对象。

评论回答:

这是您想要的。我使用@la Pieuvre建议的编辑非常快地重新创建您的代码:

public UnityEngine.UI.Text text;
string oldTextValue = "";
bool isHide = true;
void Update()
{
    Debug.Log( "Start" );
    if( Input.GetMouseButtonDown( 1 ) )
    {
        Debug.Log( "Pressed Mouse button" );
        if( isHide == true )
        {
            Debug.Log( "Disabling Text" );
            oldTextValue = text.text;
            text.text = "";
            isHide = false;
        }
        else if ( isHide == false ) // Else it wil always just enable the button when u press your mouse.
        {
            Debug.Log( "Enabling Text" );
            text.text = oldTextValue;
            isHide = true;
        }
    }
}

另外,除了@livo所说的外,您的病情不是很干净。你最好写:

if (Input.GetMouseButtonDown (1)){ 
    if(isHiden){
      // your code
   }else{
      // your code
   }
}

相关内容

最新更新