Preventing your game from running on unknown servers

After all the hard work you've had to go through to complete your web game project, it wouldn't be fair if it ended up generating traffic and income on 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 webspace provider where you can host the game.

How to do it...

To prevent your web game from being pirated, follow these steps:

  1. From the Hierarchy view, use the Create drop-down menu to create a UI Text GameObject (Create | UI | Text). Name it Text – warning. Then, from the Text component in the Inspector, change its text field to Getting Info. Please wait.
  2. Add the following C# script to the Text – warning game object:
    using UnityEngine;
    using System.Collections;
    using UnityEngine.UI;
    
    public class BlockAccess : MonoBehaviour {
      public bool checkDomain = true;
      public bool fullURL = true;
      public string[] domainList;
      public string warning;
    
    
      private void Start(){
        Text scoreText = GetComponent<Text>();
        bool illegalCopy = true;
    
        if (Application.isEditor)
          illegalCopy = false;
    
        if (Application.isWebPlayer && checkDomain){
          for (int i = 0; i < domainList.Length; i++){
            if (Application.absoluteURL == domainList[i]){
              illegalCopy = false;
            }else if (Application.absoluteURL.Contains(domainList[i]) && !fullURL){
              illegalCopy = false;
            }
          }
        }
    
        if (illegalCopy)
          scoreText.text = warning;
        else
          Application.LoadLevel(Application.loadedLevel + 1);
      }
    }
  3. From the Inspector view, leave the options Check Domain and Full URL checked, and increase 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. You might have to change the paragraph's Horizontal Overflow to Overflow.

    Note

    Note: Remember to include the Unity 3D file name and extension in the URL, and not the HTML where it is embedded.

    How to do it...
  4. Save your scene as menu.
  5. Create a new scene and change its Main Camera background color to black. Save this scene as nextLevel.
  6. Let's build the game. Go to the File | Build Settings… menu and 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 the screen. If they do match, the line of code Application.LoadLevel(Application.loadedLevel + 1) will load the next scene from the build list.

There's more...

Here 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 that 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 fill out 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
3.145.35.194