IEnumerator Start() { //intArray <- �̿��ؼ� �ڽ��� ������. //item + ��ġ�� ���Ѱ��� ��������. cube.gameObject.SetActive(false); for (int i = 0; i < intArray.Count; i++) { var pos = cube.transform.position + offset * i; BubbleSortNewItem newItem = Instantiate(cube, pos, cube.transform.rotation); newItem.SetNumber(intArray[i]); newItem.gameObject.SetActive(true); cubes.Add(newItem); } yield return(new WaitForSeconds(0.5f)); for (int turn = 0; turn < intArray.Count; turn++) { int fixCount = turn; int checkLength = intArray.Count - fixCount; checkLength = checkLength - 1;// ������ ������ �ѽ����� �˻��ϹǷ� �ִ���� -1������ �˻��ϸ� �ȴ� for (int x = 0; x < checkLength; x++) { int downNumber = intArray[x]; int upNumber = intArray[x + 1]; if (downNumber > upNumber) { //intArray�� �ִ� ���� �ٲٱ�. Swap(x, intArray); //�ڽ��� ��ġ �ٲٱ� var downCube = cubes[x]; var upCube = cubes[x + 1]; var downPosition = downCube.transform.position; var upPosition = upCube.transform.position; //cubes�ڽ������� ��������. cubes[x] = upCube; cubes[x + 1] = downCube; downCube.transform.DOMove(upPosition, 0.5f).SetEase(Ease.InBounce); upCube.transform.DOMove(downPosition, 0.5f).SetEase(Ease.InBounce); yield return(new WaitForSeconds(0.5f)); } } } void Swap(int index, List <int> intArray) { int leftIndex = index; int rightIndex = index + 1; int temp = intArray[leftIndex]; intArray[leftIndex] = intArray[rightIndex]; intArray[rightIndex] = temp; } }
IEnumerator SortCo() { // 토대 박스는 안보이게 하자 cube.gameObject.SetActive(false); // list 이용하여 박스 만들자 // item + 위치값 더한 곳에 생성하자 for (int i = 0; i < cubeNumList.Count; i++) { var pos = cube.transform.position + offset * i; BubbleSortNewItem newItem = Instantiate(cube, pos, cube.transform.rotation); newItem.SetNumber(cubeNumList[i]); newItem.gameObject.SetActive(true); cubes.Add(newItem); } // 박스 생성하고 0.5초 멈추자 yield return(new WaitForSeconds(0.5f)); // 버블정렬 for (int i = 0; i < cubeNumList.Count; i++) { for (int j = 0; j + 1 < cubeNumList.Count - i; j++) { // 박스 정보 가져오자 var cube1 = cubes[j]; var cube2 = cubes[j + 1]; yield return(StartCoroutine(ChangeColorCo(cube1, cube2, Color.yellow))); if (cubeNumList[j] > cubeNumList[j + 1]) { // 리스트 숫자 바꾸기 SwapNum(cubeNumList, j, j + 1); // 박스 위치 바꾸기 SwapBox(j, cube1, cube2); yield return(new WaitForSeconds(0.5f)); } else { yield return(StartCoroutine(ChangeColorCo(cube1, cube2, Color.green))); } yield return(StartCoroutine(ChangeColorCo(cube1, cube2, Color.white))); } // 고정된 블럭 회색으로 표시 cubes[cubeNumList.Count - 1 - i].ChangeColor(Color.gray); } void SwapNum(List <int> list, int idx1, int idx2) { int temp = list[idx1]; list[idx1] = list[idx2]; list[idx2] = temp; } void SwapBox(int j, BubbleSortNewItem cube1, BubbleSortNewItem cube2) { // 박스의 위치를 가져오자 var cube1Pos = cube1.transform.position; var cube2Pos = cube2.transform.position; // 박스 배열, 박스 위치바꾸자 cube1.ChangeColor(Color.red); cube2.ChangeColor(new Color(255, 0, 0)); cubes[j] = cube2; cubes[j + 1] = cube1; cube2.transform.DOMove(cube1Pos, 0.5f).SetEase(Ease.InBounce); cube1.transform.DOMove(cube2Pos, 0.5f).SetEase(Ease.InBounce); } IEnumerator ChangeColorCo( BubbleSortNewItem cube1, BubbleSortNewItem cube2, Color color) { cube1.ChangeColor(color); cube2.ChangeColor(color); yield return(new WaitForSeconds(0.5f)); } }
IEnumerator SortCo() { //intArray <- 이용해서 박스를 만들자. //item + 위치값 더한곳에 생성하자. cube.gameObject.SetActive(false); for (int i = 0; i < intArray.Count; i++) { var pos = cube.transform.position + offset * i; BubbleSortNewItem newItem = Instantiate(cube, pos, cube.transform.rotation); newItem.SetNumber(intArray[i]); newItem.gameObject.SetActive(true); cubes.Add(newItem); } yield return(new WaitForSeconds(0.5f)); for (int turn = 0; turn < intArray.Count; turn++) { int fixCount = turn; int checkLength = intArray.Count - fixCount; checkLength = checkLength - 1;// 오른쪽 왼쪽을 한쌍으로 검사하므로 최대길이 -1까지만 검사하면 된다 for (int x = 0; x < checkLength; x++) { int downNumber = intArray[x]; int upNumber = intArray[x + 1]; // todo: 지금 체크 하고 있는거 노란색으로 표시. var downCube = cubes[x]; var upCube = cubes[x + 1]; yield return(StartCoroutine( ChangeColorCo(upCube, downCube, Color.yellow))); if (downNumber > upNumber) { // 빨간색 지정. yield return(StartCoroutine( ChangeColorCo(upCube, downCube, Color.red))); //upCube.ChangeColor(new Color(1, 0, 0)); // Color.red //downCube.ChangeColor(new Color32(255, 0, 0, 0)); //intArray에 있는 숫자 바꾸기. Swap(x, intArray); //박스의 위치 바꾸기 var downPosition = downCube.transform.position; var upPosition = upCube.transform.position; //cubes박스정보도 스왑하자. cubes[x] = upCube; cubes[x + 1] = downCube; downCube.transform.DOMove(upPosition, 0.5f).SetEase(Ease.InBounce); upCube.transform.DOMove(downPosition, 0.5f).SetEase(Ease.InBounce); yield return(new WaitForSeconds(0.5f)); } else { yield return(StartCoroutine( ChangeColorCo(upCube, downCube, Color.green))); } yield return(StartCoroutine (ChangeColorCo(upCube, downCube, Color.white))); } // 고정된 블락을 회색으로 표시 cubes[intArray.Count - 1 - turn].ChangeColor(Color.gray); } void Swap(int index, List <int> intArray) { int leftIndex = index; int rightIndex = index + 1; int temp = intArray[leftIndex]; intArray[leftIndex] = intArray[rightIndex]; intArray[rightIndex] = temp; } }