4

I'm currently writing an In-App purchase plugin for Unity in Objective-C using pure C as the API between unity and the objective-c code.

Introduction to the problem I am facing

The basic functionality already works. That is, I already have a very basic function call in my plugin to start a product request to the Apple App Store which can be called from Unity. In the unity script you simply call this method like this:

[DllImport ("__Internal")]
private static extern void RequestProducts(string[] productIdentifiers, int arrayCount);                          

public static void RequestProductData( string[] productIdentifiers )
{
    RequestProducts(productIdentifiers, productIdentifiers.Length);
}

This calls the C function which looks like this:

void RequestProducts(char* productIdentifiers[], int arrayCount)
{
    // Call the In-App purchase manager to initialize the product request
    [[InAppPurchaseManager sharedInAppPurchaseManager] RequestProductData:productIdentifierCollection];
}

Now I've omitted the part of the C function which takes the productIdentifiers array and converts it into an NSArray just to keep it simple and explain the problem I'm trying to solve.

Now the InAppPurchaseManager class is a singleton class. The RequestProductData method initiates the product request to the app store.

The Problem

When the StoreKit gives me a response back with all the products, this is where it starts to get tricky for me. I want to pass all the relevant data for each product retrieved back to the unity C# script so you can handle the data in your game. This means storing:

-The name of each product

-The description of each product

-The price of each product

An obvious solution would be to make a struct or class which contains this information and then make an instance for each product and put them all into an array.

The Question

Now the question is, how would I go about sending this array of complex data structures back to the Unity script? Unity's official documentation mentions that you can send messages back to Unity using this method call: UnitySendMessage("GameObjectName1", "MethodName1", "Message to send");

I got the call to work, but the obvious problem is that it only allows you to send strings.

So:

how would I go about sending arrays of complex data structures back to the Unity script? Is this even possible?

4
  • 1
    You need to serialize the objects and pass them to the C# component. There is no "built in" way to pass complex objects or collection of objects. Commented Aug 31, 2012 at 13:48
  • Aha I see :) Can you recommend any articles/resources on the basis of serialization/marshalling as I've never really worked with this before. Any kind of references would be appriciated. I will look into this :) Commented Aug 31, 2012 at 16:16
  • @user1163640: did you managed to use protocol buffer to exchange data between unity and the plugin? Commented Dec 5, 2012 at 14:50
  • Yes :) The method is actually in successful use in the latest iOS game from the company I'm a part of :) It should hopefully be approved by Apple and released within this month :) I used it as part of the in-app purchase plugin and it works wonderfully. Commented Dec 6, 2012 at 7:58

1 Answer 1

1

Follow up on my comment and your comment: if you want a robust solution, I would recommend that you check out this answer. If this is something performance critical, then you will have to get a lot more "manual" with your serialization. So how performance critical is this transfer of data?

Sign up to request clarification or add additional context in comments.

4 Comments

Aha I see, I will definitely check up on this :) As to the performance critical point, I would not say it is something that is critical down to milliseconds or even a few seconds. The plugin is used to fetch in-app purchases from the Apple App Store (since Unity does not provide native access to the StoreKit framework which is written in Objective-C). So you initialize a request, and get back an array of product information (prices, descriptions, and so on). Most probably it will be run at the start of the game and then cached. So as long as this approach isn't extremely slow
it should not be a problem. Do you think this solution will suffice to what I've descriped?
Yep, proto buffers are fairly fast, they're often used at real time and they should suffice for what you're describing. I was just pointing out that if you're looking for really high performance, then you'll have to write something more customized by yourself.
All right :) Thank you very much for the help. I will look into the answer provided.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.