为什么没有加载对象的颜色?
Why is the object's color not being loaded?
我已经成功保存了 3D 对象的位置、旋转、比例、颜色和形状。但是,当我尝试加载对象时,颜色不显示;但其他一切工作正常。我尝试通过同行的建议使用 "newObject.GetComponent().material.color = colorOfObject",但编译器不喜欢这种语法。我在正确的轨道上吗?
注意:我只是包含了立方体选项的代码以提供更短的代码块,但我确实有用户可以选择的其他形状选项。
// Saving
if (GUI.Button(new Rect(700, 330, 50, 30), "Save"))
{
// Saving the object's color and resetting it to white
Color colorOfObject = rend.material.GetColor("_Color");
PlayerPrefs.SetFloat("rValue", colorOfObject.r);
PlayerPrefs.SetFloat("gValue", colorOfObject.g);
PlayerPrefs.SetFloat("bValue", colorOfObject.b);
rend.material.SetColor("_Color", Color.white);
}
// Loading
if (GUI.Button(new Rect(770, 330, 50, 30), "Load"))
{
GameObject newObject;
if (PlayerPrefs.GetString("Shape") == "cube")
{
newObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
newObject.AddComponent<cubeControls>();
newObject.transform.position = new Vector3(PlayerPrefs.GetFloat("xCoord"), PlayerPrefs.GetFloat("yCoord"), PlayerPrefs.GetFloat("zCoord"));
newObject.transform.rotation = Quaternion.Euler(PlayerPrefs.GetFloat("xRot"), PlayerPrefs.GetFloat("yRot"), PlayerPrefs.GetFloat("zRot"));
newObject.transform.localScale = new Vector3(PlayerPrefs.GetFloat("xScale"), PlayerPrefs.GetFloat("yScale"), PlayerPrefs.GetFloat("zScale"));
Color defaultColor = Color.white;
Color colorOfObject = new Color(PlayerPrefs.GetFloat("rValue", defaultColor.r), PlayerPrefs.GetFloat("gValue", defaultColor.g), PlayerPrefs.GetFloat("bValue", defaultColor.b));
//rend.material.SetColor("_Color", colorOfObject);
newObject.GetComponent().material.color = colorOfObject;
Destroy(gameObject);
}
如果对象的 material 使用的着色器没有 "_Color" 属性 那么它将被忽略。 Unity 中的一些着色器使用 "_TintColor",其他的只是不支持任何着色。
nb:您的方法将生成一个新的 material 实例,从而为每个对象生成一个新的绘图调用。查看 MaterialPropertyBlocks and use a GPU instancing 启用 shader/material 设置。
啊,我找了一些别的例子,才知道应该是:
newObject.GetComponent<Renderer>().material.color = colorOfObject;
但感谢所有帮助过的人!
我已经成功保存了 3D 对象的位置、旋转、比例、颜色和形状。但是,当我尝试加载对象时,颜色不显示;但其他一切工作正常。我尝试通过同行的建议使用 "newObject.GetComponent().material.color = colorOfObject",但编译器不喜欢这种语法。我在正确的轨道上吗?
注意:我只是包含了立方体选项的代码以提供更短的代码块,但我确实有用户可以选择的其他形状选项。
// Saving
if (GUI.Button(new Rect(700, 330, 50, 30), "Save"))
{
// Saving the object's color and resetting it to white
Color colorOfObject = rend.material.GetColor("_Color");
PlayerPrefs.SetFloat("rValue", colorOfObject.r);
PlayerPrefs.SetFloat("gValue", colorOfObject.g);
PlayerPrefs.SetFloat("bValue", colorOfObject.b);
rend.material.SetColor("_Color", Color.white);
}
// Loading
if (GUI.Button(new Rect(770, 330, 50, 30), "Load"))
{
GameObject newObject;
if (PlayerPrefs.GetString("Shape") == "cube")
{
newObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
newObject.AddComponent<cubeControls>();
newObject.transform.position = new Vector3(PlayerPrefs.GetFloat("xCoord"), PlayerPrefs.GetFloat("yCoord"), PlayerPrefs.GetFloat("zCoord"));
newObject.transform.rotation = Quaternion.Euler(PlayerPrefs.GetFloat("xRot"), PlayerPrefs.GetFloat("yRot"), PlayerPrefs.GetFloat("zRot"));
newObject.transform.localScale = new Vector3(PlayerPrefs.GetFloat("xScale"), PlayerPrefs.GetFloat("yScale"), PlayerPrefs.GetFloat("zScale"));
Color defaultColor = Color.white;
Color colorOfObject = new Color(PlayerPrefs.GetFloat("rValue", defaultColor.r), PlayerPrefs.GetFloat("gValue", defaultColor.g), PlayerPrefs.GetFloat("bValue", defaultColor.b));
//rend.material.SetColor("_Color", colorOfObject);
newObject.GetComponent().material.color = colorOfObject;
Destroy(gameObject);
}
如果对象的 material 使用的着色器没有 "_Color" 属性 那么它将被忽略。 Unity 中的一些着色器使用 "_TintColor",其他的只是不支持任何着色。
nb:您的方法将生成一个新的 material 实例,从而为每个对象生成一个新的绘图调用。查看 MaterialPropertyBlocks and use a GPU instancing 启用 shader/material 设置。
啊,我找了一些别的例子,才知道应该是:
newObject.GetComponent<Renderer>().material.color = colorOfObject;
但感谢所有帮助过的人!