RSS

Firebase Unity3D example: a walk through tutorial

16 Jan

image

Step I: Download Firebase Unity3D packages

  1. Download Firebase Unity SDK.
  2. Create a Unity3D project.
  3. Unpack the file, there will be a number of different unity packages. Double click on the packages you wish to add (i.e. import) to Unity3D projects. In this article, I am using FirebaseMessaging as an example.

    image

  4. Once the packages of interest are imported to Unity3D, you will see several folders created in your project:

    image


Step II: setup in Firebase Console

  1. Create a Firebase project in the Firebase console. If you already have an existing Google project associated with your mobile app, click Import Google Project.

    image

  2. Click Add Firebase to your Android/iOS app and follow the setup steps.

     image image

    When prompted, enter your app’s bundle ID. It’s important to enter and remember the bundle ID.

  3. If you’re importing an existing project, this may happen automatically and you can just download the config file. The file for Unity3D might be one or both of the below files:
     
        GoogleService-Info.plist (for iOS)
        Google-services.json (for Android).

  4. Add the GoogleService-Info.plist (for iOS) and/or Google-services.json (for Android) file to the project by dragging the downloaded file(s) from the Firebase console into any folder in the Unity project.


Step III: Add code to your project

  1. Create a MonoBehaviour script .

    public class GameInit : MonoBehaviour
    {
        public void Start()
        {
            Debug.Log(Unity Firebase App started);
            Firebase.Messaging.FirebaseMessaging.TokenReceived += OnTokenReceived;
            Firebase.Messaging.FirebaseMessaging.MessageReceived += OnMessageReceived;
        }

        public void OnTokenReceived(object sender, Firebase.Messaging.TokenReceivedEventArgs token)
        {
            Debug.Log(Received Registration Token: + token.Token);
        }

        public void OnMessageReceived(object sender, Firebase.Messaging.MessageReceivedEventArgs e)
        {
            Debug.Log(Received a new message from: + e.Message.From);
        }   

  2. Attach the script to the camera or which ever game object.


Step IV: Build and run the project

  1. Setup the App’s bundle ID, this should be the same when you create the Firebase project in Firebase console.

    image

  2. Build for iOS in Unity3D and attach it to the scene

    • Select the File > Build Settings menu option.
    • Select iOS in the Platform list.
    • Click Switch Platform to select iOS as the target platform.
    • Wait for the spinner (compiling) icon to stop in the bottom right corner of the Unity status bar.
    • Click Build and Run.
  3. Build for Android

    • Select the File > Build Settings menu option.
    • Select Android in the Platform list.
    • Click Switch Platform to select Android as the target platform.
    • Wait for the spinner (compiling) icon to stop in the bottom right corner of the Unity status bar.
    • Click Build and Run.

Now, everything should be fine and you can run the app on your Android and iOS devices without problems.


Step V: Testing
  1. Now go to Firebase console. Select your project.
  2. Select Notification on the left navigation bar, and then click “New Message” button

    image

  3. Send some message, and note the Unity3D app running on your device!

    image

  4. Now you should see below notification on your Android or iOS device!

image

Enjoy and happy coding!

Advertisements
 
1 Comment

Posted by on January 16, 2017 in Programming, Unity 3d

 

Tags: , , , , , , , ,

One response to “Firebase Unity3D example: a walk through tutorial

  1. afra

    February 18, 2017 at 3:03 pm

    thank you

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: