javascript - unity 触摸按钮仅带有此标签

标签 javascript android unity-game-engine touch

所以我有一个统一的触摸脚本,用于使用盒子碰撞器对 2d Sprite 进行简单触摸:

    var platform : RuntimePlatform = Application.platform;

 function Update(){
     if(platform == RuntimePlatform.Android || platform == RuntimePlatform.IPhonePlayer){
         if(Input.touchCount > 0) {
             if(Input.GetTouch(0).phase == TouchPhase.Began){
                 checkTouch(Input.GetTouch(0).position);
             }
         }
     }else if(platform == RuntimePlatform.OSXEditor){
         if(Input.GetMouseButtonDown(0)) {
             checkTouch(Input.mousePosition);
         }
     }
 }

 function checkTouch(pos){
     var wp : Vector3 = Camera.main.ScreenToWorldPoint(pos);
     var touchPos : Vector2 = new Vector2(wp.x, wp.y);
     var hit = Physics2D.OverlapPoint(touchPos);

     if (hit){
         Debug.Log("touched");
     }
 }

但我希望当单击/触摸的对象具有特定标签时,Debug.Log 会有所不同

最佳答案

您想要的是System.Action

public System.Action _action;
...

public void FutureAction()
{
    //Whatever you want this function to perform
}
...

//When you know the action that you want it to perform
_action = FutureAction;
...

//When you want to call this action, in your checkTouched function for example
if (hit.CompareTag("myTag"))
    _action(); //This will call whatever action was saved into the variable.

希望对您有所帮助!

编辑: 我用 C# 编写了代码,但 javascript 中的等效代码也可以类似地工作。

关于javascript - unity 触摸按钮仅带有此标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35350231/

相关文章:

android - 无法在 JNI 中更新 jobobject

c# - Unity 3D 中的角色 Controller 碰撞和斜坡

unity-game-engine - MRTK Photon Unity Networking 'AnchorModuleScript' 命名空间未找到

c# - 将 ScriptableObjects 加载到单个预制件/多个预制件的最佳实践是什么?

javascript - javascript 对象的元素是否按特定顺序初始化?

javascript - $(document).ready 处理程序 (IE) 太多了吗?

android - 在背景图像上重复图像渐变

android - 如何去除透明 ActionBar 的标题背景?

c# - WebSocket WCF 传输绑定(bind)

Grails 中的 Javascript 回发