feat: 添加微信SDK和关卡页面,重构预制体结构
- 新增 WxSDK 微信SDK工具类 - 新增 PageLevel 关卡选择页面组件 - 将 prefabs 目录从 resources 移至根目录 - 更新 ViewManager 支持预制体属性引用 - 添加 BaseView 页面基类 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
import { _decorator, Component } from 'cc';
|
||||
import { _decorator, Component, Prefab } from 'cc';
|
||||
const { ccclass } = _decorator;
|
||||
|
||||
/**
|
||||
* 页面配置接口
|
||||
*/
|
||||
export interface ViewConfig {
|
||||
prefabPath: string; // 相对于 resources 的路径
|
||||
prefab: Prefab; // 预制体引用(主包资源)
|
||||
cache?: boolean; // 是否缓存页面,默认 true
|
||||
zIndex?: number; // 层级,默认 0
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { _decorator, Node, resources, Prefab, instantiate, error } from 'cc';
|
||||
import { _decorator, Node, Prefab, instantiate, error } from 'cc';
|
||||
import { BaseView, ViewConfig, ViewOptions } from './BaseView';
|
||||
const { ccclass } = _decorator;
|
||||
|
||||
@@ -7,7 +7,6 @@ const { ccclass } = _decorator;
|
||||
*/
|
||||
interface RegisteredView {
|
||||
config: ViewConfig;
|
||||
prefab: Prefab | null; // 缓存的预制体
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -70,8 +69,7 @@ export class ViewManager {
|
||||
cache: true, // 默认缓存
|
||||
zIndex: 0, // 默认层级
|
||||
...config
|
||||
},
|
||||
prefab: null
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -111,24 +109,8 @@ export class ViewManager {
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查是否有缓存的预制体
|
||||
if (registered.prefab) {
|
||||
this._instantiateView(viewId, registered.prefab, options);
|
||||
return;
|
||||
}
|
||||
|
||||
// 动态加载预制体
|
||||
resources.load(registered.config.prefabPath, Prefab, (err, prefab) => {
|
||||
if (err) {
|
||||
error(`ViewManager: 加载预制体失败 "${registered.config.prefabPath}"`, err);
|
||||
options?.onError?.(err);
|
||||
return;
|
||||
}
|
||||
|
||||
// 缓存预制体
|
||||
registered.prefab = prefab;
|
||||
this._instantiateView(viewId, prefab!, options);
|
||||
});
|
||||
// 直接使用预制体引用实例化
|
||||
this._instantiateView(viewId, registered.config.prefab, options);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -144,7 +126,7 @@ export class ViewManager {
|
||||
const view = node.getComponent(BaseView);
|
||||
|
||||
if (!view) {
|
||||
error(`ViewManager: 预制体 "${registered.config.prefabPath}" 缺少 BaseView 组件`);
|
||||
error(`ViewManager: 预制体 "${viewId}" 缺少 BaseView 组件`);
|
||||
node.destroy();
|
||||
options?.onError?.(new Error('缺少 BaseView 组件'));
|
||||
return;
|
||||
@@ -290,7 +272,7 @@ export class ViewManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* 预加载页面预制体
|
||||
* 预加载页面预制体(主包资源已随游戏加载,此方法仅为兼容性保留)
|
||||
* @param viewId 页面标识
|
||||
* @param onProgress 进度回调
|
||||
* @param onComplete 完成回调
|
||||
@@ -303,22 +285,9 @@ export class ViewManager {
|
||||
return;
|
||||
}
|
||||
|
||||
// 已缓存
|
||||
if (registered.prefab) {
|
||||
onProgress?.(1);
|
||||
onComplete?.();
|
||||
return;
|
||||
}
|
||||
|
||||
resources.load(registered.config.prefabPath, Prefab, (err, prefab) => {
|
||||
if (err) {
|
||||
error(`ViewManager: 预加载失败 "${registered.config.prefabPath}"`, err);
|
||||
} else {
|
||||
registered.prefab = prefab;
|
||||
onProgress?.(1);
|
||||
}
|
||||
onComplete?.();
|
||||
});
|
||||
// 主包资源已加载,直接回调
|
||||
onProgress?.(1);
|
||||
onComplete?.();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
148
assets/scripts/utils/WxSDK.ts
Normal file
148
assets/scripts/utils/WxSDK.ts
Normal file
@@ -0,0 +1,148 @@
|
||||
import { sys } from 'cc';
|
||||
|
||||
/**
|
||||
* 微信分享配置
|
||||
*/
|
||||
export interface WxShareConfig {
|
||||
/** 分享标题 */
|
||||
title: string;
|
||||
/** 分享图片 URL 或本地路径 */
|
||||
imageUrl?: string;
|
||||
/** 查询字符串,从这条转发消息进入后,可通过 wx.getLaunchOptionsSync 或 wx.onShow 获取 */
|
||||
query?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 微信朋友圈分享配置
|
||||
*/
|
||||
export interface WxShareTimelineConfig {
|
||||
/** 分享标题 */
|
||||
title: string;
|
||||
/** 分享图片 URL 或本地路径 */
|
||||
imageUrl?: string;
|
||||
/** 查询字符串 */
|
||||
query?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 微信小游戏 SDK 工具类
|
||||
* 封装微信平台相关 API,非微信环境下静默降级
|
||||
*/
|
||||
export class WxSDK {
|
||||
/**
|
||||
* 是否处于微信小游戏环境
|
||||
*/
|
||||
static isWechat(): boolean {
|
||||
return sys.platform === sys.Platform.WECHAT_GAME;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 wx 全局对象(仅微信环境下可用)
|
||||
*/
|
||||
private static getWx(): any {
|
||||
if (!WxSDK.isWechat()) return null;
|
||||
return typeof wx !== 'undefined' ? wx : null;
|
||||
}
|
||||
|
||||
// ==================== 分享相关 ====================
|
||||
|
||||
/**
|
||||
* 开启转发/分享菜单
|
||||
* 调用后用户可通过右上角菜单进行转发
|
||||
* @param withShareTicket 是否带 shareTicket,用于获取群信息
|
||||
*/
|
||||
static showShareMenu(withShareTicket: boolean = true): void {
|
||||
const wxApi = WxSDK.getWx();
|
||||
if (!wxApi) return;
|
||||
|
||||
wxApi.showShareMenu({
|
||||
withShareTicket,
|
||||
menus: ['shareAppMessage', 'shareTimeline'],
|
||||
success: () => {
|
||||
console.log('[WxSDK] showShareMenu 成功');
|
||||
},
|
||||
fail: (err: any) => {
|
||||
console.warn('[WxSDK] showShareMenu 失败', err);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置被动分享(右上角菜单 "转发给朋友")的内容
|
||||
* 需要在页面加载后尽早调用,只需调用一次
|
||||
* @param config 分享配置
|
||||
*/
|
||||
static onShareAppMessage(config: WxShareConfig): void {
|
||||
const wxApi = WxSDK.getWx();
|
||||
if (!wxApi) return;
|
||||
|
||||
wxApi.onShareAppMessage(() => ({
|
||||
title: config.title,
|
||||
imageUrl: config.imageUrl ?? '',
|
||||
query: config.query ?? ''
|
||||
}));
|
||||
|
||||
console.log('[WxSDK] onShareAppMessage 已设置');
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置分享到朋友圈的内容
|
||||
* @param config 朋友圈分享配置
|
||||
*/
|
||||
static onShareTimeline(config: WxShareTimelineConfig): void {
|
||||
const wxApi = WxSDK.getWx();
|
||||
if (!wxApi) return;
|
||||
|
||||
if (typeof wxApi.onShareTimeline !== 'function') {
|
||||
console.warn('[WxSDK] 当前微信版本不支持 onShareTimeline');
|
||||
return;
|
||||
}
|
||||
|
||||
wxApi.onShareTimeline(() => ({
|
||||
title: config.title,
|
||||
imageUrl: config.imageUrl ?? '',
|
||||
query: config.query ?? ''
|
||||
}));
|
||||
|
||||
console.log('[WxSDK] onShareTimeline 已设置');
|
||||
}
|
||||
|
||||
/**
|
||||
* 主动触发转发(拉起分享面板)
|
||||
* @param config 分享配置
|
||||
*/
|
||||
static shareAppMessage(config: WxShareConfig): void {
|
||||
const wxApi = WxSDK.getWx();
|
||||
if (!wxApi) return;
|
||||
|
||||
wxApi.shareAppMessage({
|
||||
title: config.title,
|
||||
imageUrl: config.imageUrl ?? '',
|
||||
query: config.query ?? ''
|
||||
});
|
||||
|
||||
console.log('[WxSDK] shareAppMessage 已触发');
|
||||
}
|
||||
|
||||
/**
|
||||
* 一键初始化分享功能
|
||||
* 开启分享菜单 + 设置被动分享内容 + 设置朋友圈分享内容
|
||||
* @param config 分享配置
|
||||
*/
|
||||
static initShare(config: WxShareConfig): void {
|
||||
if (!WxSDK.isWechat()) {
|
||||
console.log('[WxSDK] 非微信环境,跳过分享初始化');
|
||||
return;
|
||||
}
|
||||
|
||||
WxSDK.showShareMenu();
|
||||
WxSDK.onShareAppMessage(config);
|
||||
WxSDK.onShareTimeline({
|
||||
title: config.title,
|
||||
imageUrl: config.imageUrl,
|
||||
query: config.query
|
||||
});
|
||||
|
||||
console.log('[WxSDK] 分享功能初始化完成');
|
||||
}
|
||||
}
|
||||
9
assets/scripts/utils/WxSDK.ts.meta
Normal file
9
assets/scripts/utils/WxSDK.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "ba0af7e0-a456-4b08-b058-8f77f5a7481f",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
Reference in New Issue
Block a user