Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple script for Unity to check the domain that the web player is hosted on, and redirect it to the proper location if it is not in the list of domains. It does nothing if it is not a web player build.
using UnityEngine;
using System.Collections;
using System.Text;
/** Add this script to an object in the first scene of your game.
* It doesn't do anything for non-webplayer builds. For webplayer
* builds, it checks the domain to make sure it contains at least
* one of the strings, or it will redirect the page to the proper
* URL for the game.
*/
public class WebLocationChecker : MonoBehaviour {
/** if it is a webplayer, then the domain must contain any
* one or more of these strings, or it will be redirected */
public string[] domainMustContain;
/** this is where to redirect the webplayer page if none of
* the strings in domainMustContain are found.
*/
public string redirectURL;
void Awake() {
#if UNITY_WEBPLAYER
if (domainMustContain.Length > 0)
{
StringBuilder buf = new StringBuilder();
for(int i = 0; i < domainMustContain.Length; i++)
{
string domain = domainMustContain[i];
if (i > 0)
{
buf.Append(" && ");
}
buf.Append("(document.location.host.indexOf('"+domain+"') == -1)");
}
string criteria = buf.ToString();
Application.ExternalEval("if("+criteria+") { document.location='"+redirectURL+"'; }");
}
#endif
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment