UISplash.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import GameController from "../script/GameController";
  2. import AssetMgr from "../script/utils/AssetMgr";
  3. // 不允许在这个脚本中引用其他文件
  4. const { ccclass, menu, property } = cc._decorator;
  5. @ccclass
  6. @menu("UI/scene/UISplash")
  7. export default class UISplash extends cc.Component {
  8. @property(cc.Label)
  9. txtVersion: cc.Label = null
  10. @property(cc.Label)
  11. txtTips: cc.Label = null
  12. @property(cc.Node)
  13. bg1: cc.Node = null
  14. @property(cc.Node)
  15. bg2: cc.Node = null
  16. onLoad() {
  17. cc.game.clear()
  18. cc.debug.setDisplayStats(false)
  19. //开启强制合批
  20. cc.macro.CLEANUP_IMAGE_CACHE = true;
  21. cc.dynamicAtlasManager.enabled = false;
  22. window["fps"] = () => {
  23. cc.debug.setDisplayStats(!cc.debug.isDisplayStats())
  24. if (cc.debug.isDisplayStats()) {
  25. setTimeout(() => {
  26. if (cc.find('PROFILER-NODE')) {
  27. cc.find('PROFILER-NODE').scale = 2;
  28. }
  29. }, 200);
  30. }
  31. }
  32. //更新新版本,删除旧缓存
  33. if (cc.sys.platform == cc.sys.WECHAT_GAME) {
  34. //小游戏平台删除过期缓存,防止缓存超过50M
  35. // cc.assetManager.cacheManager.clearLRU()
  36. cc.assetManager.cacheManager.autoClear = true
  37. }
  38. }
  39. protected start(): void {
  40. //电魂sdk漏斗打点
  41. if (window['dhSDK']) {
  42. // 事件ID
  43. let eventId = "110001::GAME_CLIENT_SDK_ACTIVATE"; // 表示SDK客户端上报的启动事件
  44. // 事件参数
  45. let eventParams = {};
  46. // 必填参数
  47. eventParams["logLevel"] = "d";
  48. eventParams["logContent"] = "游戏首包加载结束";
  49. // 执行上报
  50. window['dhSDK'].trackEvent(eventId, eventParams);
  51. }
  52. //电魂sdk漏斗打点
  53. if (window['dhSDK']) {
  54. // 事件ID
  55. let eventId = "110000::GAME_CLIENT_LAUNCH"; // 表示SDK客户端上报的启动事件
  56. // 事件参数
  57. let eventParams = {};
  58. // 必填参数
  59. eventParams["logLevel"] = "d";
  60. eventParams["logContent"] = "SDK启动";
  61. // 执行上报
  62. window['dhSDK'].trackEvent(eventId, eventParams);
  63. }
  64. if (!cc.Node.prototype["clickChange"]) {
  65. let prototype = cc.Node.prototype;
  66. let dispatchEvent = prototype.dispatchEvent;
  67. prototype.dispatchEvent = function (event) {
  68. try {
  69. if (event.type == cc.Node.EventType.TOUCH_END) {
  70. cc.game.emit("GameEvent.ON_CLICK", event)
  71. }
  72. dispatchEvent.bind(this)(event);
  73. } catch (error) {
  74. console.error("按钮报错", "", "", error.message, error.stack)
  75. if (window["sendClientErr"]) {
  76. window["sendClientErr"]([error.message.substring(0, 200), error.stack.substring(0, 400)])
  77. }
  78. }
  79. }
  80. cc.Node.prototype["clickChange"] = true;
  81. }
  82. // 游戏速率变量
  83. cc.director["_kSpeed"] = 1;
  84. var _originCalculateDeltaTime = cc.Director.prototype["calculateDeltaTime"];
  85. cc.director["calculateDeltaTime"] = function (now) {
  86. _originCalculateDeltaTime.call(this, now);
  87. this._deltaTime *= this._kSpeed;
  88. }
  89. //设置速率
  90. // cc.director.setSpeed = function (speed) {
  91. // cc.director["_kSpeed"] = speed;
  92. // }
  93. console.log("===脚本分包加载开始")
  94. let time = new Date().getTime()
  95. // cc.assetManager
  96. this.txtTips.string = "正在加载本地资源..."
  97. cc.assetManager.loadBundle("scriptAsset", (err, bundle) => {
  98. if (err) {
  99. console.error(`load scriptAsset ${0} err:${1}`, bundle, err)
  100. return
  101. }
  102. console.log("===脚本分包加载完成", new Date().getTime() - time)
  103. this.txtTips.string = "正在加载配置..."
  104. import("RemoteConfig" as any).then((remoteParam) => {
  105. let remoteConfig = remoteParam.default
  106. import("GameController" as any).then((gameCtrlParam) => {
  107. let gameController = gameCtrlParam.default
  108. import("Config" as any).then((cfgParam) => {
  109. let config = cfgParam.default
  110. // pad
  111. config.isPad = cc.winSize.width / cc.winSize.height > (750 / 1334)
  112. // 显示版本号
  113. this.txtVersion.node.active = true
  114. this.txtVersion.string = "版本号:" + config.appVersion
  115. this.txtTips.string = "游戏初始化中(50%)..."
  116. remoteConfig.init(() => {
  117. this.txtTips.string = "游戏初始化中(80%)..."
  118. //电魂sdk漏斗打点
  119. if (window['dhSDK']) {
  120. // 事件ID
  121. let eventId = "100102::GAME_CREATE_VERSION_SUCCESS"; // 表示SDK客户端上报的启动事件
  122. // 事件参数
  123. let eventParams = {};
  124. // 必填参数
  125. eventParams["logLevel"] = "d";
  126. eventParams["logContent"] = "检查版本结束";
  127. // 执行上报
  128. window['dhSDK'].trackEvent(eventId, eventParams);
  129. }
  130. //电魂sdk漏斗打点
  131. if (window['dhSDK']) {
  132. // 事件ID
  133. let eventId = "211000::GAME_CLIENT_INIT_START"; // 表示SDK客户端上报的启动事件
  134. // 事件参数
  135. let eventParams = {};
  136. // 必填参数
  137. eventParams["logLevel"] = "d";
  138. eventParams["logContent"] = "游戏资源包加载进度条开始";
  139. // 执行上报
  140. window['dhSDK'].trackEvent(eventId, eventParams);
  141. }
  142. gameController.init((isSucc: boolean) => {
  143. if (isSucc) {
  144. //电魂sdk漏斗打点
  145. if (window['dhSDK']) {
  146. // 事件ID
  147. let eventId = "211001::GAME_CLIENT_INIT_END"; // 表示SDK客户端上报的启动事件
  148. // 事件参数
  149. let eventParams = {};
  150. // 必填参数
  151. eventParams["logLevel"] = "d";
  152. eventParams["logContent"] = "游戏资源包加载进度条结束";
  153. // 执行上报
  154. window['dhSDK'].trackEvent(eventId, eventParams);
  155. }
  156. this.txtTips.string = "游戏初始化中(100%)..."
  157. // 计算屏幕宽度, 考虑刘海屏
  158. config.safeAreaRect = cc.sys.getSafeAreaRect()
  159. // //是否是电脑端微信
  160. // let isWinByWx = false
  161. // if (cc.sys.platform == cc.sys.WECHAT_GAME) {
  162. // let sysInfo = wx.getSystemInfoSync();
  163. // if (sysInfo?.platform == "windows" || sysInfo?.platform == "mac") {
  164. // isWinByWx = true
  165. // }
  166. // }
  167. if (config.safeAreaRect.y == 0 && cc.winSize.width / cc.winSize.height < 0.5) {
  168. //识别不到安全区域的设备,强制预留
  169. config.safeAreaRect.y = 50
  170. }
  171. console.log("安全区域:", config.safeAreaRect.x, config.safeAreaRect.y, config.safeAreaRect.width, config.safeAreaRect.height)
  172. let perY = config.safeAreaRect.y / (config.safeAreaRect.height)
  173. console.log("屏幕宽高:", cc.winSize.width, cc.winSize.height)
  174. let realSafeY = config.safeAreaRect.y;//perY * cc.winSize.height //config.safeAreaRect.y
  175. console.log("占用百分比:", perY, "真实安全区域像素x:", realSafeY)
  176. config.realHeight = cc.winSize.height - Math.abs(realSafeY) * 1.5
  177. console.log("最终高度:", config.realHeight)
  178. if (cc.sys.isNative && (cc.sys.platform == cc.sys.ANDROID || cc.sys.platform == cc.sys.IPHONE || cc.sys.platform == cc.sys.IPAD)) {
  179. //等待检测更新后再切换场景
  180. let hotUpdateCom = this.node.getComponent("HotUpdate")
  181. hotUpdateCom.checkUpdate()
  182. } else {
  183. // 加载资源
  184. cc.director.loadScene("loading")
  185. }
  186. }
  187. });
  188. })
  189. // 设置游戏主循环刷新帧率
  190. cc.game.setFrameRate(config.GAME_FRAME)
  191. // 设置动画间隔
  192. // cc.director.setAnimationInterval(1000 / config.GAME_FRAME)
  193. })
  194. })
  195. })
  196. })
  197. }
  198. }