簡體   English   中英

如何獲得統一C#的點擊次數

[英]how to get the number of click in unity c#

我當前正在編寫一個統一的c#腳本,主要思想是當我單擊模型的某些部分時,該部分將突出顯示,現在我希望通過再次單擊將其恢復到原始狀態。 當我第三次單擊同一部分時,它應該再次突出顯示。

我不知道如何在Update()方法中實現它,因為每次單擊都會花費幾幀,而我無法識別第二次點擊,第三次點擊是哪一幀。

有什么方法可以在不考慮整體框架的情況下識別點擊次數?

 void Update(){
    if (Input.GetMouseButton(0))
    {
        RaycastHit hit;

        if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit)){
                bone = hit.collider.transform;
                if (boneList.Contains(bone) != true)
                {
                    /* if the part is not chosen before, add it to the list and highlight all elements in the list */
                    boneList.Add(bone);
                    Highlight(boneList);
                }/*cannot delete the element chosen repetitively*/
    }
}}

你好親近 else語句應添加到您的代碼中。 您的邏輯應如下所示:

if(List contains clickedObject){
    Remove clickedObject from List
    UnHighlight the clickedObject
}else{
    Add clickedObject to List
    Highlight the clickedObject
}

此外,像Serlite提到的,你必須使用GetMouseButtonDown代替GetMouseButton因為GetMouseButtonDown當按下鍵,但被調用一次GetMouseButton被稱為每一幀,而關鍵是下來。

最終代碼應如下所示:

void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        RaycastHit hit;

        if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit))
        {
            bone = hit.collider.transform;


            if (boneList.Contains(bone))
            {
                //Object Clicked is already in List. Remove it from the List then UnHighlight it
                boneList.Remove(bone);
                UnHighlight(boneList);
            }
            else
            {
                //Object Clicked is not in List. Add it to the List then Highlight it
                boneList.Add(bone);
                Highlight(boneList);
            }
        }
    }
}

您必須編寫UnHighlight函數,該函數基本上將傳遞的UnHighlight / Transform恢復為默認狀態。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM