Skip to content

Commit 2946c81

Browse files
authored
feat: android support for ue 5.2 (#9)
1 parent 6cd6aba commit 2946c81

8 files changed

+58
-2
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,6 @@ DerivedDataCache/*
7878
.idea/
7979

8080
node_modules/
81+
82+
.DS_Store
83+
Content/.DS_Store

Source/Immutable/Private/Immutable/ImmutableSubsystem.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,14 @@ void UImmutableSubsystem::Initialize(FSubsystemCollectionBase& Collection)
2222
IMTBL_LOG_FUNCSIG
2323
Super::Initialize(Collection);
2424

25-
ViewportCreatedHandle = UGameViewportClient::OnViewportCreated().AddUObject(this, &UImmutableSubsystem::OnViewportCreated);
25+
#if PLATFORM_ANDROID
26+
// Enable DOM storage so we can use localStorage in the Android webview
27+
GConfig->SetBool(TEXT("/Script/AndroidRuntimeSettings.AndroidRuntimeSettings"), TEXT("bEnableDomStorage"), true, GEngineIni);
28+
29+
EngineInitCompleteHandle = FCoreDelegates::OnFEngineLoopInitComplete.AddUObject(this, &UImmutableSubsystem::OnViewportCreated);
30+
#else
31+
ViewportCreatedHandle = UGameViewportClient::OnViewportCreated().AddUObject(this, &UImmutableSubsystem::OnViewportCreated);
32+
#endif
2633
WorldTickHandle = FWorldDelegates::OnWorldTickStart.AddUObject(this, &UImmutableSubsystem::WorldTickStart);
2734
}
2835

Source/Immutable/Private/Immutable/ImtblBrowserUserWidget.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,15 @@ TSharedRef<SWidget> UImtblBrowserUserWidget::RebuildWidget()
4141
ScaleBox->AddChild(Browser);
4242
if (UCanvasPanelSlot* RootWidgetSlot = Cast<UCanvasPanelSlot>(ScaleBox->Slot))
4343
{
44+
#if PLATFORM_ANDROID
45+
// Android webview needs to be at least 1px to 1px big to work
46+
// but it can be off screen
47+
RootWidgetSlot->SetAnchors(FAnchors(0, 0, 0, 0));
48+
RootWidgetSlot->SetOffsets(FMargin(-1, -1, 1, 1));
49+
#else
4450
RootWidgetSlot->SetAnchors(FAnchors(0, 0, 1, 1));
4551
RootWidgetSlot->SetOffsets(DefaultOffsets);
52+
#endif
4653
}
4754
if (UScaleBoxSlot* ScaleBoxSlot = Cast<UScaleBoxSlot>(Browser->Slot))
4855
{
@@ -64,19 +71,25 @@ void UImtblBrowserUserWidget::BeginDestroy()
6471

6572
void UImtblBrowserUserWidget::RemoveFromParent()
6673
{
74+
#if !PLATFORM_ANDROID
6775
// This is all that is needed to persist the widget state outside throughout level/world changes.
6876
// Super::RemoveFromParent();
6977

7078
if (UPanelWidget* CurrentParent = GetParent())
7179
{
7280
CurrentParent->RemoveChild(this);
7381
}
82+
#endif
7483
}
7584

7685

7786
void UImtblBrowserUserWidget::OnWidgetRebuilt()
7887
{
88+
#if PLATFORM_ANDROID
89+
// Android webview needs to be visible to work
90+
#else
7991
SetVisibility(ESlateVisibility::Collapsed);
92+
#endif
8093
Super::OnWidgetRebuilt();
8194
}
8295

Source/Immutable/Private/Immutable/ImtblBrowserWidget.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ TSharedRef<SWidget> UImtblBrowserWidget::RebuildWidget()
146146
.ShowControls(false)
147147
.SupportsTransparency(bSupportsTransparency)
148148
.ShowInitialThrobber(bShowInitialThrobber)
149+
#if PLATFORM_ANDROID
150+
.OnLoadCompleted(BIND_UOBJECT_DELEGATE(FSimpleDelegate, HandleOnLoadCompleted))
151+
#endif
149152
#if ENGINE_MAJOR_VERSION == 5 && ENGINE_MINOR_VERSION >= 1
150153
.OnConsoleMessage(BIND_UOBJECT_DELEGATE(FOnConsoleMessageDelegate, HandleOnConsoleMessage))
151154
#endif
@@ -165,6 +168,17 @@ TSharedRef<SWidget> UImtblBrowserWidget::RebuildWidget()
165168
}
166169

167170

171+
#if PLATFORM_ANDROID
172+
void UImtblBrowserWidget::HandleOnLoadCompleted()
173+
{
174+
if (WebBrowserWidget->GetUrl() == "file://immutable/index.html")
175+
{
176+
JSConnector->SetAndroidBridgeReady();
177+
}
178+
}
179+
#endif
180+
181+
168182
void UImtblBrowserWidget::OnWidgetRebuilt()
169183
{
170184
Super::OnWidgetRebuilt();

Source/Immutable/Private/Immutable/ImtblBrowserWidget.h

+4
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ class IMMUTABLE_API UImtblBrowserWidget : public UWidget
5151
bool BindUObject(const FString& Name, UObject* Object, bool bIsPermanent = true) const;
5252
// Bind the JSConnector to the browser window
5353
void BindConnector();
54+
55+
#if PLATFORM_ANDROID
56+
void HandleOnLoadCompleted();
57+
#endif
5458

5559
#if ENGINE_MAJOR_VERSION == 5 && ENGINE_MINOR_VERSION >= 1
5660
void HandleOnConsoleMessage(const FString& Message, const FString& Source, int32 Line, EWebBrowserConsoleLogSeverity Severity);

Source/Immutable/Private/Immutable/ImtblJSConnector.cpp

+9-1
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,12 @@ void UImtblJSConnector::SendToGame(FString Message)
142142
}
143143

144144

145-
145+
#if PLATFORM_ANDROID
146+
void UImtblJSConnector::SetAndroidBridgeReady()
147+
{
148+
IMTBL_LOG_FUNCSIG
149+
bIsBridgeReady = true;
150+
OnBridgeReady.Broadcast();
151+
OnBridgeReady.Clear();
152+
}
153+
#endif

Source/Immutable/Private/Immutable/ImtblJSConnector.h

+4
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ class IMMUTABLE_API UImtblJSConnector : public UObject
5151
// Bind the func to be called for executing JS. Typically by the BrowserWidget (UE5) or Blui for UE4
5252
FOnExecuteJsDelegate ExecuteJs;
5353

54+
#if PLATFORM_ANDROID
55+
void SetAndroidBridgeReady();
56+
#endif
57+
5458
protected:
5559

5660
// Call a JavaScript function in the connected browser

Source/Immutable/Public/Immutable/ImmutableSubsystem.h

+3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ class IMMUTABLE_API UImmutableSubsystem : public UGameInstanceSubsystem
5959

6060
FDelegateHandle WorldTickHandle;
6161
FDelegateHandle ViewportCreatedHandle;
62+
#if PLATFORM_ANDROID
63+
FDelegateHandle EngineInitCompleteHandle;
64+
#endif
6265

6366
void SetupGameBridge();
6467
void OnBridgeReady();

0 commit comments

Comments
 (0)