center cursor and toggle on and off (Unity 5)

I’m trying to get the cursor to toggle on and off at the center of the screen. Has anyone achieved this with Unity 5?

 #pragma strict
    
    Cursor.lockState = CursorLockMode.Confined;
    Cursor.lockState = CursorLockMode.Locked;
    
    function Update () {
    if(Input.GetKeyDown(KeyCode.Tab)) {
            Cursor.visible = true;
        }
        else if (Input.GetKeyUp(KeyCode.Tab)) {
            Cursor.visible = false;
        }
    }

Cursor.lockState = CursorLockMode.Confined;
Cursor.lockState = CursorLockMode.Locked;

seems to work when applied within a canvas.

The cursor may still show when you run the app from the editor thus build it and compare the result.

here is the full code that i’ve used within a canvas object:

using UnityEngine;
using System.Collections;

public class GameCanvas : MonoBehaviour {

	// Use this for initialization
	void Start () {
		Cursor.lockState = CursorLockMode.Locked;
		Cursor.visible = false;
	}

	void OnGUI ()
	{
		GUILayout.BeginVertical ();
		// Release cursor on escape keypress
		if (Input.GetKeyDown (KeyCode.Escape)) {
			Cursor.lockState = CursorLockMode.None;
			Cursor.visible = true;
		}
	}
}