【Unity】汇总一些遇到的Bug

本文列举了在Unity开发中遇到的一些典型问题,包括脚本行尾不一致、HTTP416错误、UnityBug、路径错误、面板管理、XLua代码生成、许可证过期、文件流管理、类型转换错误、Toggle组问题、动画Clip兼容性、纹理格式错误、Light接口问题和ShaderGraph中的纹理类型不匹配。每个问题都提供了详细的解决步骤和建议。
摘要由CSDN通过智能技术生成

1.There are inconsistent line endings in the 'XXX.cs' script. Some are Mac OS X (UNIX) and some are Windows.

解决方法:

调出visual studio的高级保存选项,并将行尾设置成Windows(CR LF)

Step1,调出高级保存选项:

方法一:直接从搜索栏里面搜高级保存选项

方法二:将高级保存选项放到菜单栏:

打开菜单栏的“工具”->“自定义”选项

选择“命令”页->点击“添加命令”

选中“文件”类别,添加“高级保存选项”,并确定

 

  

 点击确认后,还可以通过上移下移调整高级保存选项在菜单栏中的位置

 Step2,打开高级保存选项,将行尾改成Windows(CR LF) 

2.HTTP/1.1 416 Range Not Satisfiable:http://www.test.com/tttttt.pdf

解决方法:

是断点续传引起的,在断点续传中设置了Request Header :“Range”,当Range报头的开始下载位置已经是末尾的时候,会引起416报错,比如下面的beginPos等于文件末尾,就会引起416

可以在检测到网络错误时,判断当报错代码是416,就删除原本下载的.temp临时文件,重新下载整个文件。

 

3.PlayerSettings Validation: Requested build target group (0) doesn't exist; #define symbols for scripting won't be added.

解决方法:

好像这个是一个unity的Bug,搜索的时候发现17年有人提出过,按理应该已经修复了?当时我触发好像是因为我把一个dll的select Platforms for plugin的exclude Platforms勾选了又取消掉了,然后就触发了这个bug...

只需要关闭整个unity hub再重启就好了

4.Unity路径自动补全。DirectoryNotFoundException: Could not find a part of the path "D:\Working\Unity\CommerialProjects\2D Game Project\‪D:\桌面\Test.txt".

 我输入的路径是桌面,unity会自动在我的绝对地址前面加上工程路径

解决方法:

应该是unity不允许写工程路径以外的文件所以出现这样的报错,只需将文件换成一个工程路径下的路径即可

5.Menu Window/Panels/1  can't be checked because it doesn't exist

解决方法:

在菜单栏打开“窗口”->“面板”->“关闭所有浮动面板"

6.InvalidOperationException: Code has not been generated, may be not work in phone!

解决方法:

是XLua导致的,build之前需要先generate code

点击菜单栏的“XLua”->“Generate Code”

7.Assertion failed on expression: 'm_ErrorCode == MDB_MAP_RESIZED || !HasAbortingErrors()'   Asset database transaction committed twice    Assertion failed on expression: 'errors == MDB_SUCCESS || errors == MDB_NOTFOUND' 

解决方法:

unity许可证过期,在unityhub中激活许可证并重新打开项目即可

8.IOException: Sharing violation on path...

解决方法:

检查用到FileStream对象的地方,用完之后要及时Dispose()

FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate,FileAccess.Write);
fs.Close();
fs.Dispose();

除了直接用FileStream对象的地方,File.Create也会产生FileStream对象,也需要进行Dispose

if (!File.Exists(path))
{
    File.Create(path).Dispose();
}

9.c# exception:System.InvalidCastException: Specified cast is not valid.

解决方法:

这个应该是类型转换失败的问题,检查涉及到的类型之间是否可以相互转换

10.Toggle组点击时乱闪的问题

问题如图:

解决方法:

好像是导航出现的问题,我不清楚具体原因(如果有知道的小伙伴可以告诉我..)

将导航改成None就解决了

11.The AnimationClip 'XXX' used by the Animation component 'XXX' must be marked as Legacy.

这个问题是在用旧版本的Animation组件,调用Animation.Play()方法出现的

解决方法:

在项目中点击AnimationClip文件,在检查器中将模式改成“调试”,勾选Legacy,如下图:

12.Unsupported GraphicsFormat(97) for SetPixel operations.

在使用Texture2D.SetPixel的时候出现的,texture格式错误

