Just a matter of curiosity here.
When you write plugins for Unity on the iOS platform, plugins have limited callback functionality with controlled handling (from the plugin and then to Unity). Basically this documentation: iOS plugin Unity documentation
indicates that the signature of the function you can call back is as follows:
Only script methods that match the following signature can be called from native code: function MethodName (message: string)
The signature defined in C looks like this:
void UnitySendMessage (const char * obj, const char * method, const char * msg);
So this pretty much means that I can only send strings to Unity.
Now in my plugin I use protobuf-net to serialize objects and send them back to the unit for deserialization. I got this to work, but the solution I feel is pretty ugly and not very elegant:
Person* person = [[[[[Person builder] setId:123] setName:@"Bob"] setEmail:@" bob@example.com "] build]; NSData* data = [person data]; NSString *rawTest = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; UnitySendMessage("GameObject", "ReceiveProductRequestResponse", [rawTest cStringUsingEncoding:NSUTF8StringEncoding]);
Basically I just encode the byte stream to a string. In Unity, I get the bytes of the string and deserialize from there:
System.Text.UTF8Encoding encoding=new System.Text.UTF8Encoding(); Byte[] bytes = encoding.GetBytes(message);
It works. But is there no other way to do this? Perhaps someone has an idea of how this can be done in some other way?
Codingbeagle
source share