init
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
import { _decorator, Component, Node, director, resources, instantiate, Prefab, js } from 'cc';
|
||||
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass('PanelBase')
|
||||
export class PanelBase extends Component {
|
||||
protected isOpen: boolean = false;
|
||||
|
||||
public async open(): Promise<void> {
|
||||
if (this.isOpen) return;
|
||||
this.node.active = true
|
||||
this.isOpen = true;
|
||||
this.onOpen();
|
||||
}
|
||||
|
||||
static async LoadPanel(prefabPath: string, parentNode: Node) {
|
||||
|
||||
let panelNode: Node = null
|
||||
resources.load(prefabPath, Prefab, (err, prefabAsset) => {
|
||||
if (prefabAsset) {
|
||||
const node = instantiate(prefabAsset);
|
||||
node.parent = parentNode;
|
||||
node.getComponent(PanelBase).open()
|
||||
panelNode = node
|
||||
} else {
|
||||
console.error(`Failed to load UI prefab: ${prefabPath}`);
|
||||
}
|
||||
});
|
||||
|
||||
await this.waitUntil(() => panelNode != null);
|
||||
return panelNode
|
||||
}
|
||||
|
||||
|
||||
static waitUntil(condition: () => boolean): Promise<void> {
|
||||
return new Promise((resolve) => {
|
||||
const checkCondition = () => {
|
||||
if (condition()) {
|
||||
resolve(); // 条件成立,结束等待
|
||||
} else {
|
||||
// 条件不成立,继续轮询
|
||||
requestAnimationFrame(checkCondition);
|
||||
}
|
||||
};
|
||||
checkCondition(); // 开始检查条件
|
||||
});
|
||||
}
|
||||
|
||||
public close(): void {
|
||||
console.log("close panel", this.node.name)
|
||||
if (!this.isOpen) return;
|
||||
if (!this.isValid) return;
|
||||
|
||||
if (this.node && this.node.parent) {
|
||||
this.node.removeFromParent();
|
||||
this.node.destroy();
|
||||
this.isOpen = false;
|
||||
this.onClose();
|
||||
}
|
||||
}
|
||||
|
||||
protected getPrefabPath(): string {
|
||||
let panelName = js.getClassName(this)
|
||||
return `ui/${panelName}`; // 生成预制件路径
|
||||
}
|
||||
|
||||
protected onOpen(): void {
|
||||
// 子类可以重写此方法以执行打开后的操作
|
||||
}
|
||||
|
||||
protected onClose(): void {
|
||||
// 子类可以重写此方法以执行关闭后的操作
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "54c67e67-8f73-4a8c-9e7d-3b861a5aa6b8",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
import { _decorator, Component, Node, ResolutionPolicy, screen, Screen, view } from 'cc';
|
||||
import { WINDOWS } from 'cc/env';
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass('UIAdapt')
|
||||
export class UIAdapt extends Component {
|
||||
start() {
|
||||
screen.on('window-resize', this.adaptScreenSize, this);
|
||||
this.adaptScreenSize()
|
||||
let self = this
|
||||
this.scheduleOnce(() => {
|
||||
self.adaptScreenSize()
|
||||
}, 0.1)
|
||||
}
|
||||
|
||||
adaptScreenSize() {
|
||||
let screenWidth = view.getViewportRect().width
|
||||
let screenHeight = view.getViewportRect().height
|
||||
// let screenWidth = view.getVisibleSize().width;
|
||||
// let screenHeight = view.getVisibleSize().height;
|
||||
let screenRatio = screenHeight / screenWidth;
|
||||
|
||||
console.log("修改屏幕分辨率", screenRatio, view.getViewportRect(), view)
|
||||
|
||||
if (screenRatio > 18 / 9) {
|
||||
// 适配屏幕高度
|
||||
console.log("修改屏幕分辨率", "适配高度")
|
||||
|
||||
|
||||
let designWidth = 1080;
|
||||
let designHeight = screenHeight * (designWidth / screenWidth);
|
||||
|
||||
view.setDesignResolutionSize(designWidth, designHeight, ResolutionPolicy.FIXED_WIDTH);
|
||||
} else if (screenRatio > 16 / 9) {
|
||||
// 适配屏幕宽度
|
||||
console.log("修改屏幕分辨率", "适配屏幕宽度")
|
||||
let designHeight = 1920;
|
||||
let designWidth = screenWidth * (designHeight / screenHeight);
|
||||
view.setDesignResolutionSize(designWidth, designHeight, ResolutionPolicy.FIXED_HEIGHT);
|
||||
} else {
|
||||
console.log("修改屏幕分辨率", "适配屏幕宽高")
|
||||
let designWidth = 1080;
|
||||
let designHeight = screenHeight * (designWidth / screenWidth);
|
||||
view.setDesignResolutionSize(designWidth, designHeight, ResolutionPolicy.EXACT_FIT);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "48e510f8-e35c-4d1b-9af5-c70766c06f36",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
import { _decorator, Node, director, resources, instantiate, Canvas, Component, js } from 'cc';
|
||||
import { PanelBase } from './PanelBase';
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass('UIManager')
|
||||
export class UIManager extends Component {
|
||||
static instance: UIManager
|
||||
|
||||
|
||||
|
||||
private panels: Map<string, PanelBase> = new Map();
|
||||
|
||||
@property({ type: Node })
|
||||
uiRoot: Node = null;
|
||||
|
||||
onLoad() {
|
||||
UIManager.instance = this;
|
||||
}
|
||||
|
||||
|
||||
// 打开UI面板
|
||||
public async openPanel<T extends PanelBase>(panelClass: new () => T): Promise<T> {
|
||||
const panelName = js.getClassName(panelClass);
|
||||
|
||||
if (!this.panels.has(panelName)) {
|
||||
let panelNode = PanelBase.LoadPanel(`ui/${js.getClassName(panelClass)}`, this.uiRoot)
|
||||
let newPanel = (await panelNode).getComponent(PanelBase)
|
||||
this.panels.set(panelName, newPanel);
|
||||
}
|
||||
const existingPanel = this.panels.get(panelName);
|
||||
if (existingPanel) {
|
||||
existingPanel.open();
|
||||
return existingPanel as T;
|
||||
}
|
||||
|
||||
}
|
||||
// 关闭UI面板
|
||||
public closePanel<T extends PanelBase>(panelClass: new () => T): void {
|
||||
const panelName = js.getClassName(panelClass);
|
||||
if (this.panels.has(panelName)) {
|
||||
const panel = this.panels.get(panelName);
|
||||
if (panel) {
|
||||
panel.close();
|
||||
this.panels.delete(panelName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 关闭所有面板
|
||||
public closeAllPanels(): void {
|
||||
this.panels.forEach(panel => {
|
||||
console.log("关闭页面", panel.node.name)
|
||||
panel.close();
|
||||
});
|
||||
this.panels.clear();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "9f00c206-3eb4-4ff5-8144-c63b426eebc0",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
import { _decorator, Button, Component, Node, Sprite, SpriteFrame, Toggle } from 'cc';
|
||||
import { AudioMgr } from '../audio/AudioMgr';
|
||||
import { AssetController } from '../../AssetController';
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass('UISwitcher')
|
||||
export class UISwitcher extends Component {
|
||||
toggle: Button
|
||||
value: boolean
|
||||
sprite: Sprite;
|
||||
|
||||
@property({ type: SpriteFrame })
|
||||
onSelect: SpriteFrame = null;
|
||||
|
||||
@property({ type: SpriteFrame })
|
||||
onNoSelect: SpriteFrame = null;
|
||||
|
||||
|
||||
protected onLoad(): void {
|
||||
this.toggle = this.getComponent(Button)
|
||||
this.sprite = this.getComponent(Sprite)
|
||||
}
|
||||
|
||||
start() {
|
||||
|
||||
this.toggle.node.on(Button.EventType.CLICK, this.onClickToggleBtn, this);
|
||||
}
|
||||
|
||||
onSwitchCallBack
|
||||
|
||||
addCallBack(callBack) {
|
||||
this.onSwitchCallBack = callBack
|
||||
}
|
||||
|
||||
SetValue(v: boolean) {
|
||||
this.value = v
|
||||
this.switchSprite(this.value)
|
||||
}
|
||||
|
||||
switchSprite(v: boolean) {
|
||||
if (v) {
|
||||
this.sprite.spriteFrame = this.onSelect
|
||||
} else {
|
||||
this.sprite.spriteFrame = this.onNoSelect
|
||||
}
|
||||
}
|
||||
|
||||
onClickToggleBtn() {
|
||||
AudioMgr.inst.playOneShot(AssetController.instance.click)
|
||||
// AudioMgr.inst.playOneShot(AssetController.instance.buttonClip)
|
||||
this.value = !this.value
|
||||
|
||||
this.switchSprite(this.value)
|
||||
|
||||
if (this.onSwitchCallBack != null) {
|
||||
this.onSwitchCallBack(this.value)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "0167bafe-2e3a-4f24-89c3-2b7dbb48fd24",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
Reference in New Issue
Block a user