Search Unity

How to transcode OnMouse to Touch phase for Android?

Discussion in 'Scripting' started by Exnihilon, Jul 27, 2014.

  1. Exnihilon

    Exnihilon

    Joined:
    Mar 2, 2014
    Posts:
    158
    Hello forum. I'm stuck to a rather simple solution, with my script transcoded to Android. I'm having a gameObject "Cube", and a script attached for rotation.

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

    Here is the code snippet:

    Code (CSharp):
    1. //This script is attached on a GUI.Texture acting as a button
    2.  
    3. var normalTexture : Texture2D;
    4. var hoverTexture : Texture2D;
    5.  
    6. function Update(){
    7.  
    8.         for (var i = 0; i < Input.touchCount; ++i) {
    9.             if (Input.GetTouch(i).phase == TouchPhase.Began)
    10.          
    11.             var rotate = Input.GetTouch(i);
    12.             rotate == doRotate();
    13.                 }
    14.             }
    15.  
    16. function OnMouseEnter(){
    17.  
    18. guiTexture.texture = hoverTexture;
    19.  
    20. }
    21.  
    22. function OnMouseExit(){
    23.  
    24. guiTexture.texture = normalTexture;
    25.  
    26. }
    27.  
    28. function OnMouseDown(){  
    29.     var runScriptRotate : GameObject[] = GameObject.FindGameObjectsWithTag("Marker");  
    30.                 for(var doRotation : GameObject in runScriptRotate)
    31.                     {  
    32.                     var scriptRT : doRotate = doRotation.GetComponent(doRotate);  
    33.                         if(scriptRT)
    34.                         {
    35.                         // access the function "doRotation" on a script named "doRotate" on gameObject "Cube"
    36.                         doRotate.doRotation();
    37.                         }  
    38.                     }    
    39.                    }
    Can someone, be kind enough to post an edit to my code script, to make it work on Android by touching? Thank you all in advance!
     
    Last edited: Jul 27, 2014
  2. Sykoo

    Sykoo

    Joined:
    Jul 25, 2014
    Posts:
    1,394
    You can use if(Input.GetMouseButtonDown(0)), which will act as "left mouse button" and also "touch" for smartphones.
     
  3. Exnihilon

    Exnihilon

    Joined:
    Mar 2, 2014
    Posts:
    158
    Hi @Sykoo, thanks for reply. I've tried what you suggested, nothing happened. I have attach the script on GUITexture, on MainCamera, nothing happens. Can you edit my above code, with a GUITexture, working as button?
     
  4. Sykoo

    Sykoo

    Joined:
    Jul 25, 2014
    Posts:
    1,394
    I'm going for vacation tomorrow so I have to pack now, but I'll see if I can answer yoiu later :)
     
  5. Exnihilon

    Exnihilon

    Joined:
    Mar 2, 2014
    Posts:
    158
    Thank you @Sykoo. Hope you can still make it. Have a nice time at your vacation! :cool:
     
  6. Sykoo

    Sykoo

    Joined:
    Jul 25, 2014
    Posts:
    1,394
    Thanks bud! :D
    I iwll try my best :)