1+ #if MLA_UNITY_ANALYTICS_MODULE || ! UNITY_2019_4_OR_NEWER
2+ #define MLA_UNITY_ANALYTICS_MODULE_ENABLED
3+ #endif
4+
15using System ;
26using System . Collections . Generic ;
7+ using System . Diagnostics ;
38using Unity . MLAgents . Actuators ;
49using Unity . MLAgents . Sensors ;
510using UnityEngine ;
11+ #if MLA_UNITY_ANALYTICS_MODULE_ENABLED
612using UnityEngine . Analytics ;
13+ #if UNITY_EDITOR
14+ using UnityEditor . Analytics ;
15+ #endif
16+ #endif
717
818#if UNITY_EDITOR
919using UnityEditor ;
10- using UnityEditor . Analytics ;
1120#endif
1221
1322namespace Unity . MLAgents . Analytics
@@ -56,22 +65,22 @@ internal class TrainingAnalytics
5665
5766 static bool EnableAnalytics ( )
5867 {
68+ #if MLA_UNITY_ANALYTICS_MODULE_ENABLED
5969 if ( s_EventsRegistered )
6070 {
6171 return true ;
6272 }
63-
6473 foreach ( var eventName in s_EventNames )
6574 {
6675#if UNITY_EDITOR
6776 AnalyticsResult result = EditorAnalytics . RegisterEventWithLimit ( eventName , k_MaxEventsPerHour , k_MaxNumberOfElements , k_VendorKey ) ;
68- #else
69- AnalyticsResult result = AnalyticsResult . UnsupportedPlatform ;
70- #endif
7177 if ( result != AnalyticsResult . Ok )
7278 {
7379 return false ;
7480 }
81+ #else
82+ return false ;
83+ #endif // UNITY_EDITOR
7584 }
7685 s_EventsRegistered = true ;
7786
@@ -83,13 +92,17 @@ static bool EnableAnalytics()
8392 }
8493
8594 return s_EventsRegistered ;
95+ #else
96+ return false ;
97+ #endif // MLA_UNITY_ANALYTICS_MODULE_ENABLED
8698 }
8799
88100 /// <summary>
89101 /// Cache information about the trainer when it becomes available in the RpcCommunicator.
90102 /// </summary>
91103 /// <param name="communicationVersion"></param>
92104 /// <param name="packageVersion"></param>
105+ [ Conditional ( "MLA_UNITY_ANALYTICS_MODULE_ENABLED" ) ]
93106 public static void SetTrainerInformation ( string packageVersion , string communicationVersion )
94107 {
95108 s_TrainerPackageVersion = packageVersion ;
@@ -98,13 +111,14 @@ public static void SetTrainerInformation(string packageVersion, string communica
98111
99112 public static bool IsAnalyticsEnabled ( )
100113 {
101- #if UNITY_EDITOR
114+ #if UNITY_EDITOR && MLA_UNITY_ANALYTICS_MODULE_ENABLED
102115 return EditorAnalytics . enabled ;
103116#else
104117 return false ;
105118#endif
106119 }
107120
121+ [ Conditional ( "MLA_UNITY_ANALYTICS_MODULE_ENABLED" ) ]
108122 public static void TrainingEnvironmentInitialized ( TrainingEnvironmentInitializedEvent tbiEvent )
109123 {
110124 if ( ! IsAnalyticsEnabled ( ) )
@@ -126,16 +140,15 @@ public static void TrainingEnvironmentInitialized(TrainingEnvironmentInitialized
126140 // Debug.Log(
127141 // $"Would send event {k_TrainingEnvironmentInitializedEventName} with body {JsonUtility.ToJson(tbiEvent, true)}"
128142 // );
129- #if UNITY_EDITOR
143+ #if UNITY_EDITOR && MLA_UNITY_ANALYTICS_MODULE_ENABLED
130144 if ( AnalyticsUtils . s_SendEditorAnalytics )
131145 {
132146 EditorAnalytics . SendEventWithLimit ( k_TrainingEnvironmentInitializedEventName , tbiEvent ) ;
133147 }
134- #else
135- return ;
136148#endif
137149 }
138150
151+ [ Conditional ( "MLA_UNITY_ANALYTICS_MODULE_ENABLED" ) ]
139152 public static void RemotePolicyInitialized (
140153 string fullyQualifiedBehaviorName ,
141154 IList < ISensor > sensors ,
@@ -164,13 +177,11 @@ IList<IActuator> actuators
164177 // Debug.Log(
165178 // $"Would send event {k_RemotePolicyInitializedEventName} with body {JsonUtility.ToJson(data, true)}"
166179 // );
167- #if UNITY_EDITOR
180+ #if UNITY_EDITOR && MLA_UNITY_ANALYTICS_MODULE_ENABLED
168181 if ( AnalyticsUtils . s_SendEditorAnalytics )
169182 {
170183 EditorAnalytics . SendEventWithLimit ( k_RemotePolicyInitializedEventName , data ) ;
171184 }
172- #else
173- return ;
174185#endif
175186 }
176187
@@ -186,6 +197,7 @@ internal static string ParseBehaviorName(string fullyQualifiedBehaviorName)
186197 return fullyQualifiedBehaviorName . Substring ( 0 , lastQuestionIndex ) ;
187198 }
188199
200+ [ Conditional ( "MLA_UNITY_ANALYTICS_MODULE_ENABLED" ) ]
189201 public static void TrainingBehaviorInitialized ( TrainingBehaviorInitializedEvent tbiEvent )
190202 {
191203 if ( ! IsAnalyticsEnabled ( ) )
@@ -211,7 +223,7 @@ public static void TrainingBehaviorInitialized(TrainingBehaviorInitializedEvent
211223 // Debug.Log(
212224 // $"Would send event {k_TrainingBehaviorInitializedEventName} with body {JsonUtility.ToJson(tbiEvent, true)}"
213225 // );
214- #if UNITY_EDITOR
226+ #if UNITY_EDITOR && MLA_UNITY_ANALYTICS_MODULE_ENABLED
215227 if ( AnalyticsUtils . s_SendEditorAnalytics )
216228 {
217229 EditorAnalytics . SendEventWithLimit ( k_TrainingBehaviorInitializedEventName , tbiEvent ) ;
0 commit comments