Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Plugin for save/load position,rotation and boolean ^_^

Discussion in 'Made With Unity' started by killerz, Jan 22, 2011.

  1. killerz

    killerz

    Joined:
    Aug 28, 2010
    Posts:
    32
    Hi all, i present you my first plugin for Unity3d. It serves for save position (Vector3), rotation (Quaternion) and booleans.
    I hope it like you!

    Script name : APlayerPrefs (C# Script)

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class APlayerPrefs : MonoBehaviour
    6. {
    7.     public static void SaveObjectPosition(string saveName,Vector3 value)
    8.     {
    9.         PlayerPrefs.SetFloat(saveName + "px",value.x);
    10.         PlayerPrefs.SetFloat(saveName + "py",value.y);
    11.         PlayerPrefs.SetFloat(saveName + "pz",value.z);
    12.     }  
    13.    
    14.     public static void SaveObjectRotation(string saveName,Quaternion value)
    15.     {
    16.         PlayerPrefs.SetFloat(saveName + "rx",value.x);
    17.         PlayerPrefs.SetFloat(saveName + "ry",value.y);
    18.         PlayerPrefs.SetFloat(saveName + "rz",value.z);
    19.         PlayerPrefs.SetFloat(saveName + "rw",value.w);
    20.     }
    21.  
    22.     public static Vector3 LoadObjectPosition(string loadPosition)
    23.     {
    24.         float pX = PlayerPrefs.GetFloat(loadPosition + "px");
    25.         float pY = PlayerPrefs.GetFloat(loadPosition + "py");
    26.         float pZ = PlayerPrefs.GetFloat(loadPosition + "pz");
    27.         return new Vector3(pX,pY,pZ);
    28.     }
    29.    
    30.     public static Quaternion LoadObjectRotation(string loadRotation)
    31.     {
    32.         float rX = PlayerPrefs.GetFloat(loadRotation + "rx");
    33.         float rY = PlayerPrefs.GetFloat(loadRotation + "ry");
    34.         float rZ = PlayerPrefs.GetFloat(loadRotation + "rz");
    35.         float rW = PlayerPrefs.GetFloat(loadRotation + "rw");
    36.         return new Quaternion(rX,rY,rZ,rW);
    37.     }
    38.    
    39.     public static void SetBool(string saveBool,bool valueBool)
    40.     {
    41.         PlayerPrefs.SetInt(saveBool,valueBool?1:0);
    42.     }
    43.    
    44.     public static bool GetBool(string loadBool)
    45.     {
    46.         return PlayerPrefs.GetInt(loadBool)==1?true:false;
    47.     }
    48. }
    49.  
    50.  
    Usage:

    Example Script (JavaScript)

    Code (csharp):
    1.  
    2. var example: boolean = true;
    3.  
    4. function Update ()
    5. {
    6.     if(Input.GetKeyDown(KeyCode.A))
    7.     {
    8.         APlayerPrefs.SetBool("newbool",example);
    9.     }
    10.    
    11.     if(Input.GetKeyDown(KeyCode.B))
    12.     {
    13.         print(APlayerPrefs.GetBool("newbool"));
    14.     }
    15.  
    16.     if(Input.GetKeyDown(KeyCode.S))
    17.     {
    18.         APlayerPrefs.SaveObjectPosition("playerposition",transform.position);
    19.         APlayerPrefs.SaveObjectRotation("playerrotation",transform.rotation);
    20.     }
    21.     if(Input.GetKeyDown(KeyCode.L))
    22.     {
    23.         transform.position = APlayerPrefs.LoadObjectPosition("playerposition");
    24.         transform.rotation = APlayerPrefs.LoadObjectRotation("playerrotation");
    25.     }
    26. }
    27.