index.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. const saveDom = document.querySelector('.save-image-backgroup');
  2. let isShowSaveDom = false;
  3. window.updateTexture = function (title, base64string) {
  4. saveDom.src = 'data:image/png;base64,' + base64string;
  5. isShowSaveDom = true;
  6. saveDom.style.pointerEvents = 'auto';
  7. refreshShareBlockSize();
  8. }
  9. window.gameInitialized = function () {
  10. }
  11. window.closeGame = function () {
  12. window.parent?.closeGame();
  13. }
  14. window.closePanel = function () {
  15. isShowSaveDom = false;
  16. saveDom.style.pointerEvents = 'none';
  17. }
  18. window.clickEdit = function () {
  19. window.parent?.isShowToastFromGame()
  20. }
  21. // 提供给父页面点击保存按钮使用
  22. window.saveTitle = function(msg) {
  23. window.unityInstance.SendMessage('WebEvent', 'UpdateTextureContent', msg)
  24. }
  25. function refreshShareBlockSize() {
  26. // Unity画布宽高比
  27. let unityCanvasWidth = 1560;
  28. let unityCanvaslHeight = 3376;
  29. let unityPanelWidth = 1450;
  30. let unityPanelHeight = 2800;
  31. let unityPanelOffsetY = -148;
  32. let innerWidth = window.innerWidth;
  33. let innerHeight = window.innerHeight;
  34. let scale = (unityCanvasWidth / unityPanelHeight) >
  35. (innerWidth / innerHeight) ? (innerWidth / unityCanvasWidth) : (innerHeight / unityCanvaslHeight);
  36. saveDom.style.width = (unityPanelWidth * scale) + 'px';
  37. saveDom.style.height = (unityPanelHeight * scale) + 'px';
  38. if (unityPanelOffsetY > 0){
  39. saveDom.style.marginTop = (unityPanelOffsetY * scale * 2) + 'px';
  40. } else {
  41. saveDom.style.marginBottom = (-unityPanelOffsetY * scale * 2) + 'px';
  42. }
  43. }
  44. window.addEventListener('resize', ()=>{
  45. if (isShowSaveDom) refreshShareBlockSize();
  46. });