「Unity/Csharp/クラス/インターフェイス」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→インターフェイスを2つ実装したサンプル) |
|||
行75: | 行75: | ||
{ | { | ||
public void OnClickUser(); | public void OnClickUser(); | ||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | ==インターフェースのジェネリック化サンプル== | ||
+ | 呼び出し処理 | ||
+ | |||
+ | LoadDialogの引数に、色々なクラスを、入れることができる。 | ||
+ | <pre> | ||
+ | var timeDetailDialogParams = new TimeDetailDialogParams(1, () => { Debug.Log("Close"); }); | ||
+ | var loadDialogUseCase = new LoadDialogUseCase(); | ||
+ | loadDialogUseCase.LoadDialog(timeDetailDialogParams); | ||
+ | </pre> | ||
+ | |||
+ | ILoadDialogDelegate.cs | ||
+ | <pre> | ||
+ | public interface ILoadDialogDelegate | ||
+ | { | ||
+ | void LoadDialog<TParams>(TParams parameters); | ||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | LoadDialogUseCase.cs | ||
+ | <pre> | ||
+ | using System; | ||
+ | using UnityEngine; | ||
+ | |||
+ | public class LoadDialogUseCase : ILoadDialogDelegate | ||
+ | { | ||
+ | public void LoadDialog<TParams>(TParams parameters) | ||
+ | { | ||
+ | var prop = parameters.GetType().GetProperty("TimeId"); | ||
+ | if (prop != null) | ||
+ | { | ||
+ | int timeId = (int)prop.GetValue(parameters); | ||
+ | Debug.Log($"TimeId: {timeId}"); | ||
+ | } | ||
+ | var closeEventProp = parameters.GetType().GetProperty("CloseEvent"); | ||
+ | if (closeEventProp != null) | ||
+ | { | ||
+ | var closeAction = closeEventProp.GetValue(parameters) as Action; | ||
+ | closeAction?.Invoke(); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | TimeDetailDialogParams.cs | ||
+ | <pre> | ||
+ | public class TimeDetailDialogParams | ||
+ | { | ||
+ | public int TimeId { get; } | ||
+ | public Action CloseEvent { get; } | ||
+ | |||
+ | public TimeDetailDialogParams(int timeId, Action closeEvent) | ||
+ | { | ||
+ | TimeId = timeId; | ||
+ | CloseEvent = closeEvent; | ||
+ | } | ||
} | } | ||
</pre> | </pre> |
2025年5月9日 (金) 04:42時点における版
サンプル
IUseCaseインターフェイスをAdUseCaseクラスに実装
呼び出し
AdUseCase adUseCase = new AdUseCase(); adUseCase.Start();
AdUseCase.cs
public class AdUseCase : IUseCase { public void Start() { Debug.Log("AdUseCase.Start"); } public void Destroy() { Debug.Log("AdUseCase.Destroy"); } }
IUseCase.cs
interface IUseCase { void Start(); void Destroy(); }
インターフェイスを2つ実装したサンプル
IUseCaseインターフェイスをUserUseCaseクラスに実装
呼び出し
UserUseCase userUseCase = new UserUseCase(); userUseCase.Start(); (userUseCase as IUserDelegate).OnClickUser(); // IUserDelegateだけのメソッドを持つインスタンスにして、メソッドを実行したい場合。
UserUseCase.cs
public class UserUseCase : IUseCase, IUserDelegate { public void Start() { Debug.Log("UserUseCase.Start"); } public void Destroy() { Debug.Log("UserUseCase.Destroy"); } public void OnClickUser() { Debug.Log("UserUseCase.OnClickUser"); } }
IUseCase.cs
interface IUseCase { void Start(); void Destroy(); }
IUserDelegate.cs
public interface IUserDelegate { public void OnClickUser(); }
インターフェースのジェネリック化サンプル
呼び出し処理
LoadDialogの引数に、色々なクラスを、入れることができる。
var timeDetailDialogParams = new TimeDetailDialogParams(1, () => { Debug.Log("Close"); }); var loadDialogUseCase = new LoadDialogUseCase(); loadDialogUseCase.LoadDialog(timeDetailDialogParams);
ILoadDialogDelegate.cs
public interface ILoadDialogDelegate { void LoadDialog<TParams>(TParams parameters); }
LoadDialogUseCase.cs
using System; using UnityEngine; public class LoadDialogUseCase : ILoadDialogDelegate { public void LoadDialog<TParams>(TParams parameters) { var prop = parameters.GetType().GetProperty("TimeId"); if (prop != null) { int timeId = (int)prop.GetValue(parameters); Debug.Log($"TimeId: {timeId}"); } var closeEventProp = parameters.GetType().GetProperty("CloseEvent"); if (closeEventProp != null) { var closeAction = closeEventProp.GetValue(parameters) as Action; closeAction?.Invoke(); } } }
TimeDetailDialogParams.cs
public class TimeDetailDialogParams { public int TimeId { get; } public Action CloseEvent { get; } public TimeDetailDialogParams(int timeId, Action closeEvent) { TimeId = timeId; CloseEvent = closeEvent; } }