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:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
class openWebPage: MonoBehaviour {
void Start() {
StartCoroutine(GetText());
}
IEnumerator GetText() {
UnityWebRequest www = UnityWebRequest.Get("http://myUsername:myPassword@my.ip.address.here/myCommand");
yield return www.Send();
if(www.isError) {
Debug.Log(www.error);
}
else {
// Show results as text
Debug.Log(www.downloadHandler.text);
// Or retrieve results as binary data
byte[] results = www.downloadHandler.data;
}
}
}
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.)
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.
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?
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?
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:
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.