Unity 对接友盟与TalkingData数据平台(简易版)

来源:互联网 发布:网络专题排版设计 编辑:程序博客网 时间:2024/04/30 07:09

关注公众号,获取更多干货。

二维码

下面是正文:


闲来无事,写一写游戏开发中用户数据的记录吧。

相信每个人做完的模块都想知道这一部分产生的数据是怎样的,但是苦于在个人开发的时候并没有游戏后台的支撑,自己打点记录游戏数据又太麻烦。

还有一些再开发中想要查看用户的数据记录等等等等可能会遇到的问题我就不多做介绍了。

下面我来介绍一下如何对接友盟与TalkingData。

这篇文章我就不截图了,东西很简单。

首先就是去两个平台注册,登录。
友盟选择U-Game基础版,TalkingData选择GameAnalytics

然后下载对应的SDK,按着接口文档先试着接其中一个,把例子跑通。

那么问题来了,我写这个文章到底是要干嘛呢。

额,,,,,,就是总结一下,把两个SDK结合到一起。
毕竟有时候平台的数据统计不想自己写的服务器那么靠谱,当然要多接几个平台将数据放到一起对比。
那么这时就需要写一个接口,然后整合SDK方便以后拓展。

那么,就上代码了。

在两个包同时放到一个项目里的时候,会有一个文件有冲突
AndroidManifest.xml
经常做Android平台开发的朋友应该知道这个东西应该如何处理。
因为我没做过传统的Android开发,所以我对这个东西的理解就相当于Web开发时候的部署描述符web.xml

那么我就合并了一下这个文件,结果还真成了。

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionName="1.0" android:versionCode="1" package="" android:installLocation="preferExternal">  <supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true" />  <application android:icon="@drawable/app_icon" android:label="@string/app_name" >    <activity android:name="com.unity3d.player.UnityPlayerActivity"              android:label="@string/app_name"              android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">      <intent-filter>        <action android:name="android.intent.action.MAIN" />        <category android:name="android.intent.category.LAUNCHER" />      </intent-filter>      <meta-data android:name="android.app.lib_name" android:value="unity"/>      <meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="false"/>    </activity>  </application>  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  <uses-permission android:name="android.permission.INTERNET"/>  <uses-permission android:name="android.permission.READ_PHONE_STATE" />  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>  <uses-permission android:name="android.permission.GET_TASKS"/></manifest>

OK。解决完这个文件,我们就可以放肆的去写代码了。

我这里写了几个简单的接口,如果有别的需要,可以尽情的拓展。
如有建议,尽情打扰。

在编辑器中新建一个GameObject,然后写一个DontDestroyOnLoad,保证这个物体不会因为场景的加载而消失。

下面开始上代码

