Nach ein paar Tests mit dem kostenlosen Service GameAnalytics kam folgende Erkenntnis: das Plugin aus dem AssetStore ist zwar ganz nett, aber kaum hilfreich. In den GameAnalytics Docs stieß ich zwar auf einen wesentlich kürzeren und übersichtlicheren Ansatz, allerdings produzierte dieser Hickups. Dort wurde die WebRequest-Klasse verwendet. Damit kann man zwar mit etwas Bastelarbeit asynchron arbeiten, aber trotzdem gibt es Hickups unter Unity.
Also zurück zum guten alten Coroutine + WWW-Klasse.
Hier gibt es ein Snippet, mit dem sich der Service nutzen lässt, ohne in der Anwendung Performance-Probleme zu erzeugen.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
using Newtonsoft.Json; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Net.NetworkInformation; using System.Security.Cryptography; using System.Text; using UnityEngine; public class GameAnalyticsClient : MonoBehaviour { public string ApiUrl; public string GameKey; public string SecretKey; private static string _userId; public void Send(CustomLogType eventCategory, string eventId, float? value) { if (string.IsNullOrEmpty(_userId)) { //generate unique user id var networkInterfaces = NetworkInterface.GetAllNetworkInterfaces(); foreach (var ni in networkInterfaces) { var address = ni.GetPhysicalAddress(); if (string.IsNullOrEmpty(address.ToString())) continue; var bytes = Encoding.UTF8.GetBytes(address.ToString()); var sha = new SHA1CryptoServiceProvider(); _userId = BitConverter.ToString(sha.ComputeHash(bytes)).Replace("-", ""); } } var dict = new Dictionary<string, string> { {"event_id", eventId}, {"user_id", _userId}, {"session_id", "session_1"}, {"build", Game.Settings.VersionControl.Current}, {"area", Application.loadedLevelName} }; Instance.Send(eventCategory, dict); } private void Send(CustomLogType eventCategory, Dictionary<string, string> dict) { var messageString = JsonConvert.SerializeObject(dict); var url = ApiUrl + "/" + GameKey + "/" + eventCategory.ToLower(); var headers = new Dictionary<string, string>(); var dataByte = Encoding.UTF8.GetBytes(messageString); headers.Add("Authorization", GetHexDigits(messageString + SecretKey)); var www = new WWW(url, dataByte, headers); StartCoroutine(WaitForRequest(www)); } private IEnumerator WaitForRequest(WWW www) { yield return www; Debug.Log("GameAnalytics: " + (www.error ?? www.text)); } private string GetHexDigits(string s) { // always use hexadecimal digits MD5 md5 = new MD5CryptoServiceProvider(); var authData = Encoding.Default.GetBytes(s); var authHash = md5.ComputeHash(authData); // Transforms as hexa return authHash.Aggregate(String.Empty, (current, b) => current + String.Format("{0:x2}", b)); } } |