티스토리 뷰

오늘은 너무 피곤하다 ... ㅋㅋㅋ :)

그래도 출장이 아니면 하나씩 게임을 만들어 봐야지 ㅎㅎㅎ

 

오늘은 패널은 만들고, 그리고 끄는 방법

캐릭터 구매정보를 패널에 띄우고, 구매버튼 클릭시 해당 캐릭터를 화면에 생성하는 법을 복습하겠당.

 

오늘 배운것으로 할 수 있는것

 1. 패널을 게임내에서 켜고 끌 수 있다.

 2. 패널에 새롭에 여러가지 이미지나 텍스트 자료를 띄울 수 있다.

 3. 만약에 어떤 아이템이나 케릭터를 구매할 경우 그것을 생성 할 수 있다.

 

입니다.

 

별거 없어 보이지만 음.. 코딩으로 보면 참 괴랄합니다. 궁금한것도 있고. 일단 저는 deep한 이해보다도 ㅋㅋㅋ 실전

위주이기 때문에 오늘 배운내용을 코딩으로 적어 보겠습니다.

 

궁금한건... 일단 2번째 코드에서, Onclick에 함수 집어넣을때 사용했던 'delegate'라는 녀석인데... ㅠ

이건 나중에 알아보도록 하겠습니다.

 

1.

    // 일단 이부분은 새롭게 케릭터를 만들때 랜덤으로 능력치를 정해주는 코드입니다.
    EmployeeControl.Employee CreatEmployee() 
    // 일단 첫번째 궁금한거는 왜 위에서 public void 대신에 Employee class를 썻는지  모르겠다.
    {
        EmployeeControl.Employee info = new EmployeeControl.Employee();
        //여기는 info라는 Employee class에 새로운 정보를 담겠다고 선언
        //참고로 Employee는 내가 직접 만든 Class임
        
        info.name = names;

        int r = Random.Range(0, 2);
        // 이거 중요 특히 뒷부분, 자료형식 특정하기
        info.gender = (EmployeeControl.Gender)r;
        info.hp = 100;
        info.planning = (int) Random.Range(0,51);
        info.programming = (int) Random.Range(0, 51);
        info.design = (int) Random.Range(0, 51);
        info.music = (int) Random.Range(0, 51);
        info.communication = (int) Random.Range(1, 5);
        info.cost = (info.planning + info.programming + info.design + info.music)*12f;
        info.totalspec = (info.planning + info.programming + info.design + info.music);

        return info;
    }

2.

    //이부분이 패널을 열고, 그 패널안의 정보를 생성하는 코드입니다.
    public void OpenHireMarket()
    {
        hirePannel.SetActive(true); //패널을 열고

        EmployeeControl.Employee e = CreatEmployee(); 
        // 위에서 만들었던 랜덤 정보를 새롭게 e라는 class변수에 담고

        // var는 컴퓨터가 알아서 형식을 맞춰줌
        var textName = hirePannel.transform.Find("Text_name").GetComponent<Text>();
        var imageCharactor = hirePannel.transform.Find("Image_charactor").GetComponent<Image>();
        var textWage = hirePannel.transform.Find("Text_wage").GetComponent<Text>();
        var textPlan = hirePannel.transform.Find("Text_planning").GetComponent<Text>();
        var textProg = hirePannel.transform.Find("Text_program").GetComponent<Text>();
        var textMusic = hirePannel.transform.Find("Text_music").GetComponent<Text>();
        var textDesign = hirePannel.transform.Find("Text_design").GetComponent<Text>();
        var textCom = hirePannel.transform.Find("Text_communication").GetComponent<Text>();
        var textTotal = hirePannel.transform.Find("Text_Total").GetComponent<Text>();
        var buttonHire = hirePannel.transform.Find("Button_hire").GetComponent<Button>();
        //여기는 사용하기 쉽게 패널에 있는 각 텍스트에 접근해놓는 부분입니다.

        textName.text = e.name;
        
        //만약 성별이 여자라면
        if(e.gender == EmployeeControl.Gender.Woman)
        {
            imageCharactor.sprite = employeeF[0];
        }
        else
        {
            imageCharactor.sprite = employeeM[0];
        }
		
        //위에서 접근해놓은 각각의 text에 랜덤하게 만들어진 능력치를 입력하는 부분
        textWage.text = e.cost.ToString();
        textPlan.text = e.planning.ToString();
        textProg.text = e.programming.ToString();
        textMusic.text = e.music.ToString();
        textDesign.text = e.design.ToString();
        textCom.text = e.communication.ToString();
        textTotal.text = e.totalspec.ToString();

        buttonHire.onClick.RemoveAllListeners(); //고용버튼 onclick부분에 있던 내용 제거
        buttonHire.onClick.AddListener(delegate { HireNewEmployee((int)e.cost); }); 
        //고용버튼 onclick에 새로운 행동을 추가한다. HireNewEmployee(); 함수를
    }

3. 패널에 있는 케릭터를 생성하는 함수

    public void HireNewEmployee(int cost)
    {
        if(money >= cost)
        {
            GameObject obj = Instantiate(newEmployee, transform.localPosition, Quaternion.identity);
            money -= cost;
        }
        
    }

 

728x90
250x250
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함