简体   繁体   English

如何获得统一C#的点击次数

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

I'm currently writing a unity c# script, the main idea is when I click on some part of the model, the part will be highlighted, and now I want it to return to original state by clicking it again. 我当前正在编写一个统一的c#脚本,主要思想是当我单击模型的某些部分时,该部分将突出显示,现在我希望通过再次单击将其恢复到原始状态。 When I click on the same part at the 3rd time, it should be highlighted again. 当我第三次单击同一部分时,它应该再次突出显示。

I don't know how to achieve it inside Update() method, because every click costs several frames and I cannot recognize which frame is the 2nd click, 3rd click, etc. 我不知道如何在Update()方法中实现它,因为每次单击都会花费几帧,而我无法识别第二次点击,第三次点击是哪一帧。

Is there any way to recognize the number of clicks without considering frames in unity? 有什么方法可以在不考虑整体框架的情况下识别点击次数?

 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*/
    }
}}

You are so close. 你好亲近 The else statement should be added to your code. else语句应添加到您的代码中。 Your logic should be like this: 您的逻辑应如下所示:

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

Also, like Serlite mentioned, you have to use GetMouseButtonDown instead of GetMouseButton since GetMouseButtonDown is called once when key is pressed but GetMouseButton is called every frame while the key is down. 此外,像Serlite提到的,你必须使用GetMouseButtonDown代替GetMouseButton因为GetMouseButtonDown当按下键,但被调用一次GetMouseButton被称为每一帧,而关键是下来。

The final code should look like this: 最终代码应如下所示:

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

You have to write the UnHighlight function which basically restores the passed in GameObject/Transform to its default state. 您必须编写UnHighlight函数,该函数基本上将传递的UnHighlight / Transform恢复为默认状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM