Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- class SaveObject : MonoBehaviour
- {
- /// <summary>
- /// ключ, уникальный для каждого game object, в данном примере я просто использую имя объекта
- /// </summary>
- private string GameObjectIndificator => gameObject.name;
- private void Start()
- {
- if(PlayerPrefs.HasKey(GameObjectIndificator))
- Load();
- }
- private void OnDestroy()
- {
- Save();
- }
- public void Save()
- {
- PlayerPrefs.SetFloat($"{GameObjectIndificator}.transform.position.x", transform.position.x);
- PlayerPrefs.SetFloat($"{GameObjectIndificator}.transform.position.y", transform.position.y);
- PlayerPrefs.SetFloat($"{GameObjectIndificator}.transform.position.z", transform.position.z);
- PlayerPrefs.SetFloat($"{GameObjectIndificator}.transform.eulerAngles.x", transform.eulerAngles.x);
- PlayerPrefs.SetFloat($"{GameObjectIndificator}.transform.eulerAngles.y", transform.eulerAngles.y);
- PlayerPrefs.SetFloat($"{GameObjectIndificator}.transform.eulerAngles.z", transform.eulerAngles.z);
- if (TryGetComponent<Rigidbody>(out var rigidbody))
- {
- PlayerPrefs.SetFloat($"{GameObjectIndificator}.rigidbody.velocity.x", rigidbody.velocity.x);
- PlayerPrefs.SetFloat($"{GameObjectIndificator}.rigidbody.velocity.y", rigidbody.velocity.y);
- PlayerPrefs.SetFloat($"{GameObjectIndificator}.rigidbody.velocity.z", rigidbody.velocity.z);
- PlayerPrefs.SetFloat($"{GameObjectIndificator}.rigidbody.angularVelocity.x", rigidbody.angularVelocity.x);
- PlayerPrefs.SetFloat($"{GameObjectIndificator}.rigidbody.angularVelocity.y", rigidbody.angularVelocity.y);
- PlayerPrefs.SetFloat($"{GameObjectIndificator}.rigidbody.angularVelocity.z", rigidbody.angularVelocity.z);
- }
- }
- public void Load()
- {
- transform.position = new Vector3
- {
- x = PlayerPrefs.GetFloat($"{GameObjectIndificator}.transform.position.x"),
- y = PlayerPrefs.GetFloat($"{GameObjectIndificator}.transform.position.y"),
- z = PlayerPrefs.GetFloat($"{GameObjectIndificator}.transform.position.z")
- };
- transform.eulerAngles = new Vector3
- {
- x = PlayerPrefs.GetFloat($"{GameObjectIndificator}.transform.eulerAngles.x"),
- y = PlayerPrefs.GetFloat($"{GameObjectIndificator}.transform.eulerAngles.y"),
- z = PlayerPrefs.GetFloat($"{GameObjectIndificator}.transform.eulerAngles.z")
- };
- if (TryGetComponent<Rigidbody>(out var rigidbody))
- {
- rigidbody.velocity = new Vector3
- {
- x = PlayerPrefs.GetFloat($"{GameObjectIndificator}.rigidbody.velocity.x"),
- y = PlayerPrefs.GetFloat($"{GameObjectIndificator}.rigidbody.velocity.y"),
- z = PlayerPrefs.GetFloat($"{GameObjectIndificator}.rigidbody.velocity.z")
- };
- rigidbody.angularVelocity = new Vector3
- {
- x = PlayerPrefs.GetFloat($"{GameObjectIndificator}.rigidbody.angularVelocity.x"),
- y = PlayerPrefs.GetFloat($"{GameObjectIndificator}.rigidbody.angularVelocity.y"),
- z = PlayerPrefs.GetFloat($"{GameObjectIndificator}.rigidbody.angularVelocity.z")
- };
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement