Unity_Learn/Assets/Scripts/Utils/Extension.cs

25 lines
914 B
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
// 익스텐션 메서드는 즉 기존 클래스를 수정하지 않고 새 기능을 추가하기 위해 사용하는 방법으로
// 메서드를 마치 해당 클래스의 멤버인 것처럼 호출이 가능하다.
// 변수를 선언해서 상태를 저장하거나 하는 기능은 없고 기능의 확장만 가능하다.
// 이제 함수의 매개변수 앞에 this를 붙여서 선언을 해주면 된다.
public static class Extension
{
public static void BindUIEvent(this GameObject go, Action<PointerEventData> action, Define.UIEvent type = Define.UIEvent.Click)
{
UI_Base.BindUIEvent(go, action, type);
}
public static T GetOrAddComponent<T>(this GameObject go) where T : Component
{
return Util.GetOrAddComponent<T>(go);
}
}