12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- import { _decorator, Component, find, Node, ResolutionPolicy, screen,Size, size, UITransform, view } from 'cc';
- const { ccclass, property } = _decorator;
- const CHECK_INTERVAL = 0.1;
- @ccclass('ResolutionAutoFit')
- export class ResolutionAutoFit extends Component {
- private _oldSize:Size = size();
- start() {
- this.adjustResolutionPolicy();
- }
- private lastCheckTime = 0;
- update(deltaTime: number) {
- this.lastCheckTime+=deltaTime;
- if(this.lastCheckTime < CHECK_INTERVAL){
- return;
- }
- this.lastCheckTime = 0;
- this.adjustResolutionPolicy();
- }
- adjustResolutionPolicy(){
- let winSize = screen.windowSize;
- if(!this._oldSize.equals(winSize)){
- let ratio = winSize.width / winSize.height;
- let drs = view.getDesignResolutionSize();
- let drsRatio = drs.width / drs.height;
- let splash: Node = find("Canvas/SpriteSplash");
- if(splash) {
- splash.getComponent(UITransform).contentSize = new Size(drs.width,drs.height);
- }
- if(ratio > drsRatio){
- //wider than desgin. fixed height
- view.setResolutionPolicy(ResolutionPolicy.FIXED_HEIGHT);
- }
- else{
- //
- view.setResolutionPolicy(ResolutionPolicy.FIXED_WIDTH);
- }
- this._oldSize.set(winSize);
- }
- }
- }
|