forked from fusetools/react-native-unity2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
71 lines (63 loc) · 1.61 KB
/
Copy pathApp.tsx
File metadata and controls
71 lines (63 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import React from "react"
import {Button, View} from "react-native"
import {UnityModule, UnityView} from "react-native-unity2"
export default function App() {
return (
<UnityView
style={{flex: 1, justifyContent: "flex-end"}}
onMessage={onMessage}
onReady={onReady}
keepAwake={true}>
<View
style={{
flexDirection: "row",
alignContent: "space-between",
justifyContent: "center",
}}>
<Button
title={"setColor"}
onPress={async () =>
console.log(await cubeApi.setColor(randomColor()))
}
/>
<Button
title={"toggleRotate"}
onPress={async () => console.log(await cubeApi.toggleRotate())}
/>
<Button
title={"getAccount"}
onPress={async () => console.log(await cubeApi.getAccount())}
/>
<Button
title={"fail"}
onPress={async () => console.log(await cubeApi.fail())}
/>
</View>
</UnityView>
)
}
const onMessage = (data: any) => {
console.log("Unity message: " + data)
}
const onReady = () => {
console.log("Ready!")
}
const cubeApi = {
setColor(color: string) {
return UnityModule.callMethod("Cube", "setColorRN", color)
},
toggleRotate() {
return UnityModule.callMethod("Cube", "toggleRotateRN")
},
getAccount() {
return UnityModule.callMethod("Cube", "getAccountRN")
},
fail() {
return UnityModule.callMethod("Cube", "failRN")
},
}
const randomColor = () => {
return `#${Math.floor(Math.random() * 16777215)
.toString(16)
.padStart(6, "0")}`
}