Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fix] Fixed Windows Standalone builds #if UNITY_EDITOR errors scripts…
Browse files Browse the repository at this point in the history
… bytes differences
mfdeveloper committed Oct 30, 2023
1 parent c8a0295 commit 8ff3ad2
Showing 3 changed files with 38 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -127,7 +127,7 @@ public static void Vibrate()

Vibrate(0, typeValue: (int) VibrationType.Normal);

#else
#elif !UNITY_STANDALONE

if (Application.isConsolePlatform)
{
Original file line number Diff line number Diff line change
@@ -30,8 +30,6 @@ public class VibrationComponent : MonoBehaviour
[SerializeField]
[Tooltip("The type of vibration effect (SHORT: Android <= 9, LIGHT...HEAVY: Android >= 10)")]
protected VibrationType type = VibrationType.None;

#if UNITY_ANDROID

// -- Unity Android core instances --
protected AndroidJavaClass unityPlayer;
@@ -41,8 +39,6 @@ public class VibrationComponent : MonoBehaviour
protected AndroidJavaObject vibrationPlugin;
protected AndroidJavaObject durationLongObj;

#endif

#endregion

#region Properties
@@ -89,27 +85,7 @@ protected virtual void Awake()
[SuppressMessage("ReSharper", "StringLiteralTypo")]
protected virtual void Start()
{
#if UNITY_ANDROID

// Avoid: "Field currentActivity or type signature not found" exception
// when a device is connected over USB and using Unity Remote app
try
{
// PS: If you wish instantiate Kotlin Companion or Java static classes straight-forward from full package name,
// use the "$" special character (e.g "android.media.AudioAttributes$Builder")
unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
vibrationPlugin = new AndroidJavaObject("com.benoitfreslon.unity.vibrations.lib.Vibration", currentActivity);
}
catch (Exception err)
{
if (!err.Message.Contains("Field currentActivity"))
{
throw err;
}
}

#endif
InitComponents();
}

protected virtual void OnDisable()
@@ -144,13 +120,47 @@ protected virtual void OnDisable()
}

#endregion

[SuppressMessage("ReSharper", "StringLiteralTypo")]
protected virtual void InitComponents()
{
#if UNITY_ANDROID

// Avoid: "Field currentActivity or type signature not found" exception
// when a device is connected over USB and using Unity Remote app
try
{
// PS: If you wish instantiate Kotlin Companion or Java static classes straight-forward from full package name,
// use the "$" special character (e.g "android.media.AudioAttributes$Builder")
unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
vibrationPlugin = new AndroidJavaObject("com.benoitfreslon.unity.vibrations.lib.Vibration", currentActivity);
}
catch (Exception err)
{
if (!err.Message.Contains("Field currentActivity"))
{
throw;
}

if (Debug.isDebugBuild)
{
var warnMessage = $"[{TAG}] [Android] Error on load fields/components." +
$"Exception => {err.GetType().Name} : {err.Message}";

Debug.LogWarning(warnMessage);
}
}

#endif
}

protected virtual void LoadOptionsSaved()
{
if (options == null)
{
/*
* Fallback to some Unity versions that ScriptableObjects
* Fallback to some Unity versions that ScriptableObjects
* passed by inspector disappears when plays.
*
* Or, if a developer forget to define a custom ScriptableObject
Original file line number Diff line number Diff line change
@@ -2,12 +2,7 @@
"name": "Vibration",
"rootNamespace": "VibrationPlugin",
"references": [],
"includePlatforms": [
"Android",
"Editor",
"iOS",
"WebGL"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,

0 comments on commit 8ff3ad2

Please sign in to comment.