@@ -30,8 +30,6 @@ public class VibrationComponent : MonoBehaviour
3030 [ SerializeField ]
3131 [ Tooltip ( "The type of vibration effect (SHORT: Android <= 9, LIGHT...HEAVY: Android >= 10)" ) ]
3232 protected VibrationType type = VibrationType . None ;
33-
34- #if UNITY_ANDROID
3533
3634 // -- Unity Android core instances --
3735 protected AndroidJavaClass unityPlayer ;
@@ -41,8 +39,6 @@ public class VibrationComponent : MonoBehaviour
4139 protected AndroidJavaObject vibrationPlugin ;
4240 protected AndroidJavaObject durationLongObj ;
4341
44- #endif
45-
4642 #endregion
4743
4844 #region Properties
@@ -89,27 +85,7 @@ protected virtual void Awake()
8985 [ SuppressMessage ( "ReSharper" , "StringLiteralTypo" ) ]
9086 protected virtual void Start ( )
9187 {
92- #if UNITY_ANDROID
93-
94- // Avoid: "Field currentActivity or type signature not found" exception
95- // when a device is connected over USB and using Unity Remote app
96- try
97- {
98- // PS: If you wish instantiate Kotlin Companion or Java static classes straight-forward from full package name,
99- // use the "$" special character (e.g "android.media.AudioAttributes$Builder")
100- unityPlayer = new AndroidJavaClass ( "com.unity3d.player.UnityPlayer" ) ;
101- currentActivity = unityPlayer . GetStatic < AndroidJavaObject > ( "currentActivity" ) ;
102- vibrationPlugin = new AndroidJavaObject ( "com.benoitfreslon.unity.vibrations.lib.Vibration" , currentActivity ) ;
103- }
104- catch ( Exception err )
105- {
106- if ( ! err . Message . Contains ( "Field currentActivity" ) )
107- {
108- throw err ;
109- }
110- }
111-
112- #endif
88+ InitComponents ( ) ;
11389 }
11490
11591 protected virtual void OnDisable ( )
@@ -144,13 +120,47 @@ protected virtual void OnDisable()
144120 }
145121
146122 #endregion
123+
124+ [ SuppressMessage ( "ReSharper" , "StringLiteralTypo" ) ]
125+ protected virtual void InitComponents ( )
126+ {
127+ #if UNITY_ANDROID
128+
129+ // Avoid: "Field currentActivity or type signature not found" exception
130+ // when a device is connected over USB and using Unity Remote app
131+ try
132+ {
133+ // PS: If you wish instantiate Kotlin Companion or Java static classes straight-forward from full package name,
134+ // use the "$" special character (e.g "android.media.AudioAttributes$Builder")
135+ unityPlayer = new AndroidJavaClass ( "com.unity3d.player.UnityPlayer" ) ;
136+ currentActivity = unityPlayer . GetStatic < AndroidJavaObject > ( "currentActivity" ) ;
137+ vibrationPlugin = new AndroidJavaObject ( "com.benoitfreslon.unity.vibrations.lib.Vibration" , currentActivity ) ;
138+ }
139+ catch ( Exception err )
140+ {
141+ if ( ! err . Message . Contains ( "Field currentActivity" ) )
142+ {
143+ throw ;
144+ }
145+
146+ if ( Debug . isDebugBuild )
147+ {
148+ var warnMessage = $ "[{ TAG } ] [Android] Error on load fields/components." +
149+ $ "Exception => { err . GetType ( ) . Name } : { err . Message } ";
150+
151+ Debug . LogWarning ( warnMessage ) ;
152+ }
153+ }
154+
155+ #endif
156+ }
147157
148158 protected virtual void LoadOptionsSaved ( )
149159 {
150160 if ( options == null )
151161 {
152162 /*
153- * Fallback to some Unity versions that ScriptableObjects
163+ * Fallback to some Unity versions that ScriptableObjects
154164 * passed by inspector disappears when plays.
155165 *
156166 * Or, if a developer forget to define a custom ScriptableObject
0 commit comments