Files
18xchat/.svn/pristine/62/62dd49a7844fe5def4673aa9d7841d50394d75db.svn-base
T
2025-07-17 17:18:21 +08:00

172 lines
6.2 KiB
Plaintext

import { _decorator, AudioClip, Button, Component, ImageAsset, JsonAsset, Label, Node, Prefab, ProgressBar, Sprite, SpriteFrame } from 'cc';
import { GButton } from '../../Main/Common/GButton';
import ResManager from '../../Main/Manager/ResManager';
import Resource from '../../Main/Config/Resource';
import { SDKManager } from '../../Main/Channel/SDKManager';
import HttpUnit from '../../Main/Common/HttpUnit';
import GlobalValue from '../../Main/Common/GlobalValue';
import Utils from '../../Main/Common/Utils';
const { ccclass, property } = _decorator;
@ccclass('PreloadUI')
export class PreloadUI extends Component {
private checkFinish = true; //检查完成
private preloadFinish = false; //资源加载完成
private loginFinish = false; //登录完成
private jinduBar: Node = null;
private jinduFilled: Sprite = null;
private LabProgress: Label = null;
private btnLogin: Sprite = null; //登录按钮
start() {
this.jinduBar = this.node.getChildByName("jinduBar");
this.jinduFilled = this.jinduBar.getChildByName("jinduFilled").getComponent(Sprite);
this.LabProgress = this.node.getChildByName("LabProgress").getComponent(Label);
this.btnLogin = this.node.getChildByName("btnLogin").getComponent(Sprite);
console.log("preload, btnLogin:", this.btnLogin);
//适配背景图
let BG = this.node.getChildByName("BG");
Utils.adjustBgPixelRatio(BG, 1)
this.jinduFilled.fillRange = 0;
this.btnLogin.node.active = false;
this.preloadFiles()
}
update(deltaTime: number) {
if (this.checkFinish) {
if (this.preloadFinish && this.loginFinish) {
this.checkFinish = false
this.enterGame()
}
}
}
//预加载文件
preloadFiles() {
let preloadTab = [
{path:"Music/music_interface", bundleName: "Audio", ftype: AudioClip},
{path:"Zjm/raw1", bundleName: "Raw", ftype: ImageAsset},
{path:"bg/mengli_stage_bg", bundleName: "Raw", ftype: ImageAsset},
{path:"touming", bundleName: "common", ftype: ImageAsset},
// {path:"chanpin_1_0", bundleName: "daoju", ftype: ImageAsset}, //有合图加载失败
{path:"zjm_1", bundleName: "Zhujiemian", ftype: ImageAsset},
{path:"UI/GM_UI", bundleName: "PB", ftype: Prefab},
{path:"commonGlobal", bundleName: "DataTable", ftype: JsonAsset},
];
let num_prefab = 0; //已加载数量
let num_succeed = 0;//加载成功数量
let preloadFunc = () => {
if (num_prefab < preloadTab.length) {
let cfg = preloadTab[num_prefab]
ResManager.I.preLoadSubpackageFile(cfg.path, cfg.bundleName, cfg.ftype, (ret) => {
num_prefab++;
num_succeed++;
let percent = 0.45 * (num_prefab / preloadTab.length)
this.showLoadProgress(0.55 + percent);
console.log("加载成功:", cfg.path);
preloadFunc();
}, (err)=>{
num_prefab++;
console.log("加载失败:", err);
});
} else {
if (num_succeed >= num_prefab) {
this.loadDataTable();
}
}
}
preloadFunc();
}
//加载配置表
loadDataTable(){
let dataArr = [
"commonGlobal",
// "levelHole",
"Music",
"Sound",
]
//配置表使用方法: let cfgs = Resource.getConfig("Sound")
let num_prefab = 0;
let preloadFunc = () => {
if (num_prefab < dataArr.length) {
let cfgname = dataArr[num_prefab]
ResManager.I.loadSubpackageDataTable(cfgname, (ret: JsonAsset) => {
num_prefab++;
Resource.addSubJsonConfig(cfgname, ret.json)
preloadFunc();
});
} else {
this.preloadFinish = true;
this.onLogin();
}
}
preloadFunc();
}
//显示加载进度
showLoadProgress(percent) {
this.jinduFilled.fillRange = percent;
}
//登录
onLogin() {
//初始化SDK信息
SDKManager.init(() => {
//检测是否有授权,有授权直接登录,没有授权就先进主界面,到主界面获取权限
SDKManager.checkAutoSetting("userInfo", (ishave) => {
if (ishave) {
console.log("有授权,直接登录")
HttpUnit.ins.login((loginData) => {
this.loginFinish = true
})
} else {
console.log("没有授权,等待授权后登录")
// this.loginFinish = true
this.jinduBar.active = false
this.LabProgress.node.active = false
//登录按钮
this.btnLogin.node.active = true
let loginPath = ""
if (SDKManager.isWenxin()) {
loginPath = "login1"
} else if (SDKManager.isByteDance()) {
loginPath = "login2"
} else if (SDKManager.isKuaishou()) {
loginPath = "login3"
}
ResManager.I.changeResourceSpriteFrame(this.btnLogin, loginPath)
HttpUnit.ins.login((loginData) => {
this.loginFinish = true
})
}
})
})
}
enterGame() {
let islogin = HttpUnit.IsLogin();
console.log("进入游戏,登录状态:", islogin);
this.jinduBar.active = false
this.LabProgress.node.active = false
this.btnLogin.node.active = false
if (islogin) {
} else {
//没登录成功,先进主界面,在主界面里进行登录
}
ResManager.I.goMainScene(); //进入下一个场景
// ResManager.I.goCustomScene("Test2DScene"); //进入自定义场景
}
}