I've managed to figure out how to save the user's chosen position and load it back in, but I'm having some trouble understanding how to do something similar with PlayerPrefs for saving the 3D shape (since I believe PlayerPrefs only saves ints, floats, and strings). Any help is appreciated!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cubeControls : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
/* Changing the position of the object
*
*/
// Moving the object right
if (Input.GetKey(KeyCode.D))
{
transform.Translate(Vector3.right * moveSpeed * Time.deltaTime);
}
// Moving the object left
if (Input.GetKey(KeyCode.A))
{
transform.Translate(Vector3.left * moveSpeed * Time.deltaTime);
}
}
// To add button elements to the visual interface
void OnGUI()
{
/* Changing the shape of the object
*
*/
// Reset shape to cube
if (GUI.Button(new Rect(50, 30, 100, 40), "Cube"))
{
GameObject newCube = GameObject.CreatePrimitive(PrimitiveType.Cube);
newCube.AddComponent();
Destroy(gameObject);
}
// Changing to cylinder
if (GUI.Button(new Rect(50, 90, 100, 40), "Cylinder"))
{
GameObject newCylinder = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
newCylinder.AddComponent();
Destroy(gameObject);
}
// Changing to sphere
if (GUI.Button(new Rect(50, 150, 100, 40), "Sphere"))
{
GameObject newSphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
newSphere.AddComponent();
Destroy(gameObject);
}
// Saving
if (GUI.Button(new Rect(700, 330, 50, 30), "Save"))
{
// Saving the object's position
PlayerPrefs.SetFloat("xCoord", transform.position.x);
PlayerPrefs.SetFloat("yCoord", transform.position.y);
PlayerPrefs.SetFloat("zCoord", transform.position.z);
// Saving the object's shape
}
// Loading
if (GUI.Button(new Rect(770, 330, 50, 30), "Load"))
{
transform.position = new Vector3(PlayerPrefs.GetFloat("xCoord"), PlayerPrefs.GetFloat("yCoord"), PlayerPrefs.GetFloat("zCoord"));
}
}
}
↧