仅带有此标签的统一触摸按钮
unity touch button only with this tag
所以我有一个统一的触摸脚本,用于在带有盒子对撞机的 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");
}
}
但我希望当对象 clicked/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 中的等效代码会以类似的方式工作。
所以我有一个统一的触摸脚本,用于在带有盒子对撞机的 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");
}
}
但我希望当对象 clicked/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 中的等效代码会以类似的方式工作。