Preventing your game from running on unknown servers

After all the hard work you've done to complete your web game project, it wouldn't be fair if it ended up generating traffic and income for someone else's website. In this recipe, we will create a script that prevents the main game menu from showing up unless it's hosted by an authorized server.

Getting ready

To test this recipe, you will need access to a provider where you can host the game.

How to do it...

To prevent your web game from being pirated, perform the following steps:

  1. Add the following C# Script to Main Camera:
    // file: BlockAccess
    using UnityEngine;
    using System.Collections;
    
    public class BlockAccess : MonoBehaviour {
        public bool checkDomain = true;
        public bool fullURL = true;
        public string[] DomainList;
        public string message;
        private bool illegalCopy = true;
    
        private void Start(){
        print (Application.absoluteURL);
            if (Application.isWebPlayer && checkDomain){
                int i = 0;
                for (i = 0; i < DomainList.Length; i++){
                    if (Application.absoluteURL == DomainList[i]){
                        illegalCopy = false;
                    }else if (Application.absoluteURL.Contains(DomainList[i]) && !fullURL){
                        illegalCopy = false;
                    }
                }
            }
        }
    
        private void OnGUI() {
            if (illegalCopy)
                GUI.Label(new Rect(Screen.width * 0.5f - 200, Screen.height * 0.5f - 10, 400, 32), message);
            else
                Application.LoadLevel(Application.loadedLevel + 1);
        }
    }
  2. Now fill out its variables. Leave the options Check Domain and Full URL checked; increase the Size of Domain List to 1 and fill out Element 0 with the complete URL for your game. Type in the sentence This is not a valid copy of the game in the Message field, as shown in the following screenshot:

    Note

    Remember to include the .unity3d filename and extension in the URL, and not the HTML where it is embedded.

    How to do it...
  3. Save your scene as menu.
  4. Create a new scene and change its Main Camera background color to black. Save this scene as nextLevel.
  5. Let's build the game. Navigate to File | Build Settings…, include the scenes menu and nextLevel, in that order, in the build list (scenes in Build). Also, select Web Player as your platform and click on Build.

How it works...

As soon as the scene starts, the script compares the actual URL of the .unity3d file to the ones listed in the Block Access component. If they don't match, the next level in the build is not loaded and a message appears on screen.

There's more...

The following is some information on how to fine-tune and customize this recipe:

Improving security by using full URLs in your Domain List

Your game will be more secure if you fill out the Domain List with complete URLs (such as http://www.myDomain.com/unitygame/game.unity3d). In fact, it's recommended that you leave the Full URL option selected, so your game won't be stolen and published under a URL such as www.stolenGames.com/yourgame.html?www.myDomain.com.

Allowing redistribution with more domains

If you want your game to run from several different domains, increase Size and enter more URLs. Also, you can leave your game completely free of protection by leaving the Check Domain option unchecked.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.47.169