유니티에서 게임 시작하면 무한 로딩 뜨는데, 왜 그런지 모르겟음.
일단 이 소스코드 실행시킨 것 밖에 없긴 한데, 혹시 한 번 점검해주실 고수 분 있음?
#pragma warning disable CS0162
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class aalgo : MonoBehaviour
{
public Vector2Int startNode = new Vector2Int(0, 0);
public Vector2Int targetNode = new Vector2Int(9, 9);
int[,] node = new int[11, 11];
int[,] history_going_save = new int[11, 11];
int position_x = 0;
int position_y = 0;
string Type;
int order; // 순서
int history_going = 0; //얼마나 갔는가?
int now_x; // 결정 함수 구할 때 사용할 것
int now_y;
int min = 0;
void Start()
{
for (int i = 0; i < 11; i++) // 행 반복
{
for (int j = 0; j < 11; j++) // 열 반복
{
node[i, j] = 0;
history_going_save[i, j] = -1;
}
}
history_going_save[startNode.x, startNode.y] = 0;
Type = "Manhattan_Distance";
position_x = startNode.x;
position_y = startNode.y;
command();
}
// Update is called once per frame
void Update()
{
}
void command()
{
for (int i = 0; i < 11; i++) // 행 반복
{
for (int j = 0; j < 11; j++) // 열 반복
{
if (node[i, j] <= min && history_going_save[i, j] != -1)
{
min = node[i, j];
now_x = i;
now_y = j;
}
}
}
if (history_going_save[targetNode.x - 1, targetNode.y] != -1 ||
history_going_save[targetNode.x, targetNode.y - 1] != -1 ||
history_going_save[targetNode.x + 1, targetNode.y] != -1 ||
history_going_save[targetNode.x, targetNode.y + 1] != -1)
{
Debug.Log("끝남");
return;
}
order_case();
}
void order_case()
{
while (order != 4)
{
order++;
find();
}
order = 0;
command();
}
void find()
{
switch (order) //평가함수를 입력할 곳 지정
{
case 1:
now_x = position_x + 1; now_y = position_y;
if (0 > now_x || now_x > 10 || 0 > now_y || now_y > 10) { return; } else { break; }
case 2:
now_x = position_x - 1; now_y = position_y;
if (0 > now_x || now_x > 10 || 0 > now_y || now_y > 10) { return; } else { break; }
case 3:
now_x = position_x; now_y = position_y + 1;
if (0 > now_x || now_x > 10 || 0 > now_y || now_y > 10) { return; } else { break; }
case 4:
now_x = position_x; now_y = position_y - 1;
if (0 > now_x || now_x > 10 || 0 > now_y || now_y > 10) { return; } else { break; }
}
history_going = history_going_save[position_x, position_y] + 1;
history_going_save[now_x, now_y] = history_going;
switch (Type) //휴리스틱 함수 결정 및 평가함수 입력
{
case "Manhattan_Distance": node[now_x, now_y] = Manhattan_Distance() + history_going; break;
case " Euclidean_Distance": node[now_x, now_y] = Euclidean_Distance() + history_going; break;
case " Chebyshev_Distance": node[now_x, now_y] = Chebyshev_Distance() + history_going; break;
}
Debug.Log(new Vector2(now_x, now_y));
Debug.Log(node[now_x, now_y]);
}
int Manhattan_Distance() //맨헤튼 거리
{
int a = Mathf.Abs(targetNode.x - now_x);
int b = Mathf.Abs(targetNode.y - now_y);
return (a + b);
}
int Euclidean_Distance() //유클리드 거리
{
int a = (targetNode.x - now_x) * (targetNode.x - now_x);
int b = (targetNode.y - now_y) * (targetNode.y - now_y);
float result = Mathf.Sqrt(a + b);
return ((int)result);
}
int Chebyshev_Distance() //체비셰프 거리
{
int a = Mathf.Abs(targetNode.x - now_x);
int b = Mathf.Abs(targetNode.y - now_y);
if (a > b) return a;
else return b;
}
}
무한루프 체크해보세요 특히 while문 너무 위험하게 씀
유니티 멈추는거의 99.5%는 무한루프임 특히 while