Im writing a plugin to do some working, and pass the data collected by the plugin to the game.
I want to use the mono_runtime_invoke to process a function defined in c#,
such as
[C#]
public class CS_Plugin{
public struct ResultInfo{
public int flag;
public string ID;
public int state;
public string PID;
public int quantity;
}
static private void TriggerResult(int ResultCode, ResultInfo info){
}
}
typedef struct tag_ResultInfo{
int flag;
MonoString ID;
int state;
MonoString PID;
int quantity;
}ResultInfo, *LPResultInfo;
(void)OnSucceeded
{
MonoMethod *triggerHandler = mono_get_method_from_image(“CS_Plugin:TriggerResult(int,ResultInfo)”, g_monoImage);
if(triggerHandler){
int returncode = 1;
ResultInfo result;
// set value in result …
There is a article there talking about it a little. You might want to look into System.Runtime.InteropServices.Marshal and System.Runtime.InteropServices.MarshalAsAttribute
Hi there, I have an example at home, I will get it a bit later and post it for you. Pretty easy to use actually, from what I remember you just set some parameters and call a class/method by variable name, almost like delegates.
here it is, Have a look at EntryPoint. I made some comments for you, hope this helps!
namespace Retail.UI
{
public static class PrintInterop
{
#region Methods
// Entry point is the name of the class (or it might be the method, not sure), Calling convention should be pretty much as it is here
[DllImport("Retail.Print.Cashup.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, EntryPoint = "printCashup")]
//defibe the params that the method expect. "Routine_printCashup" is the "method name" you want to call it.
public static extern void Routine_printCashup(string headerXml, string xml);
//now you can call this method and define the params you want to send to it (like above)
public static void PrintCashup(string aHeaderXml, string aXml)
{
Routine_printCashup(aHeaderXml, aXml);
}
#endregion
}
}
Knowing about some of the bugs with Unity chubbspet have you tested this code outside of windows? From my experience for some reason DllImportAttribute ignores the EntryPoint parameter and uses the actual function name. It might just be when on ios with __Internal as the module name, but it should be noted anyways as a bug.
yltang I think the way your going about this could be problematic. I’m assuming your running off a separate native thread than unity? I’m intrested to hear your results. But as far as i’d understand it you’d use the same marshal methods to define the struct in C# and then it looks like your setting everything correctly ( based on this ).
But it wouldn’t surprise me if unity did not support this for some reason… If you want to call from native code to unity your best bet (unless you can get this working, which you should let me know if you can cause that would be great ) is probably UnitySendMessage ( under Deployment → IOS ) though you need to put your data as text and then have unity parse it how you’d like and the pointer you send to UnitySendMessage cointaining the string must be kept in memory untill unity has read it ( which is hard to determin )
Also one of the unfortunate things with UnitySendMessage is its only available on platforms which go to xcode or otherwise built natively outside of unity. Normal windows dll plugins for example do not have this ability.
I think chubbspet might mis-understood my mean.
My problem is I fail to find and execute the function defined in C#(in unity) from my plugin(code with objective c++),
For that I need to pass struct parameter from objective c++.
also ,as BDev had said, unity’s document says we should use the actual function name and “__Internal” as the module name for the plugin code intergrated with the game’s native code.
PS: Im developing IOS game under max os now.
BDev :
As u might thought, the plugin is seperated with unity.
as to the UnitySendMessage,for I need to send some string such as ID,receipt return by apple,they may contain all kinds of chararcters, so the difficulty for this way is to find a seperator to combine the string.