- Home /
Question by
Aubrey-Falconer · Jan 23, 2015 at 07:34 PM ·
genericsunity analyticsc# to javascript
(analytics beta) How to initialize Generic Dictionary from JS?
Here's the sample c# code provided by Unity for the registration of a custom Analytics event:
using System.Collections.Generic;
using UnityEngine.Cloud.Analytics;
int totalPotions = 5;
int totalCoins = 100;
UnityAnalytics.CustomEvent("gameOver", new Dictionary<string, object>
{
{ "potions", totalPotions },
{ "coins", totalCoins }
});
I have attempted to convert this to JS, but met with limited success:
import System.Collections.Generic;
import UnityEngine.Cloud.Analytics;
UnityAnalytics.CustomEvent("gameOver", new System.Collections.Generic.Dictionary.<System.String, System.Object> ({
"name": screenName,
"coins", totalCoins
}));
Above code results in the following error:
Assets/Worlds/_Index/Scripts/IndexContentScreens.js(110,96): BCE0004: Ambiguous reference 'constructor': System.Collections.Generic.Dictionary.<String, Object>.constructor(System.Collections.Generic.IDictionary.<String, Object>), System.Collections.Generic.Dictionary.<String, Object>.constructor(System.Collections.Generic.IEqualityComparer.<String>).
Anyone care to point me in the right direction?
Comment
Not sure whether that dictionary initialization is supported in Unityscript. You can declare a variable for the dictionary, initialize it as empty, and fill it with values using the Add(key, value) method.
Answer by kentunity · Mar 21, 2015 at 04:17 PM
@Aubrey is right, You can do something like this in Javascript:
var params = new System.Collections.Generic.Dictionary.<System.String, System.Object>();
params.Add("coins", 2);
params.Add("armor", "Shield");
UnityAnalytics.CustomEvent("MyCustomEvent",params);