统一触摸按钮只有这个标签

时间:2016-02-11 21:19:11

标签: javascript android unity3d touch

所以我有一个统一的触摸脚本,可以通过盒式对撞机轻松触摸2d精灵:

    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将会不同

1 个答案:

答案 0 :(得分:1)

你想要的是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中的等价物也可以起到类似的作用。