私はそれがあなたの問題が、私を解決することを確認することはできません。
[DllExport("SayText", CallingConvention = CallingConvention.Cdecl)]
public static void SayText()
{
var task = new Task(() =>
{
var synth = new SpeechSynthesizer();
synth.SetOutputToNull();
synth.SetOutputToWaveFile("D:\\test3.wav");
synth.SpeakCompleted += SynthOnSpeakCompleted;
synth.SpeakAsync("This is a test text");
});
task.Start();
}
private static void SynthOnSpeakCompleted(object sender, SpeakCompletedEventArgs speakCompletedEventArgs)
{
var synth = (SpeechSynthesizer)sender;
synth.SpeakCompleted -= SynthOnSpeakCompleted;
synth.SetOutputToDefaultAudioDevice();
// I want to trigger an event here so the application knows that the file has been created.
}
そして、ここでは、私はUnity3Dからこの関数を呼んでいる方法ですiosプラグインと同様のものを試しました。
public class CSharpWrapper
{
// Create delegate type
public delegate void TestDelegate();
// Connection with the native side
[DllImport("__Internal")]
private static extern void externMethod(TestDelegate onCompletion);
private static Action callback = null;
// This is most likely the part you are looking for.
// That method is marshalled so it will pass its address to the native side
// When native calls it we can add any code within
[MonoPInvokeCallback(typeof(TestDelegate))]
private static void ManagedTest()
{
if(callback != null) { callback(); }
callback = null;
}
#endif
public static void CallMethod(Action<string> onCompletion)
{
callback = onCompletion;
// Here we pass our own method that is marshalled for native side
externMethod(ManagedTest);
}
}
は、それから私は、.mファイルでのCセクションがあります。
はここでプラグインのC#の側です。
extern "C"
{
typedef void (*TestCallback)();
void externMethod(TestCallback testCallback)
{
// Here communication with Objective-C code
}
}
多分、これはあなたの近くのどこかで完了します。パラメータを追加することも可能です。通常のようにそれらをリストするだけで、マーシャリング属性があなたのためにすべて行います。
ありがとう、私の質問は、C#(Unity)でコールバック関数を定義し、ライブラリ(C#.net 4をネイティブライブラリにエクスポート)から呼び出す方法です。私は関数ハンドルをライブラリに送る方法とそれを呼び出す方法を意味します。両方ともc#、1つには1つはms c#です。 私のMS c#コードは管理されていないことに注意してください。 – FSFisGOOD