【问题标题】:Unity: Code works great in editor but fails in build version (Windows 10 64 bit)Unity:代码在编辑器中运行良好,但在构建版本中失败(Windows 10 64 位)
【发布时间】:2018-04-04 14:10:52
【问题描述】:

我在这里遇到了一个奇怪的问题:网格在运行时在编辑器中显示得很漂亮。但是:它没有显示在我的构建版本中

查看以下 2 个构建的附加屏幕截图:

Build version vs Editor version

另外,我想向您展示我为它编写的脚本会有所帮助..:

 void GenerateGrid()
{
    Color gridColor = Color.cyan;
    Color borderColor = Color.black;
    Collider floorCollider = floor.GetComponent<Collider>();
    Vector3 foorSize = new Vector3(floorCollider.bounds.size.x, floorCollider.bounds.size.z);
    for (int x = 0; x < gridImage.width; x++)
    {
        for (int y = 0; y < gridImage.height; y++)
        {
            if (x < borderSize || x > gridImage.width - borderSize || y < borderSize || y > gridImage.height - borderSize)
            {
                gridImage.SetPixel(x, y, new Color(borderColor.r, borderColor.g, borderColor.b, 50));
            }
            else gridImage.SetPixel(x, y, new Color(gridColor.r, gridColor.g, gridColor.b, 50));
        }
        gridImage.wrapMode = TextureWrapMode.Repeat;
        gridImage.Apply();
    }       
    floor.GetComponent<MeshRenderer>().material.SetTexture(1, gridImage);
    floor.GetComponent<MeshRenderer>().material.SetTextureScale(1, new Vector2(floorCollider.bounds.size.x, floorCollider.bounds.size.z));
    floor.GetComponent<MeshRenderer>().material.SetTextureOffset(1, new Vector2(.5f, .5f));
    Debug.Log(floor.GetComponent<MeshRenderer>().material.GetTexture(1));

}

【问题讨论】:

  • 您使用的是非标准着色器吗?该平面使用什么着色器和材质?
  • 不,它是标准着色器。代码根本没有修改着色器,只是修改了纹理。
  • 如果不修改纹理会怎样?你能在编辑器中发布截图并构建吗?
  • srv3.imgonline.com.ua/result_img/…这是一张没有更新纹理的截图
  • 试试floor.GetComponent&lt;MeshRenderer&gt;().material.SetTexture("_MainTex", gridImage);。如果您想使用SetTexture(int nameID, Texture value);,请使用Shader.PropertyToID 获取nameID

标签: c# unity3d build editor


【解决方案1】:

试试floor.GetComponent&lt;MeshRenderer&gt;().material.SetTexture("_Mai‌​nTex", gridImage);

如果你想使用SetTexture(int nameID, Texture value);,你应该使用Shader.PropertyToID来获取nameID

着色器属性的每个名称(例如,_MainTex 或 _Color)都是 在 Unity 中分配了一个唯一的整数,对于 整场比赛。不同之间的数字不会相同 游戏运行或机器之间的运行,所以不要存储它们或发送 他们通过网络。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-23
    • 2015-08-18
    • 1970-01-01
    • 2011-10-27
    • 1970-01-01
    • 2021-08-29
    • 1970-01-01
    • 2017-11-22
    相关资源
    最近更新 更多