Adding products by code (editor or runtime)



  • I dont understand how the data flows from the IAP Settings window to the IAP Manager. Could you clarify that please be as detailed as posible

    //edited thread title after question was more clear -- Baroni



  • IAP data does not 'flow' from the IAP Settings window to the IAPManager, the data IS saved in the IAPManager all along. To clarify: the IAPManager has a dictionary named 'IAPs' containing all groups of products, which is then displayed by the IAP Settings window.  The IAP Settings editor window DOES NOT save any information in itself.

    Not sure about the reason you are asking or what other details you need?



  • Im just trying to understand how everything works, because I need to modify it for it to fit my needs.

    how is the data saved in the IAPManager prefa, how can i write,read without opening the IAP Settings?



  • What's your requirement then?

    Our scripting reference has an overview of public methods and variables of the IAPManager. You can access the IAPs list (it's not a dictionary, sorry, correction of my previous post) at any time - both at editor or runtime to add/modify groups of products and the product data itself. The IAP Settings window does nothing more than to read and write to the IAPs list directly. You can also get a specific product at runtime via IAPManager.GetIAPObject(your product id) and modify that.



  • thanks for the response.

    Now that you say that, my question is how does The IAP Settings windowwrite to the IAPs list directly??? could you provide the section of the code that does that please



  • from what i understand so far from reviewing the code.

    • script = FindIAPManager(); on IAPEditor.cs    read IAPs
    • DrawIAP(script.IAPs);                                      visualize IAPs on IAP settings window
    • IAP settings window Editing?????                    Edit IAPs
    • PrefabUtility.ReplacePrefab(go, IAPPrefab);      save IAPs

    i want to understand how step 3 and 4 work, how do they modify the data in the prefab and save it?. my ultimate goal is to be able to do everything from code without having to use the IAP settings window.



  • Please do not post source code of our asset publicly - this is a paid asset! I have removed the code snippets from your post.

    With your latest post, it gets more transparent that your question is nothing specific to our asset, but how to edit variables by code, in the editor, in general?

    Your points 1., 2., and 4. are correct so far. However you must know that, in order to "do everything from code" in the editor, you need to write an editor script too. This editor scripts then needs a button or something else to execute the code you wrote - and, be aware that editor scripts only exist in the editor, so this can't be executed at runtime. I guess this is not what you want, since it still requires opening an editor window manually, and you are approaching this from the wrong angle.

    Just for completeness sake, point 2. and 3. are the same thing. They happen at the same line. This, for example, displays a text field for the current product identifier value and writes it back to the object at the same time.

    obj.id = EditorGUILayout.TextField(obj.id, GUILayout.MaxWidth(150));

    Nonetheless, what stops you from using the methods I wrote in my last post? You absolutely can do everything from code, just by modifying the IAPManager IAPs list before the IAPManager initializes itself (before InitIds() is called in its Awake method). For example, this would add a new IAP group with one product in it:

    //create group
    IAPGroup newGroup = new IAPGroup();
    newGroup.id = "My unique Group ID";
    newGroup.name = "My new Group Name";
    
    //create IAPObject
    IAPObject newObject = new IAPObject();
    newObject.id = "My new Product Identifier";
    newObject.title = "My new Product Title";
    
    //add IAPObject to IAPGroup
    newGroup.items.Add(newObject);
    
    //add IAPGroup to IAPManager IAPs list
    IAPs.Add(newGroup);
    

    Note that this is an example and IAPObject has actually more variables to set, all listed in our scripting reference. What you also want to do is to access a ShopManager instance in the scene and assign the group id + prefab game object, since this is what then instantiates and renders the IAPGroup in your shop scene. Note that you don't have to do that if you create an IAPGroup in the IAPSettings editor first. You can then access its existing items list and only have to add IAPObjects via code.



  • @nogoodnamesleft said in Adding Virtual Products At Runtime:

    GameObject.Find("IAPManager").GetComponent<IAPManager>();

    Im trying to follow this example, but its not working for me, im making a editor script to create the IAP Objects, but, althoug im not getting any error, after the execution, when i go to IAP Settings... the new products are not there, i am missing something to save those new products?

                            bool iapExist = iapManager.IAPObjects.ContainsKey(v.name);
                            if ((iapExist && overwrite) || !iapExist)
                            {
                                //create IAPObject
                                IAPObject iapVehicle = new IAPObject();
                                iapVehicle.id = v.name;
                                iapVehicle.title = asset.name;
                                iapVehicle.editorType = IAPType.Virtual;
                                IAPCurrency niap = new IAPCurrency();
                                niap.amount = defaultCost;
                                niap.name = currency;
                                iapVehicle.type = UnityEngine.Purchasing.ProductType.Consumable;
    
                                //add IAPObject to IAPGroup
                                iapManager.IAPs[0].items.Add(iapVehicle);
                            }
    


  • @lordzeon moved your thread here because out of the two threads I linked, you chose the wrong one :) You are trying to manipulate objects on the IAPManager instance (at runtime), while what you want to do is editing them in an editor script. In an editor script you cannot access the instance (which only lives at runtime) - you need to manipulate the IAPManager prefab in the Resources folder.

    As described in this thread above, have a look at our FindIAPManager() method of the IAPEditor script. There we get the prefab file and its IAPManager class. After adding objects (like in your code), we call the SavePrefab method (which does an PrefabUtility.ReplacePrefab) for writing the changes back to the prefab file.


Log in to reply