gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 07:42:24 25 4
gpt4 key购买 nike

所以我有一个统一的触摸脚本,用于使用盒子碰撞器对 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com