今早我梦醒了坎 发表于 2021-1-29 09:04

Unity踩坑笔记(持续更新)

1.error CS0104: 'MinAttribute' is an ambiguous reference between 'UnityEngine.Rendering.PostProcessing.MinAttribute' and 'UnityEngine.MinAttribute'
Solution:using MinAttribute = UnityEngine.PostProcessing.MinAttribute;
2.自动生成粒子好像粒子数大于16384就会出问题
16384 * 4 = 2 的 16次方, unity最多只能有65535个顶点。即索引值是uint的,本身的int类型很令人迷惑。create.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;可设置为uint32
3.shift alt enter vs enter full screen
4.Unity sky box render order appears quite bugged. Shader says queue is background, but it is actually rendering as the last thing in the Geometry queue.
5.unity can't add script behaviour assemblyinfo. the script needs to derive from monobehaviour
一种可能是没有继承monobehaviour 或者脚本名字和类名不一致
6.fragment shader 中利用天空盒位置计算随机数,然后通过乘以一个大数然后模一个数来获取随机星星,但是好像不可行呐,因为精度问题模出来之后星星老是会闪
原因:应该是因为天空盒精度达不到导致像素对应的天空盒坐标有微小变化最终的出来的随机数也有微小变化导致闪烁。
Tsize=Rwidth/tan(fov/2) 因为减小了fov之后增加了天空盒精度之后改善了。
如上图所示,假设红色是第T帧的屏幕(假设屏幕5*5像素),蓝色是第T+1帧的屏幕,则可以看到前一帧的红点并不一定在后一帧中出现,所以根据位置算出来的随机数是不一致的,导致闪烁现象
目前想到的解决方案:不要让位置完全随机,而是把整个天空盒分为512等份,然后fragment shader里面获得的天空盒位置规则化到这512等份里面,这样可以通过减少随机精度的方案来减少位置的变化抖动。
7.Shader.setGlobal不生效问题
According to the reference, Shader.setGlobal functions would only work on variables that are not exposed as a property in the property block. So what you have to do is remove the _test("_test",Float) = 0.0 line to make it work. Be sure to remake a new material, since unity will save the properties you have set on a material even when you are using a shader that doesn't have that property.
8.UNITY_TRANSFER_INSTANCE_IDinvalid subscript instanceID
解决方案:在顶点着色器的输入输出加入宏UNITY_INSTANCE_ID
9.Unity shader 改完之后要实际赋给材质用一下可能有些错误才能暴露出来
10.shader_feature & multi_compile
shader_feature is very similar to multi_compile. The only difference is that Unity does not include unused variants of shader_feature shaders in the final build. For this reason, you should use shader_feature for keywords that are set from the Materials, while multi_compile is better for keywords that are set from code globally.
The main disadvantage of shader_feature and multi_compile is that all keywords defined in them contribute towards Unity’s global keyword count limit (256 global keywords, plus 64 local keywords). To avoid this issue, you can use different shader variant directives: shader_feature_local and multi_compile_local.
shader_feature_local: similar to shader_feature, but enumerated keywords are local.multi_compile_local: similar to multi_compile, but enumerated keywords are local.
11.Difference between UnityObjectToWorldDir and UnityObjectToWorldNormal
WorldDir is applying the rotation from the transform matrix.
WorldNormal is apply the rotation from the inverse transpose matrix.
12.图片边界有条缝
(把图片排布方式由repeat改为clamp)
13.关于Skybox
粗略做了一下测试unity默认的Skybox应该是用的Sphere模型,然后当前摄像机下ModelMatrix大概放大了十几万倍。而且这个ModelMatrix是和裁剪平面的大小有关的。
14.build target android player is not supported
解决方案:把VS工程关掉重启好像就可以了
15.Alpha 贴图采样的时候一定记得要是tex2Dlod().a, 不写.a取不到正确的值
16.Vertex Shader 采样贴图需要SM3以上,最好用tex2Dlod而不是tex2D
17.2D Texture在Spherical mapping的时候会有一条缝隙
解决方案,用tex2Dlod采样贴图,防止mipmap插值采样可以防止。
18.AssetImporter.GetAtPath 需要Assets开头的相对路径
19.2D Texture(从左到右六个面)转cubemap size must power of two
20.surface shader里面取出来的vertex color和texcoord不对,转成正常的vert frag就正常了
21.shader中定义常量定义在shader外面好像不生效,写在shader里面就可以了
22.Unity 和UE4 FBX导入之后UV.v是相反的,需要flip一下才能一致
23.反射不要忘记设置BindingFlags
24.抽象函数的反射,反射函数要用抽象类型来获取(反射函数参数是基本类型),然后反射调用的时候再用实际的对象去执行,不然可能反射参数会不对。
25.unity 没有实例化的prefab获取的collider bound是不对的
26.unity在PC float half fixed都是32位,移动端half16位,可能会导致精度问题。
页: [1]
查看完整版本: Unity踩坑笔记(持续更新)