Fandom

Adventure Creator Wikia

Unity analytics Integration (AC Action)

42pages on
this wiki
Add New Page
Comments0 Share

Note: This is an AC action made to allow sending events. Script Author's quote:

Hi! We wanted to add Unity Analytics for our game to see which puzzles are easy and which ones are hard. So I wrote up a little custom Action to do this. I can't guarantee that it's all correct code but this version should work and I also added the possibility to use AC Variables as parameters to send to Unity Analytics. Check it out
To use it replace the text in a .cs file named ActionAnalyticsSendEvent with this text, or create a new blank text file, then paste the text and rename the file ActionAnalyticsSendEvent.cs. Then drop the file into the folder named actions inside the Adventure creator folder. Then, just use the action in an action list.

This is the code:

/*
 *
  *	Adventure Creator Action: Unity Analytics
 *	by Jens Bahr, 2016
 *	
  *	"ActionAnalyticsSendEvent.cs"
 * 
  *	This action sends custom data to Unity Analytics.
 * 
  */
 
 using UnityEngine;
 using UnityEngine.Analytics;
 using System.Collections;
 using System.Collections.Generic;
 
 #if UNITY_EDITOR
using UnityEditor;
#endif
 
 namespace AC
 {
 
 	[System.Serializable]
 	public class ActionAnalyticsSendEvent : Action
 	{
 
 		public int parameterID = -1;
 		public string analyticsID;
 		
 		// enum for data types of parameters
 		public enum AnalyticsParameterType { String, Int, Float, Bool, Variable };
 		//public enum ParameterType { GlobalVariable, LocalVariable };
 		
 		// number of parameters for this event
 		public int numParameters;
 		
 		// parameters are stored in lists of length numParameters
 		public List<string> parameterNames = new List<string>();
 		public List<AnalyticsParameterType> parameterTypes = new List<AnalyticsParameterType>();
 		public List<int> 	parameterIntegers = new List<int>(); 	// 0 
 		public List<float> 	parameterFloats = new List<float>(); 	// 1
 		public List<string> parameterStrings = new List<string>(); 	// 2
 		public List<bool> parameterBools = new List<bool>(); 	// 2
 		
 		public List<VariableLocation> parameterVarLocation = new List<VariableLocation>();
 		public List<int> parameterVarID = new List<int>();
 		public List<int> parameterVarParamID = new List<int>();
 		public List<VariableType> parameterVarType = new List<VariableType>();
 		
 		
 		public ActionAnalyticsSendEvent ()
 		{
 			this.isDisplayed = true;
 			category = ActionCategory.Custom;
 			title = "Unity Analytics";
 			description = "Sends an event to Unity Analytics.";
 		}
 
 
 		override public void AssignValues (List<ActionParameter> parameters)
 		{
 			// is this correct?
 		}
 
 		
 		override public float Run ()
 		{
 			// prepare data
 			Dictionary<string, object> sendDict = new Dictionary<string, object>();
 			for(int i = 0; i < numParameters; i++) {
 				if(parameterTypes[i] == AnalyticsParameterType.Int) {
 					sendDict.Add(parameterNames[i], parameterIntegers[i]);
 				} else if(parameterTypes[i] == AnalyticsParameterType.String) {
 					sendDict.Add(parameterNames[i], parameterStrings[i]);
 				} else if(parameterTypes[i] == AnalyticsParameterType.Float){
 					sendDict.Add(parameterNames[i], parameterFloats[i]);
 				} else if(parameterTypes[i] == AnalyticsParameterType.Bool) {
 					sendDict.Add(parameterNames[i], parameterBools[i]);
 				} else if(parameterTypes[i] == AnalyticsParameterType.Variable) {
 					
 					GVar variab;
 					if (parameterVarLocation[i] == VariableLocation.Global)	{
 						variab = GlobalVariables.GetVariable (parameterVarID[i]);
 					} else {
 						variab = LocalVariables.GetVariable (parameterVarID[i]);
 					}
 					
 					if(variab != null) {
 						switch(parameterVarType[i]) {
 							case VariableType.Integer:
 							case VariableType.Boolean:
 								sendDict.Add(parameterNames[i], variab.val);
 								break;
 							case VariableType.String:
 								sendDict.Add(parameterNames[i], variab.textVal);
 								break;
 								
 							case VariableType.Float:
 								sendDict.Add(parameterNames[i], variab.floatVal);
 								break;
 						}						
 					}
 				}
 			}
 		  	
 			// send the data to unity analytics
 			Analytics.CustomEvent(analyticsID, sendDict);
   
 			if (!isRunning)
 			{
 				isRunning = true;
 				return 0f;
 			}
 			else
 			{
 				isRunning = false;
 				return 0f;
 			}
 		}
 
 
 		#if UNITY_EDITOR
 
 		override public void ShowGUI (List<ActionParameter> parameters)
 		{
 			EditorGUILayout.BeginHorizontal ();
 			EditorGUILayout.LabelField ("Event:", GUILayout.Width (65f));
 			EditorStyles.textField.wordWrap = true;
 			analyticsID = EditorGUILayout.TextArea (analyticsID, GUILayout.MaxWidth (400f));
 			EditorGUILayout.EndHorizontal ();
 			
 			
 			EditorGUILayout.HelpBox ("Set the number of parameters you want to set here.", MessageType.Info);
 			numParameters = EditorGUILayout.IntField ("Num Parameters:", numParameters);
 			
 			// display all the parameters we want to set
 			for(int i = 0; i < numParameters; i++) {
 				// if we don't have enough list elements, add a new one
 				if(i >= parameterNames.Count) 	parameterNames.Add("newParam");
 				if(i >= parameterTypes.Count) parameterTypes.Add(AnalyticsParameterType.Int);
 				if(i >= parameterIntegers.Count) parameterIntegers.Add(0);
 				if(i >= parameterFloats.Count) parameterFloats.Add(0f); 	
 				if(i >= parameterStrings.Count) parameterStrings.Add(""); 	
 				if(i >= parameterBools.Count) parameterBools.Add(false); 	
 				if(i >= parameterVarLocation.Count) parameterVarLocation.Add(VariableLocation.Global);
 				if(i >= parameterVarID.Count) parameterVarID.Add(0);
 				if(i >= parameterVarParamID.Count) parameterVarParamID.Add(0);
 				if(i >= parameterVarType.Count) parameterVarType.Add(VariableType.Integer);
 				
 				EditorGUILayout.HelpBox ("Parameter " + (i+1) + " data", MessageType.Info);
 				
 				EditorGUILayout.BeginHorizontal ();				
 				EditorGUILayout.LabelField ("Name:", GUILayout.Width (65f));
 				EditorStyles.textField.wordWrap = true;
 				parameterNames[i] = EditorGUILayout.TextArea (parameterNames[i], GUILayout.MaxWidth (400f));
 				EditorGUILayout.EndHorizontal ();
 				
 				// set parameter type
 				parameterTypes[i] = (AnalyticsParameterType) EditorGUILayout.EnumPopup ("Source:", parameterTypes[i]);
 				
 				// value is set dependent of type
 				if(parameterTypes[i] == AnalyticsParameterType.Int) {
 					parameterIntegers[i] = EditorGUILayout.IntField ("Value:", parameterIntegers[i]);
 					
 				} else if(parameterTypes[i] == AnalyticsParameterType.Float) {
 					parameterFloats[i] = EditorGUILayout.FloatField ("Value:", parameterFloats[i]);
 					
 				} else if(parameterTypes[i] == AnalyticsParameterType.Bool) {
 					parameterBools[i] = EditorGUILayout.Toggle ("Value:", parameterBools[i]);
 					
 				} else if(parameterTypes[i] == AnalyticsParameterType.String){
 					EditorGUILayout.BeginHorizontal ();				
 					EditorGUILayout.LabelField ("Value:", GUILayout.Width (65f));
 					EditorStyles.textField.wordWrap = true;
 					parameterStrings[i] = EditorGUILayout.TextArea (parameterStrings[i], GUILayout.MaxWidth (400f));
 					EditorGUILayout.EndHorizontal ();
 					
 				} else {
 					if (isAssetFile) {
 						EditorGUILayout.HelpBox ("Asset files can only use global vars", MessageType.Info);
 						parameterVarLocation[i] = VariableLocation.Global;
 					} else {
 						parameterVarLocation[i] = (VariableLocation) EditorGUILayout.EnumPopup ("Source:", parameterVarLocation[i]);
 					}
 					
 					if (parameterVarLocation[i] == VariableLocation.Global)	{
 						if (AdvGame.GetReferences ().variablesManager) {
 							parameterVarID[i] = ShowVarGUI (AdvGame.GetReferences ().variablesManager.vars, parameters, ParameterType.GlobalVariable, parameterVarID[i], parameterVarParamID[i], i);
 						}
 					} else if (parameterVarLocation[i] == VariableLocation.Local)	{
 						if (KickStarter.localVariables)	{
 							parameterVarID[i] = ShowVarGUI (KickStarter.localVariables.localVars, parameters, ParameterType.LocalVariable, parameterVarID[i], parameterVarParamID[i], i);
 						}
 					}
 					
 					
 				}
 				
 				EditorGUILayout.Space ();
 			}
 						
 			AfterRunningOption ();
 		}
 		
 		// this was more or less copied from adventure creator core
 		private int ShowVarGUI (List<GVar> vars, List<ActionParameter> parameters, ParameterType parameterType, int variableID, int parameterID, int listID)
 		{
 			// Create a string List of the field's names (for the PopUp box)
 			List<string> labelList = new List<string>();
 			
 			int i = 0;
 			int variableNumber = -1;
 			
 			if (vars.Count > 0)
 			{
 				foreach (GVar _var in vars)
 				{
 					labelList.Add (_var.label);
 					
 					// If a GlobalVar variable has been removed, make sure selected variable is still valid
 					if (_var.id == variableID)
 					{
 						variableNumber = i;
 					}
 					
 					i ++;
 				}
 				
 				if (variableNumber == -1 && (parameters == null || parameters.Count == 0 || parameterID == -1))
 				{
 					// Wasn't found (variable was deleted?), so revert to zero
 					ACDebug.LogWarning ("Previously chosen variable no longer exists!");
 					variableNumber = 0;
 					variableID = 0;
 				}
 
 				string label = "Variable:";
 
 				parameterID = Action.ChooseParameterGUI (label, parameters, parameterID, parameterType);
 				if (parameterID >= 0)
 				{
 					//variableNumber = 0;
 					variableNumber = Mathf.Min (variableNumber, vars.Count-1);
 					variableID = -1;
 				}
 				else
 				{
 					variableNumber = EditorGUILayout.Popup (label, variableNumber, labelList.ToArray());
 					variableID = vars [variableNumber].id;
 				}
 			}
 			else
 			{
 				EditorGUILayout.HelpBox ("No variables exist!", MessageType.Info);
 				variableID = -1;
 				variableNumber = -1;
 			}
 
 			
 			parameterVarID[listID] = parameterID;
 
 			if (variableNumber >= 0)
 			{
 				parameterVarType[listID] = vars[variableNumber].type;
 			}
 
 			return variableID;
 		}
 		
 
 		public override string SetLabel ()
 		{
 			string labelAdd = " (" + analyticsID + ")";
 			return labelAdd;
 		}
 
 		#endif
 		
 	}
 
 }

-Provided by user jens108 -

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.