using System.Collections;using System.Collections.Generic;using Umeng;using UnityEngine;public class Aries_GameDataStatisticalAnalysis : MonoBehaviour{    public static Aries_GameDataStatisticalAnalysis _Instince = null;    /// <summary>    /// 友盟Appkey    /// </summary>    public static string UmengAppKey = "xxx";    /// <summary>    /// TalkingData AppKey    /// </summary>    public static string TalkingDataKey = "xxx";    /// <summary>    /// 渠道    /// </summary>    public static string[] PlaceName =new string[] { "xxx" , "xxx"};    /// <summary>    /// 当前选择渠道    /// </summary>    public static int PlaceIndex = 0;    void Awake()    {        if (_Instince == null)        {            DontDestroyOnLoad(gameObject);            _Instince = this;        }        else if (_Instince != this)        {            Destroy(gameObject);        }    }    void Start()    {        Init();    }    /// <summary>    /// 初始化友盟 、 TalkingData    /// </summary>    void Init()    {        GA.StartWithAppKeyAndChannelId(UmengAppKey, PlaceName[PlaceIndex]);        TalkingDataGA.OnStart(TalkingDataKey, PlaceName[PlaceIndex]);    }    /// <summary>    /// 登录    /// </summary>    /// <param name="userId">玩家账号ID</param>    public void Login(string userId)    {        Debug.Log("Login ======> " + userId);        GA.ProfileSignIn(userId);        TDGAAccount account = TDGAAccount.SetAccount(userId);    }    /// <summary>    /// 获得奖励    /// </summary>    /// <param name="item">道具ID 非空字符串</param>    /// <param name="amount">道具数量   大于0的整数</param>    /// <param name="source">奖励渠道   枚举类型</param>    public void GetRewards(string item, int amount, GA.BonusSource source)    {        Debug.Log("获得奖励 ======> " + item + ":" + amount + ":" + source.ToString());        GA.Bonus(item, amount, 0, source);        Dictionary<string, object> dic = new Dictionary<string, object>();        dic.Add("道具ID", item);        dic.Add("道具数量", amount);        dic.Add("奖励渠道", source.ToString());        TalkingDataGA.OnEvent("获得奖励", dic);    }    /// <summary>    /// 进入关卡    /// </summary>    /// <param name="level">关卡id    非空字符串</param>    public void StartLevel(string level)    {        Debug.Log("进入关卡 ======> " + level);        GA.StartLevel(level);        TDGAMission.OnBegin(level);    }    /// <summary>    /// 通过关卡    /// </summary>    /// <param name="level">关卡id    非空字符串</param>    public void FinishLevel(string level)    {        Debug.Log("通过关卡 ======> " + level);        GA.FinishLevel(level);        TDGAMission.OnCompleted(level);    }    /// <summary>    /// 未通过关卡    /// </summary>    /// <param name="level">关卡id    非空字符串</param>    /// <param name="cause">失败原因    最多支持 16个字符</param>    public void FailLevel(string level, string cause)    {        Debug.Log("未通过关卡 ======> " + level);        GA.FailLevel(level);        TDGAMission.OnFailed(level, cause);    }    /// <summary>    /// 购买道具    /// </summary>    /// <param name="item">道具ID 非空字符串</param>    /// <param name="amount">道具数量   大于0的整数</param>    /// <param name="price">道具单价    >=0</param>    public void Buy(string item, int amount, double price)    {        Debug.Log("购买道具 ======> " + item + ":" + amount + ":" + price);        GA.Buy(item, amount, price);        TDGAItem.OnPurchase(item, amount, price);    }    /// <summary>    /// 使用道具    /// </summary>    /// <param name="item">道具ID 非空字符串</param>    /// <param name="amount">道具数量   大于0的整数</param>    /// <param name="price">道具单价    >=0</param>    public void Use(string item, int amount, double price)    {        Debug.Log("使用道具 ======> " + item + ":" + amount + ":" + price);        GA.Use(item, amount, price);        TDGAItem.OnUse(item, amount);    }    /// <summary>    /// 访问页面(这个接口 TalkingData有数据,友盟没收到,可能要去平台注册事件,大家可以尽情尝试)    /// </summary>    /// <param name="pageName">页面名称</param>    public void pageBegin(string pageName)    {        Debug.Log("访问页面 ======> " + pageName );        var dict = new Dictionary<string, string>();        dict["页面名称"] = pageName;        GA.Event("访问页面", dict);        Dictionary<string, object> dic = new Dictionary<string, object>();        dic.Add("页面名称", pageName);        TalkingDataGA.OnEvent("访问页面", dic);    }}

这样就大功告成了,需要扩展的扩展,需要统计的时候直接这样调用就可以了
例如:

  Aries_GameDataStatisticalAnalysis._Instince.Login(PlayerData.GetInstance().playerID);

好了,这个文章就先写到这里,后期遇到问题再来这里补充。

本文永久链接:http://blog.csdn.net/Aries_H/article/details/78282868
转载请注明出处。谢谢。

阅读全文
0 0
原创粉丝点击