无效点分配给2D边缘对撞机错误 [英] Invalid points assigned to 2D edge collider Error

查看:120
本文介绍了无效点分配给2D边缘对撞机错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下绘制LineRendererEdgeCollider2D的方法.

I have the following method that draws a LineRenderer and an EdgeCollider2D.

    protected override void Draw ( EdgeCollider2D edgeCollider2D, LineRenderer lineRenderer , float resolution , GoSpline[] spline , float startFill , float targetFill )
{

    float increment = ( 1f / resolution );
    List< Vector3 > vertices = new List<Vector3>();
    List< Vector2 > pts = new List<Vector2>();

    float t = 0;

    while ( targetFill > t )
    {
        vertices.Add( spline[ 0 ].getPointOnPath( t ) );
        pts.Add( new Vector2(spline[ 0 ].getPointOnPath( t ).x, spline[ 0 ].getPointOnPath( t ).y));
        Vector3 v3 = spline[ 0 ].getPointOnPath( t );
        Debug.Log (v3);

        t += increment;
    }

    lineRenderer.SetVertexCount( vertices.Count );
    lineRenderer.SetPositions( vertices.ToArray() );

    edgeCollider2D.points = pts.ToArray ();

}

我不断收到错误消息

Invalid points assigned to 2D edge collider.
UnityEngine.EdgeCollider2D:set_points(Vector2[])

在线

edgeCollider2D.points = pts.ToArray ();

此外,偶尔当lineRenderer.positionCount为0时,edgeCollider2D.points有2分,这不是应该的,它们都应该没有分.

Also, occasionally when lineRenderer.positionCount is 0, edgeCollider2D.points has 2 points, which should not be the case, they should both have no points.

我尝试更换

edgeCollider2D.points = pts.ToArray ();

使用

edgeCollider2D.points = vertices.Cast<Vector2> ().ToArray();

但这还会导致以下错误:

but this gave the following error in addition:

InvalidCastException: Cannot cast from source type to destination type.
System.Linq.Enumerable+<CreateCastIterator>c__Iterator0`1[UnityEngine.Vector2].MoveNext ()

我该如何解决这个问题?

How can I solve this issue?

推荐答案

在将数组pts.ToArray()分配给edgeCollider2D.points之前,请确保该数组不是null.

Before assigning the array pts.ToArray() to edgeCollider2D.points, make sure that the array is not null.

还要确保要分配给EdgeCollider2D点的数据/数组(pts.ToArray)的长度为>= 2 .如果不是,您将得到该错误.

Also make sure that the length of the data/array you want to assign (pts.ToArray) to the EdgeCollider2D points is >= 2. You will get that error if it is not.

这两个都可以通过简单的if语句进行检查.如果要分配给edgeCollider2D.points的数组是0< 2 ,只需禁用EdgeCollider2D组件或按您希望的方式处理它,但不要将该数组分配给EdgeCollider2D点,因为您将获得该异常.

Both of these can be checked with a simple if statement. If the array to assign to the edgeCollider2D.points is 0 or < 2, simply disable the EdgeCollider2D component or handle it which ever way you want but don't assign that array to the EdgeCollider2D points as you will get that exception.

List<Vector2> pts = new List<Vector2>();

//The array to assign to the EdgeCollider2D points 
Vector2 []tempArray = pts.ToArray();

//Check if point is valid
if(tempArray != null && tempArray.Length >= 2){
    //Enable EdgeCollider2D if previously disabled
    if(!edgeCollider2D.enabled)
    edgeCollider2D.enabled = true;

    //Ok to assign the array
    edgeCollider2D.points = tempArray;
}
else
{
    //Point is NOT valid, disable the `EdgeCollider2D` component.
    Debug.Log("Point cannot be null nor less than 2");
    edgeCollider2D.enabled = false;
}

这篇关于无效点分配给2D边缘对撞机错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