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

问题描述 投票:1回答:2

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

我的画布上有一个文本(用户界面),我希望它在我第一次点击鼠标时消失,然后再用第二次鼠标重新出现,但由于某种原因它不再出现。

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);
        }
    }
}

谢谢

c# unity3d
2个回答
2
投票

你不能点击不活跃的东西。因此,您需要在同一位置创建一个空标签,或者将.text值设为空而不是禁用该对象。

回答评论:

这是你想要的wat。我使用@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;
        }
    }
}

0
投票

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

if (Input.GetMouseButtonDown (1)){ 
    if(isHiden){
      // your code
   }else{
      // your code
   }
}
© www.soinside.com 2019 - 2024. All rights reserved.