Unity 5 lock mouse

I want to know a script that locks mouse in center and unlocks when I press a key(for example, ESC). It should work with Unity 5. I also need to know how to call it and how to execute it(on which object to put it on). I have a character controller(FPS) in scene. Could you help me?

Take a look at Cursor.lockstate