简体   繁体   English

如何将OnMouse事件转换为Android的Touch阶段?

[英]How to translate OnMouse events to Touch phases for Android?

I'm stuck to a rather simple solution, with my script translated run in Android with Unity3d. 我坚持使用一个相当简单的解决方案,将我的脚本翻译为在Android中使用Unity3d运行。 I'm having a gameObject "Cube" and a js script attached for rotation. 我有一个gameObject“ Cube”和一个用于旋转的js脚本。

Also I' m having a script "ClickButton.js" attached to a GUI.Texture. 另外,我在GUI.Texture上附加了脚本“ ClickButton.js”。 Everything works ok in Unity Player, but I want to translate this script to be used by touches in Android devices. 在Unity Player中一切正常,但是我想翻译此脚本以供Android设备中的触摸使用。 Problem is I can't do it, although I have read the Unity documentation. 问题是,尽管我已阅读Unity文档,但我无法做到。

Here is the code snippet: 这是代码片段:

//This script is attached on a GUI.Texture acting as a button

var normalTexture : Texture2D;
var hoverTexture : Texture2D;

function Update(){
    for (var i = 0; i < Input.touchCount; ++i) {
        if (Input.GetTouch(i).phase == TouchPhase.Began)
            var rotate = Input.GetTouch(i);
            rotate == doRotate();
        }
    }
}

function OnMouseEnter(){
    guiTexture.texture = hoverTexture;
}

function OnMouseExit(){
    guiTexture.texture = normalTexture;
}

function OnMouseDown(){  
    var runScriptRotate : GameObject[] = GameObject.FindGameObjectsWithTag("Marker");    
    for(var doRotation : GameObject in runScriptRotate){    
        var scriptRT : doRotate = doRotation.GetComponent(doRotate);    
        if(scriptRT){
            // access the function "doRotation" on a script named "doRotate" on     gameObject "Cube"
            doRotate.doRotation();
        }    
    }      
}

Can somebody, be kind enough to edit this code script, so to make it work on Android by touching? 有人可以友好地编辑此代码脚本,以便通过触摸使其在Android上运行吗? Thank you all in advance! 谢谢大家!

Why don't you just copy the contents of OnMouseDown() ? 您为什么不只复制OnMouseDown()的内容? Basically touch is the same as mouse down on android devices, isn't it? 基本上,触摸与在Android设备上按下鼠标相同,不是吗?

if(Input.touchCount > 0){
    if(Input.GetTouch(0).phase == TouchPhase.Began){
        var runScriptRotate : GameObject[] = GameObject.FindGameObjectsWithTag("Marker");    
        for(var doRotation : GameObject in runScriptRotate){    
            var scriptRT : doRotate = doRotation.GetComponent(doRotate);    
            if(scriptRT){
                // access the function "doRotation" on a script named "doRotate" on gameObject "Cube"
                doRotate.doRotation();
            }    
        }     
    } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM