(Unityで)以下のようにDllImportで関数(静的関数)を定義し、そのDllImport関数を実行するExec関数を書いています。
exec関数は静的関数ではなくインスタンス関数です。
また、DllImport関数の処理は非同期であるため、Promiseライブラリを使用して実行結果の処理を行っています。
このようにExecA関数とExecB関数は、実行するDllImport関数とイベントハンドラが違うだけです。また、このようにDllImport関数とイベントハンドラが違うだけというパターンが多数あるため、こういったパターンのExecXXX関数を1つのexec関数にまとめてDllImport関数とイベントハンドラは引数で渡すことで、コード量を減らしたいと思っているのですが、DllImportの関数を引数で渡すことはできるのでしょうか?
c#
1class Hoge 2{ 3 public static Dictionary<string, Hoge> dicHoge; 4 5 [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)] 6 static void OnAfterSceneLoadRuntimeMethod() 7 { 8 var tmpKey = Guid.NewGuid.ToString(); 9 dicHoge = new Dictionary<string, Hoge>(); 10 dicHoge.add(tmpKey, new Hoge()); 11 Hoge.DllInit( 12 tmpKey, 13 Action<string, string> _dllInitResult, 14 Action<int> _dllFuncAResult, 15 Action<int> _dllFuncBResult 16 ); 17 } 18 19 [MonoPInvokeCallback(typeof(Action<string, int>))] 20 public static void _dllInitResult(string tmpKey, string instanceKey) 21 { 22 var instance = dicHoge[tmpKey]; 23 instance.instanceKey = instanceKey; 24 dicHoge.Remove(tmpKey); 25 dicHoge.add(instanceKey, instance); 26 } 27 28 [DllImport("__Internal")] 29 public static extern void DllFuncA(); 30 31 [DllImport("__Internal")] 32 public static extern void DllFuncB(); 33 34 [MonoPInvokeCallback(typeof(Action<string, int>))] 35 public static void _dllFuncAResult(string instanceKey, int result) 36 { 37 dicHoge[instanceKey].OnFuncAResult.Invoke(result); 38 } 39 40 [MonoPInvokeCallback(typeof(Action<string, int>))] 41 public static void _dllFuncBResult(string instanceKey, int result) 42 { 43 dicHoge[instanceKey].OnFuncBResult.Invoke(result); 44 } 45 46 public string instanceKey; 47 private const int SUCCESS = 0; 48 private const int ERROR = -1; 49 public Action<string> OnFuncAResult; 50 public Action<string> OnFuncBResult; 51 52 public Promise ExecA() 53 { 54 var promise = new Promise((resolve, reject) => 55 { 56 Action<int> handler = null; 57 handler = (result) => 58 { 59 this.OnFuncAResult -= handler; 60 if (result == SUCCESS) 61 { 62 resolve(); 63 } 64 else 65 { 66 reject(new Exception("Error")); 67 } 68 }; 69 this.OnFuncAResult += handler; 70 Hoge.DllFuncA(); 71 }); 72 73 return promise; 74 } 75 76 public Promise ExecB() 77 { 78 var promise = new Promise((resolve, reject) => 79 { 80 Action<int> handler = null; 81 handler = (result) => 82 { 83 this.OnFuncBResult -= handler; 84 if (result == SUCCESS) 85 { 86 resolve(); 87 } 88 else 89 { 90 reject(new Exception("Error")); 91 } 92 }; 93 this.OnFuncBResult += handler; 94 Hoge.DllFuncB(); 95 }); 96 97 return promise; 98 } 99}
c#
1// 実際はコンパイルエラーとなるが、このようにしたいというコード 2class Hoge 3{ 4 // 略 5 6 public Promise ExecA() { 7 exec(DllFuncA, this.OnFuncAResult); 8 } 9 10 public Promise ExecB() { 11 exec(DllFuncB, this.OnFuncBResult); 12 } 13 14 private Promise exec(func dllFunc, Action<int> eventHandler) 15 { 16 var promise = new Promise((resolve, reject) => 17 { 18 Action<int> handler = null; 19 handler = (result) => 20 { 21 eventHandler -= handler; 22 if (result == SUCCESS) 23 { 24 resolve(); 25 } 26 else 27 { 28 reject(new Exception("Error")); 29 } 30 }; 31 eventHandler += handler; 32 dllFunc(); 33 }); 34 35 return promise; 36 } 37}
回答1件
あなたの回答
tips
プレビュー