Checking Between Android & IOS

Would it be better to use in the start method :

        if (Application.platform == RuntimePlatform.Android) gameId = googleID; //IF Android use Android ID
        else if (Application.platform == RuntimePlatform.IPhonePlayer) gameId = appleID; //Else IF Apple use Apple ID

Or this at the top where the variables are:

public class AdsButton : MonoBehaviour, IUnityAdsListener
{
    #if UNITY_IOS
    private string gameId = "appleID";
    #elif UNITY_ANDROID
    private string gameId = "googleID";
    #endif
    Button myButton;
    public string mySurfacingId = "rewardedVideo";
}

Thank you for your time-!

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:

private void Start(){
    #if UNITY_IOS
    gameId = googleID;
    #elif UNITY_ANDROID
    gameId = appleID;
    #endif
    //*rest of your code...
}
1 Like

OOO I didn’t know I could move the code inside the start, THANK YOU, my question has been answered!!