-
Notifications
You must be signed in to change notification settings - Fork 5
/
play-unity.js
121 lines (111 loc) · 3.34 KB
/
play-unity.js
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import React, {
Fragment,
useEffect,
useRef,
useState,
} from "react";
import { Box, Button, Stack } from "@mui/material";
const isLocal =
typeof window !== "undefined" && window.location.host === "localhost:3000";
function sendTokenToUnity(token) {
const iframe = document.getElementById("unity-webgl-iframe"); // Make sure to give your iframe an id
if (iframe && iframe.contentWindow) {
iframe.contentWindow.postMessage(
{
type: "SendDIDToken",
value: token,
},
"*"
);
}
}
export const PlayUnity = ({ user, didToken, onClose }) => {
const [frameHeight, setFrameHeight] = useState(window.innerHeight);
const [isLoaded, setIsLoaded] = useState(false);
const isInitialMount = useRef(true);
useEffect(() => {
if (isLocal && isInitialMount.current) {
isInitialMount.current = false;
} else {
function updateFrameHeight() {
setFrameHeight(window.innerHeight);
}
function handleUnityMessage(event) {
if (event.data === "unity-webgl-loaded") {
setIsLoaded(true);
}
}
window.addEventListener("resize", updateFrameHeight);
window.addEventListener("message", handleUnityMessage);
return () => {
window.removeEventListener("resize", updateFrameHeight);
window.removeEventListener("message", handleUnityMessage);
onClose();
};
}
}, []);
useEffect(() => {
if (!user || !isLoaded || !didToken) return;
sendTokenToUnity(didToken);
}, [isLoaded, user]);
return (
<Fragment>
<Stack>
<Box
sx={{
width: "100%",
height: { xs: frameHeight + "px", lg: `${frameHeight - 42}px` },
overflow: "hidden", // Prevent scrolling
maxHeight: "100vh",
}}
>
<iframe
id="unity-webgl-iframe"
src="unity/index.html"
style={{
position: "relative",
top: 0,
left: 0,
zIndex: 999,
width: "100%",
height: "100%",
border: "none",
}}
title="Unity WebGL Content"
allowFullScreen
/>
</Box>
<Button
variant="contained"
color="primary"
sx={{
backgroundColor: "#045CD2", // Set the background color
width: "100%",
height: "42px",
boxShadow: "0px 3px 5px 2px rgba(0, 0, 0, .3)", // Add drop shadow
fontWeight: "bold", // Bold font
"&:hover::before": {
// Use the ::before pseudo-element for the overlay
content: '""',
position: "absolute",
top: 0,
borderRadius: "5px",
right: 0,
bottom: 0,
left: 0,
backgroundColor: "rgba(255, 255, 255, 0.1)", // 10% white overlay
zIndex: 1, // Ensure the overlay is above the card content but below any interactive elements
},
"&:hover": {
backgroundColor: "#045CD2", // Keep the background color consistent when hovered
boxShadow: "0px 3px 5px 2px rgba(0, 0, 0, .3)", // Add drop shadow
},
}}
onClick={onClose}
>
Return
</Button>
</Stack>
</Fragment>
);
};