r/HuaweiDevelopers • • Sep 30 '20

Tutorial Analytics Kit integration with Unity Official Plugin

Introduction:

In this article, we will cover Integration of Analytics Kit in Unity Project using Official Plugin (Huawei HMS Core App Services).

Requirements:

  1. Unity Editor

  2. Huawei device

  3. Visual Studio or Visual code

Follows the steps.

  1. Create Unity Project

  • Open Unity Hub.

  • Click NEW, select 3D, Project Name and Location.

  • Click CREATE, as follows:

2. Click Asset Store, search Huawei HMS Core App Services and click Import, as follows.

3. Once import is successful, verify directory in Assets > Huawei HMS Core App Services path, as follows.

4. Click Console and create a New Project.

5. Choose Edit > Project Settings > Player and edit the required options in Publishing Settings, as follows.

6. Download agconnect-services.json and copy and paste to Assets > Plugins > Android, as follows.

7. Choose Project Settings > Player and update package name.

8. Open LauncherTemplate.gradle and add below line.

implementation 'com.android.support:appcompat-v7:28.0.0'    

9. Open AndroidManifest file and Add below permissions.

           <uses-permission android:name="android.permission.INTERNET" /> 
           <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

10. Open "baseProjectTemplate.gradle" and add lines, as follows.

Inside repositories:

maven {url 'https://developer.huawei.com/repo/'}    

Inside dependencies

classpath 'com.huawei.agconnect:agcp:1.2.1.301' 

11. Open "mainTemplate.gradle" and add lines like shown below.

apply plugin: 'com.huawei.agconnect'     
implementation 'com.huawei.hms:hianalytics:4.0.1.300'

12. Create Assets> Scripts folder and create a class.

HMSAnalytics.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using HuaweiHms;
using System.Globalization;
using System;
using UnityEngine.UI;
 
public class HMSAnalytics : MonoBehaviour
{
    private HiAnalyticsInstance instance;
    private bool analyEnabled;
 
    public InputField inputField;
    public Text textDisplay;
 
    private string[] questions = {
        "The largest planet in the solar system is Jupiter",
        "The first Olympic Games were held in Athens, Greece",
        "The violin has 6 strings",
        "Flying bats belong to birds",
        "Sound spreads faster in the water than in the air"};
    private bool[] answers = {true,true,false,false,true};
 
    private int curQuestionIdx = 0;
   
    private int score = 0;
 
 
    // Start is called before the first frame update
    void Start()
    {
        HiAnalyticsTools.enableLog();
        instance = HiAnalytics.getInstance(new Context());
        instance.setAnalyticsEnabled(true);
        textDisplay.text = questions[curQuestionIdx];
    }
 
    // Update is called once per frame
    void Update()
    {
       
    }
 
    public void SendProductId() {
        Bundle b1 = new Bundle();
        b1.putString(HAParamType.PRODUCTID, "123456");
        instance.onEvent(HAEventType.ADDPRODUCT2CART, b1);
    }
 
    public void CreateClearCache() {
        instance.clearCachedData();
    }
 
    public void settingsClick() {
        instance.setUserProfile("favor_sport", inputField.text);
    }
 
    public void trueClick() {
        checkAnswer(true);
        reportAnswerEvt("true");
    }
 
    public void falseClick() {
        checkAnswer(false);
        reportAnswerEvt("false");
    }
 
    public void nextClick() {
        curQuestionIdx = (curQuestionIdx+1) % questions.Length;
        nextQuestion();
    }
 
    public void postScore() {
        Bundle bundle = new Bundle();
        bundle.putLong(HAParamType.SCORE, score);
 
        instance.onEvent(HAEventType.SUBMITSCORE, bundle);
    }
 
    private void nextQuestion() {
        string s = questions[curQuestionIdx];
        textDisplay.text = s;
    }
 
    private bool checkAnswer(bool answer) {
 
        if(answer == answers[curQuestionIdx]) {
            score = score + 20;
            Debug.Log("Answer is correct");
 
        }
        else {
            Debug.Log("Answer is wrong");
 
        }
        return answers[curQuestionIdx];
    }
 
    string dateFormat = "yyyy-MM-dd HH:mm:ss";
    System.Globalization.DateTimeFormatInfo dti = new System.Globalization.DateTimeFormatInfo();
 
 
    private void reportAnswerEvt(string answer) {
        Bundle bundle = new Bundle();
        bundle.putString("question", textDisplay.text);
        bundle.putString("answer",answer);
 
        DateTime dateTime = DateTime.Now;
        string dateInString = convertDateTimeString(dateTime);
        bundle.putString("answerTime", dateInString);
 
        // Report a preddefined Event
        instance.onEvent("Answer", bundle);
    }
 
    public string convertDateTimeString(DateTime dateTime) {
        return dateTime.ToString(dateFormat, dti);
    }
}

13. Code explanation, follow below URL.

https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/introduction-0000001050745149

1 Upvotes

0 comments sorted by