【问题标题】:Why is the object's color not being loaded?为什么没有加载对象的颜色?
【发布时间】:2019-01-16 23:19:49
【问题描述】:

我已经设法保存了 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);
        }

【问题讨论】:

  • 能否包含“rend”变量类型?您还确定这不是空/未定义吗?您的代码看起来不错,正如您所说,应该可以正常工作。我唯一的建议是查看 Unity 文档:docs.unity3d.com/ScriptReference/Material.GetColor.html 如您所见,您可以访问颜色调用 material.color (docs.unity3d.com/ScriptReference/Material-color.html)。
  • 是的,我会继续编辑问题。
  • Unity API 中没有采用 0 个参数的 GetComponent() 重载。这是您自己实现的方法吗?否则,您将缺少给它一个您想要获得的组件类型,例如 GetComponent(typeof(Renderer))GetComponent&lt;Renderer&gt;()

标签: c# unity3d colors save load


【解决方案1】:

如果对象的材质使用的着色器没有 "_Color" 属性,那么它将被忽略。 Unity 中包含的一些着色器使用 "_TintColor",其他的只是不支持任何着色。

nb:您的方法将生成一个新的材质实例,从而为每个对象生成一个新的drawcall。查看 MaterialPropertyBlocks 并使用启用了 GPU instancing 的着色器/材质设置。

【讨论】:

  • 我只是尝试了一种不同的方法(参见上面的编辑),我想知道您是否对此有任何意见?这是 Unity 帮助论坛上某人提出的解决方案,所以我想看看这是否解决了我的问题,但遗憾的是它没有。
【解决方案2】:

啊,我找到了一些其他的例子,我意识到应该是:

newObject.GetComponent<Renderer>().material.color = colorOfObject;

但感谢所有帮助过的人!

【讨论】:

  • 这只是您已经在做的事情的简写 (docs.unity3d.com/ScriptReference/Material-color.html),但我现在意识到您实际上可能没有正确获取渲染器,它应该类似于 .GetComponent () 。此外,由于这只是一个语法糖,你仍然会遇到唯一实例问题(但在你的情况下这可能不是问题)
  • 你已经正确了,包括&lt;Renderer&gt;,但是这个页面的html格式删除了“标签”^^我为你修好了
猜你喜欢
  • 2015-12-23
  • 1970-01-01
  • 2011-11-21
  • 2021-04-01
  • 1970-01-01
  • 2011-11-23
  • 1970-01-01
  • 2022-01-25
  • 1970-01-01
相关资源
最近更新 更多