본문 바로가기
Software/Unity

[Unity] 7. 함수

by yunaoh2 2021. 8. 31.

1. 반환 데이터가 변수로 있는 함수

자료형 + 함수명 ( 자료형 + 변수명 )
{
함수내용
return 변수명 ;
}
int Heal(int health)
{
    health += 10 ; 
    Debug.Log("힘을 받았습니다." + health);
    return health;
}

 

2. 반환 데이터가 없는 함수

void 함수명()
{
함수내용
}
void Heal()
{
   currenthealth += 10;
   Debug.Log("힘을 받았습니다."+currenthealth);
}

 

 

*지역변수 : 함수 안에 선언된 변수 - 함수 내에서만 사용 가능

 

*전역변수 : 함수 밖에 선언된 변수 - 전체 명령에서 사용됨.

 

 

3. Class

*파일 생성 - 하나의 클래스 입력

 

public class 클래스명 {

}


*public은 외부 클래스에 공개로 설정하는 접근자
*private은 외부 클래스에 비공개 (기본은 비공개로 되어있음)
public classs Actor {
  public int id;
  public  string name;
  public  string title;
  public string weapon;
  public  float strength;
  public int level;

  public string Talk()
   {
      return "대화를 걸었습니다.";
   }

  public  string HasWeapon()
      return weapon;
   }
  public void LevelUp()
   {
      level = level + 1;
   }
}

 

4. 클래스의 인스턴스화 : 정의된 클래스를 변수 초기화로 실체화

기존 전체 파일로 이동

Actor player = new Actor();
player.id = 0;
player.name="나법사";
player.title="현명한";
player.strength=2.4f;
player.weapon="나무 지팡이";
Debug.Log(player.Talk());
Debug.Log(player.HasWeapon());

player.LevelUp();
Debug.Log(player.name+"의 레벨");

'Software > Unity' 카테고리의 다른 글

[Unity] 9. 키보드, 마우스로 이동시키기  (0) 2021.08.31
[Unity] 8. 게임오브젝트의 흐름  (0) 2021.08.31
[Unity] 6. 반복문  (0) 2021.08.25
[Unity] 5. 조건문  (0) 2021.08.25
[Unity] C#파일 생성하고 적용하기  (0) 2021.08.25