[Unity][개발#3]반복문, for문, 블록코딩

2021. 4. 29. 16:29PROJECT/VR 소프트웨어 코딩교육 플랫폼

저번 글에서 단일동작에 대한 블록코딩까지 완료되었다.

 

구현할 반복문은 for문이다. 아이디어는 for문에서 돌아갈 동작을 임시로 담아둘 커다란 박스를 하나 만들고 박스를 실행 시키는 방식이다.

고민인 점은 현 프로젝트는 VR을 고려하고 있기 때문에 웬만한 인터랙션은 버튼과 드래그로 하려고 한다. 그래서 텍스트를 받지 않을것이기 때문에 몇번 반복할지, 혹은 그 반복을 어떻게 정의해 줄지는 추가적인 논의가 필요하다

 

IEnumerator ForBox() // For()에서 돌아갈 블록들을 여기에 한번 저장을 한다.
    {
        int nSize = transform.childCount;
        for (int i = 0; i < nSize; i++)
        {
            GameObject Child = transform.GetChild(i).gameObject; // Panel Main Loop의 자식오브젝트 저장
            FunctionMove FunctionMove = Child.GetComponent<FunctionMove>();
            FunctionRotate FunctionRotate = Child.GetComponent<FunctionRotate>();
            
            if (FunctionMove)   //FunctionMove 즉, 이동 블록일때 실행
            {
                yield return StartCoroutine(FunctionMove.MoveZ()); //z축 1 이동

            }
            else if (!FunctionMove) // 회전 블록 일때 실행
            {
                if (Child.tag == "Rotate_R") //오른쪽 회전
                    yield return StartCoroutine(FunctionRotate.RightRotate());
                else if (Child.tag == "Rotate_L") //왼쪽 회전
                    yield return StartCoroutine(FunctionRotate.LeftRotate());
                else if (Child.tag == "Rotate_B") //오른쪽 회전
                    yield return StartCoroutine(FunctionRotate.BackRotate());
            }
        }
    }
    
    public IEnumerator For() //ForBox()에 저장된 블록들을 실행. 최종목표는 특정 스테이지가 끝날때까지 돌아가도록 하고 싶지만
    {                        //현재는 스테이지 없는 관계로 임의로 3번만 반복하도록 count를 추가함. 추후에 수정하면 됨
        for(count = 0; count < 3; count++)
        {
            yield return StartCoroutine(ForBox());
        }
    }

 

단일동작은 ForBox()에 저장한다. 여기서 For문에 들어온 블록이 무슨 블록인지를 판단하고 순차적으로 담는다.

For()에서 저장된 ForBox() 불러와 실행한다. 현재는 3번만 반복하도록 구현해놓은 상태이다. 이는 추후에 반복을 어떻게 종료할지를 규명할 필요가 있다. 

 

생각중인 방법은 슬라이드바를 추가하여 (예 : 0부터 10) 슬라이드바만큼 반복하게끔 하려한다. 

 

public IEnumerator Go() //스타트버튼을 눌렀을때 실행되는 코루틴함수
    {
        yield return new WaitForSeconds(1f); //1초 딜레이
        ///메인패널에 접근

        //GameObject Blocks = GameObject.Find("Main/Canvas/Panel Main Loop").gameObject;
        //Blocks = GameObject.Find("Main").transform.Find("Canvas").transform.Find("Panel Main Loop").gameObject; //Panel Main Loop에 접근
        Blocks = GameObject.FindGameObjectWithTag("section0"); //Panel Main Loop에 접근
        int nSize = Blocks.transform.childCount; //Panel Main Loop의 자식들 갯수
        for (int i = 0; i < nSize; i++)
        {
            GameObject Child = Blocks.transform.GetChild(i).gameObject; // Panel Main Loop의 자식오브젝트 저장
            ///블록의 함수들 다 불러오기
            FunctionMove FunctionMove = Child.GetComponent<FunctionMove>();  //자식오브젝트의 FunctionMove 함수 불러오기
            FunctionRotate FunctionRotate = Child.GetComponent<FunctionRotate>();  //자식오브젝트의 FunctionRotate 함수 불러오기
            FunctionFor FunctionFor = Child.GetComponent<FunctionFor> (); //자식오브젝트의 FunctionFor 함수 불러오기
            FunctionClass FunctionClass = Child.GetComponent<FunctionClass>();
           
            ///차례로 함수들이 있는지 체크하기
            if (FunctionMove)   //FunctionMove 즉, 이동 블록일때 실행
            {
                yield return StartCoroutine(FunctionMove.MoveZ()); //z축 1 이동
            }
            else if (!FunctionMove && FunctionRotate) // 회전 블록 일때 실행
            {
                if (Child.tag == "Rotate_R") //오른쪽 회전
                    yield return StartCoroutine(FunctionRotate.RightRotate());
                else if (Child.tag == "Rotate_L") //왼쪽 회전
                    yield return StartCoroutine(FunctionRotate.LeftRotate());
                else if (Child.tag == "Rotate_B") //오른쪽 회전
                    yield return StartCoroutine(FunctionRotate.BackRotate());
            }

            else if (!FunctionMove && !FunctionRotate && FunctionFor) //반복문 실행
            {
                yield return StartCoroutine(FunctionFor.For()); 
            }
            else if (!FunctionMove && !FunctionRotate && !FunctionFor && FunctionClass)
            {
                Debug.Log("Class");
            }
            
        }
    }

 

기존 player_holder에서 for문 블록을 인식할 수 있는 조건문을 추가하였다.

(else if (!FunctionMove && !FunctionRotate && FunctionFor) //반복문 실행)

 

 

반복문을 지칭하는 블록은 위에 보이는 붉은 박스로 한다. 붉은 박스 안에 기존 단일블록을 추가하는 식으로 한다.

녹색을 띄는 버튼은 단일블록을, 파란색을 띄는 버튼은 반복문블록을 배치 하였다.

즉, 녹색버튼을 누르면 단일동작이 나오고 파란색버튼을 누르면 반복문블록이 나온다.

(예정이지만 남은 두칸에 함수를 추가할것이다)

 

 

보이는 바와 같이 For문 박스를 Panel Main Loop에 넣고 그 위에 단일블록을 넣어 3번 반복됨을 확인할 수 있다.