포스트

[C#] 인터페이스 문법 with Unity

[C#] 인터페이스 문법 with Unity

C# (Unity 기준) 인터페이스 문법 상세 설명

1. 인터페이스란?

  • 메서드 시그니처(선언)만 정의
  • 구현 내용은 클래스에서 담당
  • 다중 상속 가능
  • 객체 간 공통 동작 보장

“이런 기능은 있어야 한다”는 규칙/약속만 제공


2. 기본 문법

1
2
3
4
public interface IExample
{
    void DoSomething();
}

구현 클래스:

1
2
3
4
5
6
7
public class ExampleClass : IExample
{
    public void DoSomething()
    {
        Debug.Log("DoSomething 실행됨");
    }
}

3. 인터페이스 특징

특징설명
다중 상속 가능클래스는 여러 인터페이스 구현 가능
접근 제한자 불가메서드는 기본 public (명시 불필요)
필드 정의 불가변수 선언 불가능 (속성은 가능)
생성자 정의 불가생성자 작성 불가

4. 다중 인터페이스 구현 예시

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public interface IDamageable
{
    void TakeDamage(int amount);
}

public interface IHealable
{
    void Heal(int amount);
}

public class Player : IDamageable, IHealable
{
    public void TakeDamage(int amount)
    {
        Debug.Log($"{amount} 데미지 받음");
    }

    public void Heal(int amount)
    {
        Debug.Log($"{amount} 회복함");
    }
}

5. Unity에서의 사용 예시 (다형성)

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
public interface IDamageable
{
    void TakeDamage(int damage);
}

public class Enemy : MonoBehaviour, IDamageable
{
    public void TakeDamage(int damage)
    {
        Debug.Log($"Enemy: {damage} 피해 받음");
    }
}

public class DestructibleObject : MonoBehaviour, IDamageable
{
    public void TakeDamage(int damage)
    {
        Debug.Log($"파괴 오브젝트: {damage} 피해 받음");
    }
}

void Attack(GameObject target)
{
    IDamageable damageable = target.GetComponent<IDamageable>();
    if (damageable != null)
    {
        damageable.TakeDamage(10);
    }
}

EnemyDestructibleObject 모두 같은 방식으로 처리 가능 (다형성)


6. 속성 포함 가능

1
2
3
4
5
6
7
8
9
public interface IHasHealth
{
    int Health { get; set; }
}

public class Enemy : MonoBehaviour, IHasHealth
{
    public int Health { get; set; } = 100;
}

→ 속성도 반드시 구현해야 함


7. 인터페이스 vs 추상 클래스

항목인터페이스추상 클래스
다중 상속OX
필드/변수XO
생성자XO
기본 구현 제공XO

8. 명시적 인터페이스 구현

1
2
3
4
5
6
7
8
interface IWalk { void Move(); }
interface IRun { void Move(); }

public class Character : IWalk, IRun
{
    void IWalk.Move() { Debug.Log("걷기"); }
    void IRun.Move() { Debug.Log("달리기"); }
}

→ 같은 시그니처 충돌 시 구분해 구현 가능


9. Unity 기본 인터페이스 예시

인터페이스역할
IPointerClickHandlerUI 클릭 처리
IBeginDragHandler드래그 시작 처리
IEndDragHandler드래그 끝 처리
IDragHandler드래그 중 처리

Unity의 이벤트 시스템에서 인터페이스 기반 이벤트 처리에 사용됨.


10. 요약

  • 인터페이스는 규약(약속) 제공, 구현은 클래스 담당
  • 다중 상속 가능, 메서드/속성 선언 가능
  • 다형성, 컴포넌트 상호작용에 유용
  • Unity에서는 이벤트 처리, 메시지 전송, 공통 동작 보장에 자주 사용
이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.