简体   繁体   English

C# Unity3D 错误:列表<int> .ToArray() 问题</int>

[英]C# Unity3D Error: List<int>.ToArray() problem

I'm building a complex mesh via C# scripting in Unity3D.我正在通过 Unity3D 中的 C# 脚本构建复杂的网格。 Following my generation codes, I pass my triangle data to UnityEngine.Mesh.triangles .按照我的生成代码,我将三角形数据传递给UnityEngine.Mesh.triangles My triangle data is stored in a variable typed as List<int> .我的三角形数据存储在类型为List<int>的变量中。 And the type of UnityEngine.Mesh.triangles is int[] . UnityEngine.Mesh.triangles的类型是int[] So I used List<int>.ToArray() method to convert it.所以我使用List<int>.ToArray()方法来转换它。 But after conversion, some items in the UnityEngine.Mesh.triangles ( int[] ) are not identical to my triangle data( List<int> ).但转换后, UnityEngine.Mesh.triangles ( int[] ) 中的某些项目与我的三角形数据 ( List<int> ) 不同。 To be more specific, I found that List<int> typed variables could store numbers greater than 65535, but int[] can not do that.更具体地说,我发现List<int>类型的变量可以存储大于 65535 的数字,但 int[] 不能这样做。

codes:代码:

List<int> ts = new List<int>();
//generation codes
mesh.triangles = ts.ToArray();

And I use this code to test it in Unity:我使用这段代码在 Unity 中对其进行测试:

for (int i = 0; i < mesh.triangles.Length; i++)
            if (mesh.triangles[i] != ts[i]) Debug.Log(mesh.triangles[i].ToString() + " ≠ " + ts[i].ToString()); 

then I get logs such as 0≠65536, indicating that there is a overflow.然后得到0≠65536等日志,说明有溢出。

but int[] can not do that.int[]不能那样做。

Of course it can it is an array of int so it supports the entire range of int .当然它可以是一个int数组,因此它支持整个int范围。

The issue is most probably not with int[] but probably rather with the Unity mesh API.问题很可能不在于int[] ,而可能在于 Unity 网格 API。

By default a Mesh in Unity can maximum have 65535 vertices.默认情况下,Unity 中的Mesh最多可以有65535个顶点。 Therefore also the triangles will not store any value above that.因此, triangles也不会存储任何高于此的值。

If you want to have more vertices than that you need to change the Mesh.indexFormat to IndexFormat.UInt32如果你想有更多的顶点,你需要将Mesh.indexFormat更改为IndexFormat.UInt32

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM