Skip to content

Commit

Permalink
Add example of returning string for unity-to-react communication
Browse files Browse the repository at this point in the history
  • Loading branch information
jozzhart committed Jan 10, 2023
1 parent 2bfa083 commit 557163d
Show file tree
Hide file tree
Showing 7 changed files with 238 additions and 2 deletions.
7 changes: 7 additions & 0 deletions testing/project/Assets/Controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Controller : MonoBehaviour {
[DllImport ("__Internal")] static extern void ClickedNumbersTestButton (int[] values, int length);
[DllImport ("__Internal")] static extern void ClickedBoolTestButton (bool value);
[DllImport ("__Internal")] static extern void ClickedObjectTestButton (string stringValue, int intValue, bool boolValue);
[DllImport ("__Internal")] static extern string ClickedTestButtonReturnString ();

public Text textLog;

Expand Down Expand Up @@ -49,6 +50,12 @@ public void DispatchObjectEvent () {
#endif
}

public void DispatchEventReturnString () {
#if !UNITY_EDITOR && UNITY_WEBGL
SetLogText(ClickedTestButtonReturnString());
#endif
}

void SetLogText (string text) {
textLog.text = text;
}
Expand Down
215 changes: 215 additions & 0 deletions testing/project/Assets/Main.unity
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,7 @@ RectTransform:
- {fileID: 938036806}
- {fileID: 1191530762}
- {fileID: 1968377178}
- {fileID: 1774420044}
m_Father: {fileID: 2053015781}
m_RootOrder: -1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
Expand Down Expand Up @@ -1336,6 +1337,220 @@ CanvasRenderer:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1561554039}
m_CullTransparentMesh: 0
--- !u!1 &1774420043
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1774420044}
- component: {fileID: 1774420047}
- component: {fileID: 1774420046}
- component: {fileID: 1774420045}
m_Layer: 5
m_Name: DispatchEventButton
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &1774420044
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1774420043}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children:
- {fileID: 1925683366}
m_Father: {fileID: 1061584829}
m_RootOrder: -1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 40}
m_Pivot: {x: 0, y: 1}
--- !u!114 &1774420045
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1774420043}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 4e29b1a8efbd4b44bb3f3716e73f07ff, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Navigation:
m_Mode: 3
m_WrapAround: 0
m_SelectOnUp: {fileID: 0}
m_SelectOnDown: {fileID: 0}
m_SelectOnLeft: {fileID: 0}
m_SelectOnRight: {fileID: 0}
m_Transition: 1
m_Colors:
m_NormalColor: {r: 1, g: 1, b: 1, a: 1}
m_HighlightedColor: {r: 0.9607843, g: 0.9607843, b: 0.9607843, a: 1}
m_PressedColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 1}
m_SelectedColor: {r: 0.9607843, g: 0.9607843, b: 0.9607843, a: 1}
m_DisabledColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 0.5019608}
m_ColorMultiplier: 1
m_FadeDuration: 0.1
m_SpriteState:
m_HighlightedSprite: {fileID: 0}
m_PressedSprite: {fileID: 0}
m_SelectedSprite: {fileID: 0}
m_DisabledSprite: {fileID: 0}
m_AnimationTriggers:
m_NormalTrigger: Normal
m_HighlightedTrigger: Highlighted
m_PressedTrigger: Pressed
m_SelectedTrigger: Highlighted
m_DisabledTrigger: Disabled
m_Interactable: 1
m_TargetGraphic: {fileID: 1774420046}
m_OnClick:
m_PersistentCalls:
m_Calls:
- m_Target: {fileID: 84545479}
m_TargetAssemblyTypeName: Controller, Assembly-CSharp
m_MethodName: DispatchEventReturnString
m_Mode: 1
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
--- !u!114 &1774420046
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1774420043}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_Sprite: {fileID: 0}
m_Type: 1
m_PreserveAspect: 0
m_FillCenter: 1
m_FillMethod: 4
m_FillAmount: 1
m_FillClockwise: 1
m_FillOrigin: 0
m_UseSpriteMesh: 0
m_PixelsPerUnitMultiplier: 1
--- !u!222 &1774420047
CanvasRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1774420043}
m_CullTransparentMesh: 0
--- !u!1 &1925683365
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1925683366}
- component: {fileID: 1925683368}
- component: {fileID: 1925683367}
m_Layer: 5
m_Name: ButtonText
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &1925683366
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1925683365}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 1774420044}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &1925683367
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1925683365}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 5f7201a12d95ffc409449d95f23cf332, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 0.19607843, g: 0.19607843, b: 0.19607843, a: 1}
m_RaycastTarget: 1
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_FontData:
m_Font: {fileID: 10102, guid: 0000000000000000e000000000000000, type: 0}
m_FontSize: 16
m_FontStyle: 0
m_BestFit: 0
m_MinSize: 1
m_MaxSize: 40
m_Alignment: 4
m_AlignByGeometry: 0
m_RichText: 1
m_HorizontalOverflow: 0
m_VerticalOverflow: 0
m_LineSpacing: 1
m_Text: Dispatch Event return String
--- !u!222 &1925683368
CanvasRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1925683365}
m_CullTransparentMesh: 0
--- !u!1 &1968377177
GameObject:
m_ObjectHideFlags: 0
Expand Down
9 changes: 8 additions & 1 deletion testing/project/Assets/Plugins/WebGL/Bridge.jslib
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mergeInto(LibraryManager.library, {
mergeInto(LibraryManager.library, {
ClickedTestButton: function () {
dispatchReactUnityEvent("ClickedTestButton");
},
Expand All @@ -25,4 +25,11 @@
boolValue: boolValue === 1,
});
},
ClickedTestButtonReturnString: function () {
var returnStr = dispatchReactUnityEvent("ClickedTestButtonReturnString");
var bufferSize = lengthBytesUTF8(returnStr) + 1;
var buffer = _malloc(bufferSize);
stringToUTF8(returnStr, buffer, bufferSize);
return buffer;
},
});
Binary file modified testing/public/unity-build/communication-tests.data
Binary file not shown.

Large diffs are not rendered by default.

Binary file modified testing/public/unity-build/communication-tests.wasm
Binary file not shown.
7 changes: 7 additions & 0 deletions testing/src/unity-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,26 @@ function UnityTest() {
console.log({ parameters });
}

function logParametersToConsoleWithReturn(...parameters: any[]) {
logParametersToConsole({ parameters });
return "String from React App";
}

addEventListener("ClickedTestButton", logParametersToConsole);
addEventListener("ClickedStringTestButton", logParametersToConsole);
addEventListener("ClickedNumberTestButton", logParametersToConsole);
addEventListener("ClickedNumbersTestButton", logParametersToConsole);
addEventListener("ClickedBoolTestButton", logParametersToConsole);
addEventListener("ClickedObjectTestButton", logParametersToConsole);
addEventListener("ClickedTestButtonReturnString", logParametersToConsoleWithReturn);
return () => {
removeEventListener("ClickedTestButton", logParametersToConsole);
removeEventListener("ClickedStringTestButton", logParametersToConsole);
removeEventListener("ClickedNumberTestButton", logParametersToConsole);
removeEventListener("ClickedNumbersTestButton", logParametersToConsole);
removeEventListener("ClickedBoolTestButton", logParametersToConsole);
removeEventListener("ClickedObjectTestButton", logParametersToConsole);
removeEventListener("ClickedTestButtonReturnString", logParametersToConsoleWithReturn);
};
}, [addEventListener, removeEventListener]);

Expand Down

0 comments on commit 557163d

Please sign in to comment.