Unity - OnMouseDown()是否与WebGL和移动设备协同工作?

时间:2017-08-09 12:19:39

标签: c# ios unity3d unity-webgl onmousedown

所以我知道移动设备上的WebGL并不是完全支持的,但我已经让游戏本身运行得很好。它只是没有注册触摸。我已经读过OnMouseDown无论如何都会在iOS上运行,如果它没有,Unity会提供一个脚本来将鼠标点击转换为触摸。

     //    OnTouchDown.cs
 //    Allows "OnMouseDown()" events to work on the iPhone.
 //    Attack to the main camera.

 #if UNITY_IPHONE || UNITY_WEBGL

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;

 public class OnTouchDown : MonoBehaviour
 {
     void Update () 
     {
         Debug.Log ("AAAAAAAAAAA");
         // Code for OnMouseDown in the iPhone. Unquote to test.
         RaycastHit hit = new RaycastHit();
         for (int i = 0; i < Input.touchCount; ++i) 
             {
                 if (Input.GetTouch(i).phase.Equals(TouchPhase.Began)) 
                 {
                     // Construct a ray from the current touch coordinates
                     Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position);
                     if (Physics.Raycast(ray, out hit)) 
                     {
                         hit.transform.gameObject.SendMessage("OnMouseDown");
                     }
                 }
             }
     }
 }
 #endif

我已将此脚本附加到按照网页上的说明呈现可点击对象的相机,但无济于事。这是使用OnMouseDown的代码。

void OnMouseDown()
 {
     isClicked = true;
 }
 //Method to check if the correct letter was clicked
 public bool checkIfClicked(char letter)
 {
     //If the letter was clicked...
     if (isClicked == true)  
     {
         //If the letter is correct, return true and let SceneManager handle it
         if (gameObject.tag [0] == letter) 
         {
             return true;
         } 
         //If the letter is wrong...
         else 
         {
             //Change the letter to red
             gameObject.GetComponent<SpriteRenderer> ().color = Color.red;
             //Play the sound effect
             badSound.Play();
             //Decrement guesses remaining
             GameObject.Find("Scene Manager").GetComponent<SceneManage>().guessesRemaining--;
             //Update the textbox
             GameObject.Find("Scene Manager").GetComponent<SceneManage>().guessesText.text = ("Wrong guesses remaining: " + GameObject.Find("Scene Manager").GetComponent<SceneManage>().guessesRemaining);
             //Set the boolean to false
             isClicked = false;
         }
     }
     return false;
 }

这些方法在桌面上运行良好。所以,我只是想知道我的代码是否错误,或者只是WebGL没有完成。谢谢!

0 个答案:

没有答案