How can I change cursor visibility and cursor lock state when I press tab then revert it back when I press tab again.

In my game I have an inventory system toggle-able by tab. What I also want to happen when I press tab is for the cursor to become visible and movable so I can move items around my inventory with out pressing escape then when I press tab I want the cursor to be locked and invisible just like it started.

What you are looking for is Cursor.lockState and Cursor.visible.

void OpenInventory () 
{
     Cursor.lockState = CursorLockMode.None;
     Cursor.visible = true;
}

void CloseInventory () 
{
     Cursor.lockState = CursorLockMode.Locked;
     Cursor.visible = false;
}

And pressing escape to see the cursor only works in the editor not in a build.