Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions Runtime/WebGLTemplates/Bridge/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,52 @@
top: 0px;
left: 0px;
}
#dpr-display {
position: fixed;
top: 10px;
right: 10px;
background: rgba(0, 0, 0, 0.7);
color: #fff;
padding: 8px 12px;
font-family: monospace;
font-size: 14px;
border-radius: 4px;
z-index: 9999;
}
</style>
</head>
<body>
<canvas id="canvas" class="fullscreen-canvas"></canvas>
<div id="dpr-display"></div>
<script>
function updateDPR() {
const isIOS = /iPhone|iPad|iPod/.test(navigator.userAgent);
const cores = navigator.hardwareConcurrency || 2;
const memory = navigator.deviceMemory || 2;
const isWeak = cores <= 4 && memory <= 4;
const nativeDPR = window.devicePixelRatio || 1;

let usedDPR;
if (isIOS || !isWeak) {
usedDPR = 'native';
} else if (navigator.maxTouchPoints > 0 && window.innerWidth < 768) {
usedDPR = 1;
} else if (navigator.maxTouchPoints > 0 && window.innerWidth < 1200) {
usedDPR = 1.5;
} else {
usedDPR = Math.min(nativeDPR, 1.5);
}

document.getElementById('dpr-display').innerHTML =
`Native DPR: ${nativeDPR.toFixed(2)}<br>` +
`Used DPR: ${usedDPR}<br>` +
`iOS: ${isIOS}<br>` +
`Cores: ${cores}<br>` +
`Memory: ${memory}GB`;
}
updateDPR();
window.matchMedia(`(resolution: ${window.devicePixelRatio}dppx)`).addEventListener('change', updateDPR);
</script>
<script src="./playgama-bridge-unity.js"></script>
</body>
</html>
54 changes: 38 additions & 16 deletions Runtime/WebGLTemplates/Bridge/playgama-bridge-unity.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,25 @@ const STORAGE_KEYS_SEPARATOR = '{bridge_keys_separator}'
const STORAGE_VALUES_SEPARATOR = '{bridge_values_separator}'

// utils
function getOptimalDPR() {
const isIOS = /iPhone|iPad|iPod/.test(navigator.userAgent);
if (isIOS) return null;

const cores = navigator.hardwareConcurrency || 2;
const memory = navigator.deviceMemory || 2;
const hasTouch = navigator.maxTouchPoints > 0;
const width = window.innerWidth;
const dpr = window.devicePixelRatio || 1;

const isWeak = cores <= 4 && memory <= 4;

if (!isWeak) return null;

if (hasTouch && width < 768) return 1;
if (hasTouch && width < 1200) return 1.5;
return Math.min(dpr, 1.5);
}

window.unityInstance = null
const messageQueue = []
let progressBarFillingInterval = null
Expand Down Expand Up @@ -137,26 +156,29 @@ function initializeBridge() {
let unityLoader = document.createElement('script')
unityLoader.src = 'Build/{{{ LOADER_FILENAME }}}'
unityLoader.onload = () => {
createUnityInstance(
CANVAS,
{
dataUrl: 'Build/{{{ DATA_FILENAME }}}',
frameworkUrl: 'Build/{{{ FRAMEWORK_FILENAME }}}',
codeUrl: 'Build/{{{ CODE_FILENAME }}}',
const config = {
dataUrl: 'Build/{{{ DATA_FILENAME }}}',
frameworkUrl: 'Build/{{{ FRAMEWORK_FILENAME }}}',
codeUrl: 'Build/{{{ CODE_FILENAME }}}',
#if MEMORY_FILENAME
memoryUrl: 'Build/{{{ MEMORY_FILENAME }}}',
memoryUrl: 'Build/{{{ MEMORY_FILENAME }}}',
#endif
#if SYMBOLS_FILENAME
symbolsUrl: 'Build/{{{ SYMBOLS_FILENAME }}}',
symbolsUrl: 'Build/{{{ SYMBOLS_FILENAME }}}',
#endif
streamingAssetsUrl: 'StreamingAssets',
companyName: '{{{ COMPANY_NAME }}}',
productName: '{{{ PRODUCT_NAME }}}',
productVersion: '{{{ PRODUCT_VERSION }}}',
// matchWebGLToCanvasSize: false, // Uncomment this to separately control WebGL canvas render size and DOM element size.
// devicePixelRatio: 1, // Uncomment this to override low DPI rendering on high DPI displays.
},
onUnityLoadingProgressChanged)
streamingAssetsUrl: 'StreamingAssets',
companyName: '{{{ COMPANY_NAME }}}',
productName: '{{{ PRODUCT_NAME }}}',
productVersion: '{{{ PRODUCT_VERSION }}}',
// matchWebGLToCanvasSize: false, // Uncomment this to separately control WebGL canvas render size and DOM element size.
}

const optimalDPR = getOptimalDPR()
if (optimalDPR !== null) {
config.devicePixelRatio = optimalDPR
}

createUnityInstance(CANVAS, config, onUnityLoadingProgressChanged)
.then((unityInstance) => {
window.unityInstance = unityInstance
CANVAS.focus()
Expand Down