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

Question Accessing Google Calendar API with Unity (Android)

Discussion in 'Scripting' started by carstencyliax, Mar 6, 2021.

  1. carstencyliax

    carstencyliax

    Joined:
    Mar 10, 2018
    Posts:
    9
    My dream: Showing my own Google Calendar events in my own privat app (no public store), no multiple users.

    I know, I know, you can find other threads with this topic, but I can't find a working solution and I'm confused when it comes to authorizing with Google. The good: I managed it to get my calendar events on my Windows Unity editor following Googles advices to integrate Calendar API, the bad: I don't know how to this on Android?

    1. Do I need Firebase Google Auth to sign in my own calendar on an Android device, or is Google Calendar API something else, working with own Oauth credentials?
    2. Has anybody a working example how it would work on Android?

    I'm familiar with Google Cloud Console, Firebase Console, Oauth, enabling Signing-In methods, getting google json files for Android, iOS, Webclient and so an but I mix it all up and don't know if there is an easy solution to get my calendar events.

    Please help, I lost days with this. Thanks guys.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    This is going to be almost all Android stuff, not Unity stuff.

    You need to find out what sequence of API calls in Android do what you want.

    Unity has an Android interface layer that may contain enough flexibility to do what you want.

    https://docs.unity3d.com/ScriptReference/AndroidJavaClass.html
     
  3. carstencyliax

    carstencyliax

    Joined:
    Mar 10, 2018
    Posts:
    9
    Thanks for your reply, but this doesn't help. I know how to call external Java methods with Unity but this is not necessary with Google C# examples:

    https://firebase.google.com/docs/auth/unity/google-signin

    and there are .NET libraries packed as DLLs to use them in Unity:

    https://github.com/SaadAnees/Unity-Google-Calendar

    But they are made for accessing the calendar on a desktop plattform.

    The problem is to sign in as a user before you can access the calendar api. And so I startet to read about Google Sign In Firebase API calls. I'm able to login with Firebase but I don't know how I use my credential to access my Google Calendar on Android. I feel that Google Calendar belongs to Google Cloud services but there is no need to login with Google Firebase Auth... just a guess.

    If there is somebody who used Google Calendar in Unity on an Android device... I need your knowledge.
     
  4. EqualCoder

    EqualCoder

    Joined:
    Dec 6, 2020
    Posts:
    3
    any solution ?
     
  5. john_primitive

    john_primitive

    Joined:
    Jan 4, 2019
    Posts:
    7
    I built this app in Unity with an Android calendar integration:
    https://play.google.com/store/apps/details?id=com.PrimeSoftwareSystems.Sundial

    It took a lot of wrestling. Ultimately I had to build an aar that included the UnityActivity extension as outlined in this tutorial:
    https://docs.unity3d.com/Manual/AndroidUnityPlayerActivity.html

    The Java code looks like this:
    Code (CSharp):
    1. public class CalendarsActivity
    2.         extends Activity
    3.         implements CalendarsProxy.Listener {
    4.  
    5.     @Override
    6.     protected void onCreate(Bundle savedInstanceState) {
    7.         Log.d("OverrideActivity", "onCreate called!");
    8.         super.onCreate(savedInstanceState);
    9.  
    10.         this.mCalendars = new ArrayList<>();
    11.  
    12.         this.mCalendarsAdapter = new CalendarsArrayAdapter(this, 0, this.mCalendars);
    13.  
    14.         this.mCalendarsQueryHandler = new CalendarsProxy(this.getContentResolver());
    15.         this.mCalendarsQueryHandler.registerListener(this);
    16.         CalendarsActivity.ctx = this;
    17.         this.queryCalendars();
    18.     }
    19.  
    20.     // called from Unity
    21.     public String getAllCalendarNames() {
    22.         String ret = "";
    23.         for (int i = 0; i < this.mCalendars.size(); i++) {
    24.             ret += this.mCalendars.get(i).getDisplayName() + "|";
    25.         }
    26.  
    27.         return ret;
    28.     }
    There is more code to make this all work that I'm not including here, because it references other source code from a Calendar Library that I can't find. If I locate it, I'll provide a link and also a fork of the code (I was working on this 3 years ago).

    The aar that is assembled from this Java code is placed in the Android/Plugins folder. It is then called from C# code like this:
    Code (CSharp):
    1. Dictionary<string, MyEvent[]> calendarsAndEvents = new Dictionary<string, MyEvent[]>();
    2.             using AndroidJavaClass javaClass = new("com.unity3d.player.UnityPlayer");
    3.             using AndroidJavaObject activity = javaClass.GetStatic<AndroidJavaObject>("currentActivity");
    4.             AndroidJavaObject calObj = activity.Call<AndroidJavaObject>("getAllCalendarNames");
    5.  
    6.             if (calObj.GetRawObject().ToInt32() == 0)
    7.             {
    8.                 // this returns empty if calendars are empty
    9.                 calObj.Dispose();
    10.                 return calendarsAndEvents;
    11.             }
    12.                    
    13.             byte[] calendarNamesBytes = calObj.GetRawObject().ToInt32() != 0
    14.                 ? AndroidJNIHelper.ConvertFromJNIArray<byte[]>(calObj.GetRawObject())
    15.                 : Array.Empty<byte>();
    16.             calObj.Dispose();
    The Android Manifests (one for the app and one for the calendar plugin) need to include the appropriate permissions and combine together and override the main activity. There are instructions in the Unity tutorial for this.

    Recently I discovered that target SDK 30 and IL2CPP compilation is required to publish your app on the Google Play store.

    Hope this helps as a starting point. I wish I knew all of the above when I started on this 3 years ago :)
     
  6. unity_794A4715F03C10CF03BF

    unity_794A4715F03C10CF03BF

    Joined:
    Apr 6, 2022
    Posts:
    1
    Good But Not Enough.