1234567891011121314151617181920212223242526272829303132333435363738394041 |
- import { _decorator, Component, Node, size, Size, view,screen, ResolutionPolicy} from 'cc';
- const { ccclass, property } = _decorator;
- const CHECK_INTERVAL = 0.1;
- @ccclass('ch.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;
- if(ratio > drsRatio){
- //wider than desgin. fixed height
- view.setResolutionPolicy(ResolutionPolicy.FIXED_HEIGHT);
- }
- else{
- //
- view.setResolutionPolicy(ResolutionPolicy.FIXED_WIDTH);
- }
- this._oldSize.set(winSize);
- }
- }
- }
|