Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Checking Between Android & IOS

Discussion in 'Scripting' started by GoodNight9, Apr 4, 2021.

  1. GoodNight9

    GoodNight9

    Joined:
    Dec 29, 2013
    Posts:
    123
    Would it be better to use in the start method :

    Code (CSharp):
    1.         if (Application.platform == RuntimePlatform.Android) gameId = googleID; //IF Android use Android ID
    2.         else if (Application.platform == RuntimePlatform.IPhonePlayer) gameId = appleID; //Else IF Apple use Apple ID
    Or this at the top where the variables are:

    Code (CSharp):
    1. public class AdsButton : MonoBehaviour, IUnityAdsListener
    2. {
    3.     #if UNITY_IOS
    4.     private string gameId = "appleID";
    5.     #elif UNITY_ANDROID
    6.     private string gameId = "googleID";
    7.     #endif
    8.     Button myButton;
    9.     public string mySurfacingId = "rewardedVideo";
    10. }
    Thank you for your time-!
     
  2. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    I would imagine the latter, platform dependent compilation, would be better. It's not so much selecting based on platform, but those portions of the code marked for the platform, exist only in that platform. Meaning, on the IOS built version of your app, the line "private string gameId = "googleID";" effectively does not exist.

    platform dependent compilation also isn't restricted to the top of your class, you can have it in pretty much any part of your code. for example, in your start method:
    Code (CSharp):
    1. private void Start(){
    2.     #if UNITY_IOS
    3.     gameId = googleID;
    4.     #elif UNITY_ANDROID
    5.     gameId = appleID;
    6.     #endif
    7.     //*rest of your code...
    8. }
     
    GoodNight9 likes this.
  3. GoodNight9

    GoodNight9

    Joined:
    Dec 29, 2013
    Posts:
    123
    OOO I didn't know I could move the code inside the start, THANK YOU, my question has been answered!!