【问题标题】:How to activate a button only when the player releases from within button boundary?仅当玩家从按钮边界内释放时如何激活按钮?
【发布时间】:2014-07-15 00:19:52
【问题描述】:

我希望我的按钮仅在玩家从按钮边界内释放时才起作用。如果玩家将他/她的手指滑过并退出按钮的边界,我不希望它做任何事情。这是基于触摸的界面。

我可以在以下代码中添加什么以使其工作:

void OnMouseUp(){

            Application.LoadLevel ("MoonWalker");

    }

【问题讨论】:

标签: c# unity3d touch


【解决方案1】:

首先,对于基于触摸的界面,您应该改用 TouchPhase。 (我已在评论中发布了链接)

要使按钮仅在其边界内释放触摸时起作用,您需要Raycast 来检测触摸释放的位置是否在按钮内。如果是则做某事,如果不是则什么也不做。

void Update(){
   foreach(Touch touch in Input.touches) {
      if(touch.phase == TouchPhase.Ended) {
         Ray ray = Camera.main.ScreenPointToRay(touch.Position);
         if (Physics.Raycast(ray, out hit)) {
            if(hit.GameObject.name == "Button")
               //do something
         }
      }
   }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-18
    • 2023-03-18
    • 1970-01-01
    • 2015-10-31
    相关资源
    最近更新 更多