简体   繁体   中英

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. I'm having a gameObject "Cube" and a js script attached for rotation.

Also I' m having a script "ClickButton.js" attached to a GUI.Texture. Everything works ok in Unity Player, but I want to translate this script to be used by touches in Android devices. Problem is I can't do it, although I have read the Unity documentation.

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? Thank you all in advance!

Why don't you just copy the contents of OnMouseDown() ? Basically touch is the same as mouse down on android devices, isn't it?

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();
            }    
        }     
    } 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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