Advertisement
Guest User

Untitled

a guest
Jul 21st, 2022
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.41 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. class SaveObject : MonoBehaviour
  4. {
  5.     /// <summary>
  6.     /// ключ, уникальный для каждого game object, в данном примере я просто использую имя объекта
  7.     /// </summary>
  8.     private string GameObjectIndificator => gameObject.name;
  9.    
  10.     private void Start()
  11.     {
  12.         if(PlayerPrefs.HasKey(GameObjectIndificator))
  13.             Load();
  14.     }
  15.  
  16.     private void OnDestroy()
  17.     {
  18.         Save();
  19.     }
  20.  
  21.     public void Save()
  22.     {
  23.         PlayerPrefs.SetFloat($"{GameObjectIndificator}.transform.position.x", transform.position.x);
  24.         PlayerPrefs.SetFloat($"{GameObjectIndificator}.transform.position.y", transform.position.y);
  25.         PlayerPrefs.SetFloat($"{GameObjectIndificator}.transform.position.z", transform.position.z);
  26.        
  27.         PlayerPrefs.SetFloat($"{GameObjectIndificator}.transform.eulerAngles.x", transform.eulerAngles.x);
  28.         PlayerPrefs.SetFloat($"{GameObjectIndificator}.transform.eulerAngles.y", transform.eulerAngles.y);
  29.         PlayerPrefs.SetFloat($"{GameObjectIndificator}.transform.eulerAngles.z", transform.eulerAngles.z);
  30.  
  31.         if (TryGetComponent<Rigidbody>(out var rigidbody))
  32.         {
  33.             PlayerPrefs.SetFloat($"{GameObjectIndificator}.rigidbody.velocity.x", rigidbody.velocity.x);
  34.             PlayerPrefs.SetFloat($"{GameObjectIndificator}.rigidbody.velocity.y", rigidbody.velocity.y);
  35.             PlayerPrefs.SetFloat($"{GameObjectIndificator}.rigidbody.velocity.z", rigidbody.velocity.z);
  36.            
  37.             PlayerPrefs.SetFloat($"{GameObjectIndificator}.rigidbody.angularVelocity.x", rigidbody.angularVelocity.x);
  38.             PlayerPrefs.SetFloat($"{GameObjectIndificator}.rigidbody.angularVelocity.y", rigidbody.angularVelocity.y);
  39.             PlayerPrefs.SetFloat($"{GameObjectIndificator}.rigidbody.angularVelocity.z", rigidbody.angularVelocity.z);
  40.         }
  41.     }
  42.  
  43.     public void Load()
  44.     {
  45.         transform.position = new Vector3
  46.         {
  47.             x = PlayerPrefs.GetFloat($"{GameObjectIndificator}.transform.position.x"),
  48.             y = PlayerPrefs.GetFloat($"{GameObjectIndificator}.transform.position.y"),
  49.             z = PlayerPrefs.GetFloat($"{GameObjectIndificator}.transform.position.z")
  50.         };
  51.  
  52.         transform.eulerAngles = new Vector3
  53.         {
  54.             x = PlayerPrefs.GetFloat($"{GameObjectIndificator}.transform.eulerAngles.x"),
  55.             y = PlayerPrefs.GetFloat($"{GameObjectIndificator}.transform.eulerAngles.y"),
  56.             z = PlayerPrefs.GetFloat($"{GameObjectIndificator}.transform.eulerAngles.z")
  57.         };
  58.  
  59.         if (TryGetComponent<Rigidbody>(out var rigidbody))
  60.         {
  61.             rigidbody.velocity = new Vector3
  62.             {
  63.                 x = PlayerPrefs.GetFloat($"{GameObjectIndificator}.rigidbody.velocity.x"),
  64.                 y = PlayerPrefs.GetFloat($"{GameObjectIndificator}.rigidbody.velocity.y"),
  65.                 z = PlayerPrefs.GetFloat($"{GameObjectIndificator}.rigidbody.velocity.z")
  66.             };
  67.  
  68.             rigidbody.angularVelocity = new Vector3
  69.             {
  70.                 x = PlayerPrefs.GetFloat($"{GameObjectIndificator}.rigidbody.angularVelocity.x"),
  71.                 y = PlayerPrefs.GetFloat($"{GameObjectIndificator}.rigidbody.angularVelocity.y"),
  72.                 z = PlayerPrefs.GetFloat($"{GameObjectIndificator}.rigidbody.angularVelocity.z")
  73.             };
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement