2
0

PersistatePlayerPosition.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using UnityEngine;
  2. using Valve.VR;
  3. namespace Calibration
  4. {
  5. public class PersistatePlayerPosition : MonoBehaviour
  6. {
  7. private const string KEY_PLAYER_POS_X = "player_pos_x";
  8. private const string KEY_PLAYER_POS_Y = "player_pos_y";
  9. private const string KEY_PLAYER_POS_Z = "player_pos_z";
  10. public SteamVR_Action_Boolean savePos;
  11. private Transform playerTransform;
  12. // Start is called before the first frame update
  13. private void Start()
  14. {
  15. playerTransform = GetComponent<Transform>();
  16. savePos.AddOnStateDownListener(OnSavePos, SteamVR_Input_Sources.Any);
  17. Load();
  18. }
  19. private void OnSavePos(SteamVR_Action_Boolean fromaction, SteamVR_Input_Sources fromsource)
  20. {
  21. Save();
  22. }
  23. private void Load()
  24. {
  25. if (PlayerPrefs.HasKey(KEY_PLAYER_POS_X) && PlayerPrefs.HasKey(KEY_PLAYER_POS_Y) &&
  26. PlayerPrefs.HasKey(KEY_PLAYER_POS_Z))
  27. {
  28. var x = PlayerPrefs.GetFloat(KEY_PLAYER_POS_X);
  29. var y = PlayerPrefs.GetFloat(KEY_PLAYER_POS_Y);
  30. var z = PlayerPrefs.GetFloat(KEY_PLAYER_POS_Z);
  31. playerTransform.position = new Vector3(x, y, z);
  32. }
  33. }
  34. public void Save()
  35. {
  36. var position = playerTransform.position;
  37. PlayerPrefs.SetFloat(KEY_PLAYER_POS_X, position.x);
  38. PlayerPrefs.SetFloat(KEY_PLAYER_POS_Y, position.y);
  39. PlayerPrefs.SetFloat(KEY_PLAYER_POS_Z, position.z);
  40. PlayerPrefs.Save();
  41. }
  42. }
  43. }