解决方法:

修改texture的格式

请参考以下链接中#3楼的回复:

SetPixels - Unsupported Texture Format - Unity Forum

13.error CS1061: 'Light' does not contain a definition for 'SetLightDirty' and no accessible extension method 'SetLightDirty' accepting a first argument of type 'Light' could be found (are you missing a using directive or an assembly reference?)

解决方法:

找到generator.cs文件,打开找到GetGenConfig方法,在BlackList中添加以下:

            BlackList = new List<List<string>>()
            {
                new List<string>(){"UnityEngine.Light", "shadowRadius"},
                new List<string>(){"UnityEngine.Light", "SetLightDirty"},
                new List<string>(){"UnityEngine.Light", "shadowAngle"},
                new List<string>(){"UnityEngine.Light", "shadowAngle"}
            };

参考:xlua 问题踩坑_light' does not contain a definition for 'setlight-CSDN博客

14.'tex2D': cannot implicitly convert from 'Texture2D<float4>' to 'struct UnityTexture2D'

在shader graph中使用custom function节点时出现这个bug

解决方法:

将编写的HLSL文件中的Texture2D类型替换成UnityTexture2D

//UNITY_SHADER_NO_UPGRADE
# ifndef MYHLSLINCLUDE_INCLUDED
# define MYHLSLINCLUDE_INCLUDED

//将Texture2D替换成UnityTexture2D
void EdgeDetection_half (half2 texelSize, UnityTexture2D tex, half2 uv, out half edge)
{
    const half Gx[9] = { -1,-2,-1,
               0,0,0,
               1,2,1 };
    const half Gy[9] = { -1,0,1,
                   -2,0,2,
                   -1,0,1 };

    half2 uvNear[9];

    uvNear[0] = uv + texelSize * half2(-1, -1);
    uvNear[1] = uv + texelSize * half2(0, -1);
    uvNear[2] = uv + texelSize * half2(1, -1);
    uvNear[3] = uv + texelSize * half2(-1, 0);
    uvNear[4] = uv;
    uvNear[5] = uv + texelSize * half2(1, 0);
    uvNear[6] = uv + texelSize * half2(-1, 1);
    uvNear[7] = uv + texelSize * half2(0, 1);
    uvNear[8] = uv + texelSize * half2(1, 1);

    float4 G;
    half edgeX = 0;
    half edgeY = 0;
    for (int i = 0; i < 9; i++)
    {
        G = tex2D(tex, uvNear[i]);
        edgeX += G.g * Gx[i];
        edgeY += G.g * Gy[i];
    }

    edge = abs(edgeX) + abs(edgeY);    
}

# endif //MYHLSLINCLUDE_INCLUDED

参考:Bug - Cannot convert UnityTexture2D to Texture2D<float4> for custom function node - Unity Forum

15.Shader error in 'XXX': unable to unroll loop, loop does not appear to terminate in a timely manner (XXX iterations) at XXX (on d3d11)

在shader循环中使用tex2D函数出现这个问题

//当count不是一个常量,且使用了tex2D函数时出现这个错报
for (int i = 0; i < count; i++)
{
    //..Logic..
    float4 color = tex2D(texture,uv);
    //..Logic..
}

解决方法:

将tex2D函数替换成tex2Dlod函数

//当count不是一个常量,且使用了tex2D函数时出现这个错报
for (int i = 0; i < count; i++)
{
    //..Logic..
    //float4 color = tex2D(texture,uv);
    //替换为tex2Dlod
    float4 color = tex2Dlod(texture,float4(uv,0,0));
    //..Logic..
}

参考:Issues with shaderProperty and for-loop - Unity Forum (#4楼Dolkar的回答)

16.LuaException: c# exception:XLua.LuaException: c# exception:System.InvalidCastException: This type must add to CSharpCallLua: System.Action<int>

使用xLua出现的报错

解决方法:

添加CSharpCallLua配置,建议代码放到Editor文件夹

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;

//列表方式均必须放到一个static类
public static class xLuaConfigAddi
{
    //列表方式均必须是static的字段/属性
    //处理报错:LuaException: c# exception:XLua.LuaException: c# exception:System.InvalidCastException: This type must add to CSharpCallLua: System.Action<int>
    [CSharpCallLua]
    public static List<Type> CSharpCallLuaAddi = new List<Type>()
    {
        typeof(System.Action<int>),
    };
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值