为什么使用 PlayerPrefs 保存 3D 对象的比例会将其变成黑色 2D 形状?
Why is saving the scale of a 3D object using PlayerPrefs turning it into a black 2D shape?
我正在调试我的代码,我意识到出于某种原因,使用 PlayerPrefs 保存 3D 对象的比例然后将其加载回来导致对象变成黑色 2D 形状(所以而不是一个立方体,它变成了一个黑色的正方形)。其他一切正常(saving/loading 位置和旋转),但是一旦我输入 saving/loading 比例的代码,就会出现黑色 2D 形状。可能是什么原因造成的?
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()
{
}
// Update is called once per frame
void Update()
{
/* Changing the position of the object
*
*/
// Reset the position to original
if (Input.GetKey(KeyCode.Alpha0))
{
transform.position = Vector3.zero;
}
// 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);
}
// Moving the object forwards
if (Input.GetKey(KeyCode.W))
{
transform.Translate(Vector3.back * moveSpeed * Time.deltaTime);
}
// Moving the object backwards
if (Input.GetKey(KeyCode.S))
{
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
}
// Moving the object up
if (Input.GetKey(KeyCode.F))
{
transform.Translate(Vector3.up * moveSpeed * Time.deltaTime);
}
// Moving the object down
if (Input.GetKey(KeyCode.V))
{
transform.Translate(Vector3.down * 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);
}
// Rotating the cube in an upwards motion
if (Input.GetKey(KeyCode.UpArrow))
{
transform.Rotate(Vector3.right, turnSpeed * Time.deltaTime);
}
// Rotating the cube in a downwards motion
if (Input.GetKey(KeyCode.DownArrow))
{
transform.Rotate(Vector3.left, turnSpeed * Time.deltaTime);
}
/* 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);
}
}
// To add button elements to the visual interface
void OnGUI()
{
// 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);
transform.position = Vector3.zero;
// Saving the object's rotation
PlayerPrefs.SetFloat("xRot", transform.rotation.eulerAngles.x);
PlayerPrefs.SetFloat("yRot", transform.rotation.eulerAngles.y);
PlayerPrefs.SetFloat("zRot", transform.rotation.eulerAngles.z);
transform.rotation = Quaternion.Euler(Vector3.zero);
// Saving the object's scale
PlayerPrefs.SetFloat("xScale", transform.localScale.x);
PlayerPrefs.SetFloat("yScale", transform.localScale.y);
PlayerPrefs.SetFloat("zscale", transform.localScale.z);
transform.localScale = initscale;
}
// Loading
if (GUI.Button(new Rect(770, 330, 50, 30), "Load"))
{
transform.position = new Vector3(PlayerPrefs.GetFloat("xCoord"), PlayerPrefs.GetFloat("yCoord"), PlayerPrefs.GetFloat("zCoord"));
transform.rotation = Quaternion.Euler(PlayerPrefs.GetFloat("xRot"), PlayerPrefs.GetFloat("yRot"), PlayerPrefs.GetFloat("zRot"));
transform.localScale = new Vector3(PlayerPrefs.GetFloat("xScale"), PlayerPrefs.GetFloat("yScale"), PlayerPrefs.GetFloat("zScale"));
}
}
}
这是我在屏幕上看到的内容:
这是我要保存的已放大的对象。
这是我在按下 "Save" 和 "Load" 后在屏幕上看到的内容。
此外,我刚刚尝试输入一些 Debug.Log 消息以查看是否有任何问题,看起来 "zScale" 被加载为 0,即使使用了正确的值在保存阶段。
它在没有 zScale 的情况下进行实例化。查看您如何使用名称 "zscale" 保存它,但尝试使用 "zScale" 加载它。大写很重要!
我正在调试我的代码,我意识到出于某种原因,使用 PlayerPrefs 保存 3D 对象的比例然后将其加载回来导致对象变成黑色 2D 形状(所以而不是一个立方体,它变成了一个黑色的正方形)。其他一切正常(saving/loading 位置和旋转),但是一旦我输入 saving/loading 比例的代码,就会出现黑色 2D 形状。可能是什么原因造成的?
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()
{
}
// Update is called once per frame
void Update()
{
/* Changing the position of the object
*
*/
// Reset the position to original
if (Input.GetKey(KeyCode.Alpha0))
{
transform.position = Vector3.zero;
}
// 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);
}
// Moving the object forwards
if (Input.GetKey(KeyCode.W))
{
transform.Translate(Vector3.back * moveSpeed * Time.deltaTime);
}
// Moving the object backwards
if (Input.GetKey(KeyCode.S))
{
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
}
// Moving the object up
if (Input.GetKey(KeyCode.F))
{
transform.Translate(Vector3.up * moveSpeed * Time.deltaTime);
}
// Moving the object down
if (Input.GetKey(KeyCode.V))
{
transform.Translate(Vector3.down * 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);
}
// Rotating the cube in an upwards motion
if (Input.GetKey(KeyCode.UpArrow))
{
transform.Rotate(Vector3.right, turnSpeed * Time.deltaTime);
}
// Rotating the cube in a downwards motion
if (Input.GetKey(KeyCode.DownArrow))
{
transform.Rotate(Vector3.left, turnSpeed * Time.deltaTime);
}
/* 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);
}
}
// To add button elements to the visual interface
void OnGUI()
{
// 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);
transform.position = Vector3.zero;
// Saving the object's rotation
PlayerPrefs.SetFloat("xRot", transform.rotation.eulerAngles.x);
PlayerPrefs.SetFloat("yRot", transform.rotation.eulerAngles.y);
PlayerPrefs.SetFloat("zRot", transform.rotation.eulerAngles.z);
transform.rotation = Quaternion.Euler(Vector3.zero);
// Saving the object's scale
PlayerPrefs.SetFloat("xScale", transform.localScale.x);
PlayerPrefs.SetFloat("yScale", transform.localScale.y);
PlayerPrefs.SetFloat("zscale", transform.localScale.z);
transform.localScale = initscale;
}
// Loading
if (GUI.Button(new Rect(770, 330, 50, 30), "Load"))
{
transform.position = new Vector3(PlayerPrefs.GetFloat("xCoord"), PlayerPrefs.GetFloat("yCoord"), PlayerPrefs.GetFloat("zCoord"));
transform.rotation = Quaternion.Euler(PlayerPrefs.GetFloat("xRot"), PlayerPrefs.GetFloat("yRot"), PlayerPrefs.GetFloat("zRot"));
transform.localScale = new Vector3(PlayerPrefs.GetFloat("xScale"), PlayerPrefs.GetFloat("yScale"), PlayerPrefs.GetFloat("zScale"));
}
}
}
这是我在屏幕上看到的内容:
这是我要保存的已放大的对象。
这是我在按下 "Save" 和 "Load" 后在屏幕上看到的内容。
此外,我刚刚尝试输入一些 Debug.Log 消息以查看是否有任何问题,看起来 "zScale" 被加载为 0,即使使用了正确的值在保存阶段。
它在没有 zScale 的情况下进行实例化。查看您如何使用名称 "zscale" 保存它,但尝试使用 "zScale" 加载它。大写很重要!