@@ -20,37 +20,37 @@ namespace IO.Ably.Unity
2020 /// - No link.xml preservation directives needed for UnityEngine.Application (it's always preserved)
2121 /// - Works across all Unity scripting backends: Mono, IL2CPP (iOS/Android/WebGL/Console)
2222 /// - Gracefully degrades in non-Unity environments (returns empty/default values)
23- /// - Zero reflection overhead after first access due to Lazy<T> caching
24- /// - Safe for AOT platforms: no runtime code generation, only metadata queries
23+ /// - Zero reflection overhead after first access due to Lazy caching
24+ /// - Safe for AOT platforms: no runtime code generation, only metadata queries.
2525 /// </summary>
2626 internal static class UnityAdapter
2727 {
2828 private static readonly Lazy < Type > _unityApplication =
2929 new Lazy < Type > ( ( ) => GetUnityApplication ( ) ) ;
30-
30+
3131 private static readonly Lazy < string > _unityVersion =
3232 new Lazy < string > ( ( ) => GetUnityVersion ( ) ) ;
33-
34- private static readonly Lazy < int > _runtimePlatform =
35- new Lazy < int > ( ( ) => GetRuntimePlatform ( ) ) ;
33+
34+ private static readonly Lazy < string > _runtimePlatform =
35+ new Lazy < string > ( ( ) => GetRuntimePlatform ( ) ) ;
3636
3737 /// <summary>
3838 /// Indicates whether Unity runtime is available.
3939 /// </summary>
4040 public static bool IsAvailable => _unityApplication . Value != null ;
41-
41+
4242 /// <summary>
4343 /// Gets the cached Unity version string.
4444 /// The value is computed once on first access and cached for subsequent calls.
4545 /// </summary>
4646 public static string UnityVersion => _unityVersion . Value ;
47-
47+
4848 /// <summary>
49- /// Gets the cached runtime platform value.
49+ /// Gets the cached runtime platform string value.
5050 /// The value is computed once on first access and cached for subsequent calls.
5151 /// </summary>
52- public static int RuntimePlatform => _runtimePlatform . Value ;
53-
52+ public static string RuntimePlatform => _runtimePlatform . Value ;
53+
5454 /// <summary>
5555 /// Gets the UnityEngine.Application type using reflection.
5656 /// </summary>
@@ -60,15 +60,13 @@ private static Type GetUnityApplication()
6060 try
6161 {
6262 // Attempt to load UnityEngine.Application type at runtime
63- // This will succeed if UnityEngine.dll is available in the runtime
64- return Type . GetType (
65- "UnityEngine.Application, UnityEngine" ,
66- throwOnError : false ) ;
63+ // The UnityEngine.Application class has been located in the UnityEngine.CoreModule assembly since Unity 2017.1
64+ // Returns null if the class is not found in UnityEngine.CoreModule
65+ return Type . GetType ( "UnityEngine.Application, UnityEngine.CoreModule" , throwOnError : false ) ;
6766 }
6867 catch
6968 {
7069 // Silently fail - Unity is not available
71- // This is expected in non-Unity environments
7270 return null ;
7371 }
7472 }
@@ -88,17 +86,11 @@ private static string GetUnityVersion()
8886 try
8987 {
9088 // Get property info for unityVersion
91- var unityVersionProperty = applicationType . GetProperty (
89+ var version = applicationType . GetProperty (
9290 "unityVersion" ,
93- BindingFlags . Public | BindingFlags . Static ) ;
91+ BindingFlags . Public | BindingFlags . Static )
92+ ? . GetValue ( null ) as string ;
9493
95- if ( unityVersionProperty == null )
96- {
97- return string . Empty ;
98- }
99-
100- // Get static property value via reflection
101- var version = unityVersionProperty . GetValue ( null ) as string ;
10294 return version ?? string . Empty ;
10395 }
10496 catch
@@ -109,43 +101,33 @@ private static string GetUnityVersion()
109101 }
110102
111103 /// <summary>
112- /// Gets the runtime platform as an integer value using reflection.
104+ /// Gets the runtime platform as a string value using reflection.
113105 /// </summary>
114- /// <returns>Platform integer value (maps to RuntimePlatform enum ) or -1 if unavailable.</returns>
115- private static int GetRuntimePlatform ( )
106+ /// <returns>Platform string value (e.g., "OSXEditor", "WindowsPlayer" ) or empty string if unavailable.</returns>
107+ private static string GetRuntimePlatform ( )
116108 {
117109 var applicationType = _unityApplication . Value ;
118110 if ( applicationType == null )
119111 {
120- return - 1 ;
112+ return string . Empty ;
121113 }
122114
123115 try
124116 {
125- // Get property info for platform
126- var platformProperty = applicationType . GetProperty (
117+ // Get property info for platform and convert enum to string (e.g., "OSXEditor", "WindowsPlayer")
118+ var platform = applicationType . GetProperty (
127119 "platform" ,
128- BindingFlags . Public | BindingFlags . Static ) ;
129-
130- if ( platformProperty == null )
131- {
132- return - 1 ;
133- }
120+ BindingFlags . Public | BindingFlags . Static )
121+ ? . GetValue ( null )
122+ ? . ToString ( ) ;
134123
135- // Get static property value via reflection
136- var platformValue = platformProperty . GetValue ( null ) ;
137- if ( platformValue != null )
138- {
139- // RuntimePlatform is an enum, convert to int
140- return ( int ) platformValue ;
141- }
124+ return platform ?? string . Empty ;
142125 }
143126 catch
144127 {
145128 // Ignore reflection errors
129+ return string . Empty ;
146130 }
147-
148- return - 1 ;
149131 }
150132 }
151133}
0 commit comments