Unity根据循环中有限值的变化停止响应

问题描述 投票:0回答:1

我在另一个脚本中多次运行'GenerateFloodFillData'。它基于int变量X在for循环中循环执行该函数。如果我只运行一次函数(floodFillSampleRate = 1),那么它似乎每次都会响应。如果我最多floodFillSampleRate = 10,则Unity编辑器会在几次尝试中停止响应25%。当floodFillSampleRate = 100时,它会计算一段时间,然后停止响应,并且该过程永远不会“返回”到响应状态。我知道为什么该函数需要很长的时间进行计算,并且还使用了while循环(并不总是建议这样做)。但这不应该是无限循环/递归问题,因为该函数之前已经执行过。

这是我对流程如何过度工作所产生的误解吗?因为即时通讯很清楚,花费的时间可能会超过一秒,是的,这非常非常慢。经过长时间的计算,是否存在冻结流程的间接风险?

TLDR:[floodFillSampleRate是循环量]在floodFillSampleRate = 1的情况下可以正常工作,但有时X上的值不是更高,因为计算需要花费相当长的时间,为什么呢冻结不一致?

private static Queue<Vector3> q = new Queue<Vector3>();
private static List<Vector3> l = new List<Vector3>();

private static void CheckIfQueueNeighbor(int index, float limit, Vector3[] allVertices, Vector3 currentNode)
{
    if (allVertices[index].y - currentNode.y <= limit)
    {
        if (l.Contains(allVertices[index]) == false)
        {
            l.Add(allVertices[index]);
            q.Enqueue(allVertices[index]);
        }
        else
        {
            // Do nothing if already in list
        }
    }
}

public static float GenerateFloodFillData(float heightThresholdValue, MeshFilter meshFilter)
{
    Vector3[] verts = meshFilter.sharedMesh.vertices;
    Vector3 cNode;

    //Initialize
    int width = (int)meshFilter.sharedMesh.bounds.size.x;
    q = new Queue<Vector3>();
    l = new List<Vector3>();

    //Start at index 0, maybe be any corner or vertex in mesh. Investigate more.
    q.Enqueue(verts[0]);

    while (q.Count > 0)
    {
        cNode = q.Dequeue();

        //Add traversable nodes: Left, right, top & bottom
        if ((Array.IndexOf(verts, cNode) - 1) >= 0)
        {
            CheckIfQueueNeighbor(Array.IndexOf(verts, cNode) - 1, heightThresholdValue, verts, cNode);
        }
        if ((Array.IndexOf(verts, cNode) + 1) < verts.Length)
        {
            CheckIfQueueNeighbor(Array.IndexOf(verts, cNode) + 1, heightThresholdValue, verts, cNode);
        }
        if ((Array.IndexOf(verts, cNode) - width) >= 0)
        {
            CheckIfQueueNeighbor(Array.IndexOf(verts, cNode) - width, heightThresholdValue, verts, cNode);
        }
        if (Array.IndexOf(verts, cNode) + width < verts.Length)
        {
            CheckIfQueueNeighbor(Array.IndexOf(verts, cNode) + width, heightThresholdValue, verts, cNode);
        }
    }

    float averageTraversal = (float)l.Count / (float)verts.Length;
    return averageTraversal;
}

此功能在按下GUILayout.Button时运行:

        float floodFillAverageTraversal = 0f;
        for (int i = 0; i < mapPreview.floodFillSampleRate; i++)
        {
            floodFillAverageTraversal += FloodFill.GenerateFloodFillData(mapPreview.floodFillHeightThresholdValue, mapPreview.previewMeshFilter);

            mapPreview.maps[mapPreview.mapIndexSelector].heightMapSettings.noiseSettings.seed = Random.Range(0, int.MaxValue);
            mapPreview.DrawMapInEditor();
        }

        floodFillAverageTraversal /= mapPreview.floodFillSampleRate;
c# unity3d while-loop infinite-loop flood-fill
1个回答
0
投票

可能是您要在“ CheckIfQueueNeighbor”函数中添加到“ q”,以便永远持续出队和排队吗?

while (q.Count > 0)
    {
        cNode = q.Dequeue();

private static void CheckIfQueueNeighbor(int index, float limit, Vector3[] allVertices, Vector3 currentNode)
{
    if (allVertices[index].y - currentNode.y <= limit)
    {
        if (l.Contains(allVertices[index]) == false)
        {
            l.Add(allVertices[index]);
            q.Enqueue(allVertices[index]); 
© www.soinside.com 2019 - 2024. All rights reserved.