UnitySendMessage如何调用接口方法
How to call UnitySendMessage to an interface method
正在开发 Android Unity 插件。
为了从 Android 到 Unity 通信,我使用 UnityPlayer.UnitySendMessage(string s1,string s2, string s3),此方法将三个字符串作为参数:
- s1:将接收消息的游戏对象名称。
- s2:将处理消息的方法的名称。
- s3:消息。
所以此设置有效,在我的 android 插件上,我使用以下代码将消息发送到我的 UnityClass:
UnityPlayer.UnitySendMessage("GameObjectName", "OnResult", results);
在 Unity 中,我名为“GameObjectName”的游戏对象实现了以下方法:
private void OnResult(string recognizedResult);
麻烦来了
现在我希望这个 GameObject class 实现一个接口,使其强制处理“OnResult”。
但是如果我创建以下界面:
//Interface for MonoBehaviour plugin holder
public interface IPlugin
{
void OnResult(string recognizedResult);
}
然后在我的 MonoBehaviour class 上实现了接口,它看起来像:
void IPlugin.OnResult(string recognizedResult)
这个方法好像不行。我尝试同时使用 "OnResult"
和 "IPlugin.OnResult"
调用 UnitySendMessage,但均无效。
我做错了什么?或者这只是一个限制?谢谢!
在 C# 中,您可以像刚才那样 implement interfaces explicitely,或者隐式地。似乎Unity只有在隐式实现时才识别该方法。
interface IPlugin
{
void OnResult(string recognizedResult);
}
class Working : IPlugin
{
public void OnResult(string recognizedResult)
{
throw new System.NotImplementedException();
}
}
class NoWorking : IPlugin
{
void IPlugin.OnResult(string recognizedResult)
{
throw new System.NotImplementedException();
}
}
正在开发 Android Unity 插件。
为了从 Android 到 Unity 通信,我使用 UnityPlayer.UnitySendMessage(string s1,string s2, string s3),此方法将三个字符串作为参数:
- s1:将接收消息的游戏对象名称。
- s2:将处理消息的方法的名称。
- s3:消息。
所以此设置有效,在我的 android 插件上,我使用以下代码将消息发送到我的 UnityClass:
UnityPlayer.UnitySendMessage("GameObjectName", "OnResult", results);
在 Unity 中,我名为“GameObjectName”的游戏对象实现了以下方法:
private void OnResult(string recognizedResult);
麻烦来了
现在我希望这个 GameObject class 实现一个接口,使其强制处理“OnResult”。
但是如果我创建以下界面:
//Interface for MonoBehaviour plugin holder
public interface IPlugin
{
void OnResult(string recognizedResult);
}
然后在我的 MonoBehaviour class 上实现了接口,它看起来像:
void IPlugin.OnResult(string recognizedResult)
这个方法好像不行。我尝试同时使用 "OnResult"
和 "IPlugin.OnResult"
调用 UnitySendMessage,但均无效。
我做错了什么?或者这只是一个限制?谢谢!
在 C# 中,您可以像刚才那样 implement interfaces explicitely,或者隐式地。似乎Unity只有在隐式实现时才识别该方法。
interface IPlugin
{
void OnResult(string recognizedResult);
}
class Working : IPlugin
{
public void OnResult(string recognizedResult)
{
throw new System.NotImplementedException();
}
}
class NoWorking : IPlugin
{
void IPlugin.OnResult(string recognizedResult)
{
throw new System.NotImplementedException();
}
}