Search Unity

How to programmatically enable/disable Unity IAP at build time?

Discussion in 'Unity IAP' started by BZH314, Jun 28, 2018.

  1. BZH314

    BZH314

    Joined:
    Jul 12, 2017
    Posts:
    6
    What is Unity's recommended way to handle enabling/disabling IAP within the same project but across different builds?

    The goal is to enable/disable IAP during the build process, for instance in OnPreprocessBuild(), or in OnActiveBuildTargetChanged()

    The manual way to disable (then re-enable) IAP is described by @ap-unity in "IAP Troubleshooting - Remove and Reinstall Unity IAP". Can this be done programmatically?

    Disabling the IAP might have to not even include the DLL in the final build to avoid auto-detection of IAP code by the store, which might lead to a store listing an app as containing IAP when it actually does not.

    Some use cases:
    • Disabling IAP in QA builds
    • Enabling IAP in one Store (ex: Apple) but disabling in another (ex: Android)
    • Free/Paid version of an app (IAP disabled) vs IAP version of the app
    Thanks
     
  2. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    3,665
    Unfortunately no, there isn't a way to programmatically disable IAP as you suggest. You could consider directly editing the UnityConnectSettings.asset file, which stores the Services settings.
     
  3. CanisLupus

    CanisLupus

    Joined:
    Jul 29, 2013
    Posts:
    246
    Hey @JeffDUnity3D, we actually do what you suggest to enable and disable IAP from code when building for several platforms, so I can chime in.

    I strongly believe there is something wrong with the compilation of the UnityPurchase package. We've always had problems with enabling or disabling the IAP, even manually in the inspector (i.e. not from code), such as errors that won't go away until the UnityPurchasing folders are reimported, or until we restart the editor. We also commonly get errors of broken DLLs in the Bin folder (Editor.dll, Stores.dll) that we don't touch at all, when simply changing that setting.

    These problems seem to be harder to reason about when using custom assembly definitions or when trying to create builds, as is our case. Actually, just trying to force Unity to compile scripts after we change UnityConnectSettings.asset is not exactly smooth sailing, since there seems to be a bug in the compilation.

    I've submitted a bug report (case 1136555) but I'll reproduce the text contents here:

    -----------------------------------------------------------------

    1. What happened

    When trying to enable/disable the IAP system from code, we found a compilation problem with UnityPurchasing, where at a specific point some assemblies see the UNITY_PURCHASING define enabled and others disabled.


    2. How we can reproduce it using the example you attached

    The project runs the most up to date IAP system version, 1.20.1 and contains the imported UnityPurchasing system (UnityChannel and UnityPurchasing folders in Plugins) and a single editor script in Editor/Commands.cs. This script sets two menu items on the top Unity toolbar, under "Commands". IAP is enabled in the Services window.

    NOTE: You may need to log into your organization in the Services tab and create a project for this, enabling IAP afterwards. The example project uses our info.

    1. Go to Commands in the top bar and select "Write UnityConnectSettings". This reads the UnityConnectSettings.asset file and writes it back EXACTLY as it was. It will print to the console.

    2. Nothing happens since we haven't forced scripts to recompile, so open Commands.cs and comment out [MenuItem("Commands/Dummy!")]:
    // [MenuItem("Commands/Dummy!")]

    3. Return to Unity.

    4. Commonly, a dialog appears saying "Compiling scripts", which finishes very quickly. Go to "Commands" at the top, you should see that the Dummy command is gone, as expected, and there are no errors. If the dialog does not appear (it can happen), go to step 6.

    5. Return to Commands.cs and uncomment [MenuItem("Commands/Dummy!")] again.

    6. Unity will recompile (now without the dialog) and you will get a warning that is defined at the top of Commands.cs and appears when UNITY_PURCHASING is disabled:
    Assets\Editor\Commands.cs(6,10): warning CS1030: #warning: 'UNITY_PURCHASING is disabled! (but is it really...?)'

    7. Change something in Commands.cs again and the warning will go away because UNITY_PURCHASING is enabled for this assembly (Assembly-CSharp-Editor) from this point on.

    The problem is that IAP was actually always enabled throughout this process, and the UnityPurchasing folder seems to think so too, since PurchasingCheck.cs prints this in case it isn't: "Unity IAP plugin is installed, but Unity IAP is not enabled. Please enable Unity IAP in the Services window."

    -----------------------------------------------------------------

    I'd also like to add that the IAP system still causes warnings on an empty project:

    > Assembly: 'Assets/Plugins/UnityPurchasing/Bin/Stores.dll' uses obsolete Unity API (UnityUpgradable)
    > Assembly: 'Assets/Plugins/UnityPurchasing/Bin/Editor.dll' uses obsolete Unity API (UnityUpgradable)
    > Finished updating scripts / assemblies
    > Assets\Plugins\UnityChannel\XiaomiSupport\Editor\AppStoreSettingsEditor.cs(36,18): warning CS0649: Field 'AppStoreSettingsEditor.ReqStruct.currentStep' is never assigned to, and will always have its default value null

    Daniel
     
    Last edited: Mar 15, 2019 at 9:08 AM
  4. CanisLupus

    CanisLupus

    Joined:
    Jul 29, 2013
    Posts:
    246
    Updated the text with the case number.
     
  5. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    3,665
  6. CanisLupus

    CanisLupus

    Joined:
    Jul 29, 2013
    Posts:
    246
    Would this help in addition to the bug report from the editor? I ask because the Project dropdown in that link doesn't seem to show the project I created for the example project, which is called "Change IAP setting from code" and although the problem can happen in any of our projects I would like to keep it simple for the team and not ask for support for a much more complicated project.

    The "Change IAP setting from code" shows just fine in the organization's project list:
    upload_2019-3-13_14-37-3.png
     
  7. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    3,665
    I will look forward to your support ticket. We don't need the project, only the code you mentioned above.
     
  8. CanisLupus

    CanisLupus

    Joined:
    Jul 29, 2013
    Posts:
    246
    I noticed there was no way to attach files in the support ticket. I will send it with the code in the body.

    I'll leave it here too. The code is the following, on a Commands.cs file (inside an Editor folder):

    Code (CSharp):
    1. using System.IO;
    2. using UnityEditor;
    3. using UnityEngine;
    4.  
    5. #if !UNITY_PURCHASING
    6. #warning UNITY_PURCHASING is disabled! (but is it really...?)
    7. #endif
    8.  
    9. public static class Commands
    10. {
    11.     private const string filePath = "ProjectSettings/UnityConnectSettings.asset";
    12.  
    13.     [MenuItem("Commands/Write UnityConnectSettings")]
    14.     public static void WriteUnityConnectSettings()
    15.     {
    16.         string content = File.ReadAllText(filePath);
    17.         File.WriteAllText(filePath, content);
    18.         Debug.Log("Wrote file at " + filePath);
    19.     }
    20.  
    21.     [MenuItem("Commands/Dummy!")]
    22.     public static void Dummy()
    23.     {
    24.     }
    25. }
    26.  
     
  9. CanisLupus

    CanisLupus

    Joined:
    Jul 29, 2013
    Posts:
    246
    @JeffDUnity3D I submitted the ticket using the green button but it didn't confirm the sending. It actually left me in the same page but now displaying this:

    upload_2019-3-14_9-24-17.png

    The grey button doesn't seem to change even after changing fields. I don't know if it actually sent anything.

    Nevertheless, everything is here in this thread. If you need the project for clarity let me know.
     
  10. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    3,665
    I have been able to reproduce, thank you for the GREAT steps to reproduce. I will let the IAP team know, any fixes would need to be included in a future release.
     
  11. CanisLupus

    CanisLupus

    Joined:
    Jul 29, 2013
    Posts:
    246
    Fantastic, thanks for looking into this!