- Home /
Unity C# Application.OpenURL not working with variable input
Also posted on Stack Overflow at: https://stackoverflow.com/questions/45039888/unity-c-sharp-application-openurl-not-working-with-variable-input
I'm building an application in Unity and I have a button that uses Application.OpenURL() to bring the user to a certain page when clicked. Up until now, I was using the fully typed out url name in double quotes as the parameter for the OpenURL function, and it was working as I intended. When I pressed the button the page was opened in the browser.
In order to incorporate multiple possible urls, I have changed the parameter to a string variable called url_name. Since this change, nothing happens when the same button is pressed. My code currently looks like this:
IEnumerator alertPopUp(string metaData_Content){
if (alertDelay)
{
yield return new WaitForSeconds (alertDelayLength);
}
string[] tokens = metaData_Content.Split (',');
string sign_name = tokens [0];
string url_name = tokens [1];
string template = "You discovered {0}";
string data = sign_name;
string message = string.Format(template, data);
MNPopup popup = new MNPopup ("Honorary Chicago", message);
popup.AddAction ("Find out more!", () => {Application.OpenURL(url_name);});
popup.AddAction ("Not now", () => {Debug.Log("action 2 action callback");});
popup.AddDismissListener (() => {Debug.Log("dismiss listener");});
popup.Show ();
}
Notes:
The block that starts with MNPopup takes advantage of a plugin I got from the asset store that builds formatted pop ups. I don't believe it has anything to do with the problem, especially since the script worked as intended when I had a string typed out. I have tried testing with Debug.Log(url_name) in place of Application.OpenURL(). The log displayed the correct url as intended, so it appears that url_name is the value I intend it to be.
Question:
Why is the Application.OpenURL() function working when I type out a string, yet not working when a string variable is inputted?
have you tried Debug.Log(url_name);
to make sure that what's stored there is correct? Perhaps it's improperly formatted. I like to put *
in the debug Debug.Log("*" + url_name + "*");
before and after the variable to see if there's any whitespace.
I did try Debug.Log(url_name);
and it seemed to be the right content. I'll try the asterisk formatted idea now.
Debug.Log("*" + url_name + "*");
returned *https://stackoverflow.com/users/6706357/luke*. That url looks correct. Any other ideas?
Where are you setting the metadata? Is this in a file that you're loading?
There might be non printable characters in your string which may cause problems. Try printing the Length of the string and maybe print the string as hex with that helper:
public static string StrToHex(string aStr)
{
var data = System.Text.Encoding.ASCII.GetBytes(aStr);
var sb = new System.Text.StringBuilder();
foreach (var b in data)
sb.Append(b.ToString("X2")).Append(" ");
return sb.ToString();
}
So just do:
Debug.Log("Length: " + url_name.Length + "\n>" + url_name + "<\n"+ StrToHex(url_name));
Copy the result and post it as comment.
The result:
Length: 47
>https://stackoverflow.com/users/6706357/luke
<
3F 68 74 74 70 73 3A 2F 2F 73 74 61 63 6B 6F 76 65 72 66 6C 6F 77 2E 63 6F 6D 2F 75 73 65 72 73 2F 36 37 30 36 33 35 37 2F 6C 75 6B 65 3F 0A
Your answer
Follow this Question
Related Questions
Loading level using a string created through code. 3 Answers
PlayerPrefs from One App to Another. 1 Answer
Open Url in browser on button click 1 Answer
Using Application.OpenUrl to open a URL in a String variable 1 Answer
Extend from box. Notes 2 Answers