Search Unity

Script does nothing when published to Android

Discussion in 'Scripting' started by Velketor, Jul 19, 2017.

  1. Velketor

    Velketor

    Joined:
    Sep 15, 2007
    Posts:
    110
    I'm using the following script on a UI button in Unity and it works great in the editor. It even works when published to PC/MAC...however this script does nothing when published to Android:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5.  
    6. class openWebPage: MonoBehaviour {
    7.     void Start() {
    8.         StartCoroutine(GetText());
    9.     }
    10.     IEnumerator GetText() {
    11.         UnityWebRequest www = UnityWebRequest.Get("http://myUsername:myPassword@my.ip.address.here/myCommand");
    12.         yield return www.Send();
    13.         if(www.isError) {
    14.             Debug.Log(www.error);
    15.         }
    16.         else {
    17.             // Show results as text
    18.             Debug.Log(www.downloadHandler.text);
    19.             // Or retrieve results as binary data
    20.             byte[] results = www.downloadHandler.data;
    21.         }
    22.     }
    23. }
    24.  
    Why doesn't it function on Android?
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Velketor likes this.
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    I know iOS has additional security for web requests that must be turned off or worked around before you can get stuff from the web; Android may have something similar.

    Do you have any error messages in your Android debug console? (This question may be better suited to the Android subforum.)
     
    Velketor likes this.
  4. Velketor

    Velketor

    Joined:
    Sep 15, 2007
    Posts:
    110
    Thank you for the response. Yes, I get 1 message in the Android debug console:
    "Game scripts or other custom code contains OnMouse_event handlers. Presence of such handlers might impact performance on handheld devices. UnityEditor.BuildPlayerWindow:BuildPlayerAndRun()"

    However, even when I disable the OnMouse_event handler and use Touch Events instead, the script still does nothing.
     
  5. Velketor

    Velketor

    Joined:
    Sep 15, 2007
    Posts:
    110
    After running several tests I found it has nothing to do with my mouse event or touch event because I put a DestroySelf script on my button and when I press the button it destroys itself (even on mobile). So the event is firing, but the script is not doing anything on mobile. Maybe C# doesn't work on Android? I guess I can try re-writing it as Javascript and see if it works?
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    No, c# works fine on Android. Considering it's not like it's c# once it's done being built to Android. There are not thousands of Android developers in Unity learning Unityscript just to do builds to it.

    So Log viewer didn't give you your debug messages? Or are you using logcat?

    Since you have two debugs there, one or the other should show up if the call completes correctly. Maybe www.isError isn't correct. Not sure, but UnityWebRequest docs show a using statement(to dispose of it when done) and two other types of error checks
    https://docs.unity3d.com/ScriptReference/Networking.UnityWebRequest.Get.html

    Maybe try switching out your error if for the example they use and see if it makes a difference.
     
    Velketor likes this.
  7. Velketor

    Velketor

    Joined:
    Sep 15, 2007
    Posts:
    110
    I've figured out the solution! Mobile devices don't allow you to send an http request with a username and a password in it (for security purposes). Instead, you have to pass an authentication string and then send the http request. Here's my code that finally worked on Android:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5.  
    6. public class OpenWebPage: MonoBehaviour {
    7.             void Start()
    8.             {
    9.                 StartCoroutine(GetText());
    10.             }
    11.  
    12.             string authenticate(string username, string password)
    13.             {
    14.                 string auth = username + ":" + password;
    15.                 auth = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(auth));
    16.                 auth = "Basic " + auth;
    17.                 return auth;
    18.             }
    19.  
    20.             IEnumerator GetText()
    21.             {
    22.  
    23.                 string authorization = authenticate("InsertUserNameHere", "InsertPasswordHere");
    24.                 while (true)
    25.                 {
    26.                     string url = "http://insertWebAddressHere/optionalCommand";
    27.  
    28.  
    29.                     UnityWebRequest www = UnityWebRequest.Get(url);
    30.                     www.SetRequestHeader("AUTHORIZATION", authorization);
    31.                     yield return www.Send();
    32.  
    33.         }
    34.     }
    35. }
    36.  
    Remember to change the name of the public class to be the same name as your script. In my case, I called my script OpenWebPage so I named my class OpenWebPage.
     
    Last edited: Jul 20, 2017