I’ve managed to figure out how to implement all the features/options that I want the user to be able to customize and I also added two buttons on the screen that the user can click to save and load the object. But I’m having some trouble figuring out how to save and load the gameObject itself; I’ve read some posts about serialization for an entire game, but it looks a bit complicated relative to what my professor has gone over in class. Is serialization the only way to do it or is there a simpler method?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cubeControls : MonoBehaviour
{
// Constants for object rotation
public float moveSpeed = 80.0F;
public float turnSpeed = 100.0F;
// Initial scale of the original cube
public static Vector3 initscale = Vector3.one;
// Start is called before the first frame update
void Start()
{
Debug.Log("I am alive!");
}
// 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);
}
/* Changing the rotation of the object
*
*/
// Reset the rotation to original by returning a rotation at (0, 0, 0)
if (Input.GetKey(KeyCode.Alpha0))
{
transform.rotation = Quaternion.Euler(Vector3.zero);
}
// Rotating the cube to the right
if (Input.GetKey(KeyCode.RightArrow))
{
transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
}
// Rotating the cube to the left
if (Input.GetKey(KeyCode.LeftArrow))
{
transform.Rotate(Vector3.down, turnSpeed * Time.deltaTime);
}
// Saving the current rendered material
Renderer rend = GetComponent<Renderer>();
/* Changing the scale of the object
*
*/
// Reset the cube to the original scaled size
if (Input.GetKeyDown(KeyCode.Alpha0))
{
transform.localScale = initscale;
}
// Double the size of the cube
if (Input.GetKeyDown(KeyCode.Alpha2))
{
transform.localScale += new Vector3(2F, 2F, 2F);
}
/* Changing the color via key presses
*
*/
// Reset color of cube to original
if (Input.GetKeyDown(KeyCode.Alpha0))
{
rend.material.SetColor("_Color", Color.white);
}
if (Input.GetKeyDown(KeyCode.R))
{
rend.material.SetColor("_Color", Color.red);
}
}
// 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<cubeControls>();
Destroy(gameObject);
}
// Changing to cylinder
if (GUI.Button(new Rect(50, 90, 100, 40), "Cylinder"))
{
GameObject newCylinder = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
newCylinder.AddComponent<cubeControls>();
Destroy(gameObject);
}
// Saving
if (GUI.Button(new Rect(700, 330, 50, 30), "Save"))
{
}
// Loading
if (GUI.Button(new Rect(770, 330, 50, 30), "Load"))
{
}
}
}