Search Unity

  1. Unity 2019.4 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  3. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

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:
    7,372
    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:
    336
    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
    alex_roboto likes this.
  4. CanisLupus

    CanisLupus

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

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    7,372
  6. CanisLupus

    CanisLupus

    Joined:
    Jul 29, 2013
    Posts:
    336
    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:
    7,372
    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:
    336
    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:
    336
    @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:
    7,372
    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:
    336
    Fantastic, thanks for looking into this!
     
  12. CanisLupus

    CanisLupus

    Joined:
    Jul 29, 2013
    Posts:
    336
    Hello @JeffDUnity3D. Are there any updates about this?

    We are going mad with this issue. :) It breaks our build process -- building for all platforms one by one -- because after changing the IAP setting from code (we disable IAP for desktop builds) it usually fails the next compilation.

    Reimporting scripts in certain assemblies works, but only under specific circumstances, since the Unity IAP must fail a compilation before we can reimport a script to fix it. It's really strange. We have wasted many days over the issues with the IAP system... :(

    Daniel
     
  13. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    7,372
    The IAP team is aware of the issue, no updates at this time.
     
  14. DoomGoober

    DoomGoober

    Joined:
    Dec 12, 2013
    Posts:
    7
    I don't know if this is the same issue, but I managed to hack the IAP to not be included for certain platforms but to be included with other platforms. Not sure if this is what OP wanted but here goes:
    1. #if define out all calls to UnityEngine.Purchasing for the platform you want to exclude in your own code. (Obviously.)
    2. In Plugins\UnityPurchasing\script\*.cs #if define out every file for the platform you want to exclude.
    3. In Plugins\UnityPurchasing\Editor\*.cs #if define out every file for the platform you want to exclude.
    4. In \UnityPurchasing\Bin\*.dll and every subdirectory, exclude for your platform. Press Apply.
    5. BEWARE: In \UnityPurchasing\Bin\Facebook\*.dll and Facebook\live\*.dll exclude for your platform. Press Apply. If a dialog asks you to apply changes again, hit Revert. (There seems to be some Editor Script the likes to change these settings out from under you.)
    6. Go back and double check all the DLLs under Facebook are excluded for your platform and not magically turned back on.
    7. Select a file to make sure the inspector is no longer on a DLL under Facebook (as the magic script will run as long as the inspector is open.)
    I can now build for one platform with IAP and another platform without IAP (and actually I completely disabled Facebook too for the non-IAP platform as I didn't need it. Similar steps of excluding FacebookSDK plugins.)
     
    Last edited: Apr 30, 2020
  15. CanisLupus

    CanisLupus

    Joined:
    Jul 29, 2013
    Posts:
    336
    The IAP compilation issue that I reported more than a year ago (issue 1136555) is still open and seems to have got no updates! :(
    We have lost dozens of hours working around this in our custom build pipeline (it now involves restarting Unity twice before AND after a build for a certain platform, to guarantee that all assemblies see the correct state of the IAP system) and we STILL encounter it in other random situations where we don't even touch the setting.

    Can't anything be done, @JeffDUnity3D? I generally have only good things to say about the Unity team's response to bug reports, and from all my dozens of reports over the years, this is the only one that is still open. :(

    @DoomGoober solution may work for some users (thank you, by the way!) but not for us, since we have macOS builds for the App Store and other macOS builds for release elsewhere that don't use IAPs, among other problems. Besides requiring changes to your package's scripts, at best it would need us to activate a custom define for some builds, always leave the IAP system enabled in the connect settings, and get IAP dlls included on some builds that don't use them.
     
  16. CanisLupus

    CanisLupus

    Joined:
    Jul 29, 2013
    Posts:
    336
  17. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    7,372
    Sorry no updates and perhaps not soon due to resource constraints.
     
  18. CanisLupus

    CanisLupus

    Joined:
    Jul 29, 2013
    Posts:
    336
unityunity