GVKun编程网logo

Cocos2dx:3.10+cocostudio多屏幕分辨率适配解决方案(cocos creator 分帧加载)

22

对于Cocos2dx:3.10+cocostudio多屏幕分辨率适配解决方案感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍cocoscreator分帧加载,并为您提供关于cocoscodeid

对于Cocos2dx:3.10+cocostudio多屏幕分辨率适配解决方案感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍cocos creator 分帧加载,并为您提供关于cocos code ide config.json&方便查看不同屏幕分辨率适配效果、Cocos Studio 2.0以后 多分辨率适配(顶点适配)、Cocos Studio如何做分辨率适配、cocos2d-js 3.0 屏幕适配方案 分辨率适应的有用信息。

本文目录一览:

Cocos2dx:3.10+cocostudio多屏幕分辨率适配解决方案(cocos creator 分帧加载)

Cocos2dx:3.10+cocostudio多屏幕分辨率适配解决方案(cocos creator 分帧加载)

1.设计分辨率
配资源使用的分辨率大小,是1334*750。
2.屏幕分辨率
实际上用户屏幕的分辨率大小,不确定。

这里是实现代码:

//
//  FixUIUtils.h
//  MapTest
//
//  Created by Alostz on 16/4/16.
//
//

#ifndef FixUIUtils_h
#define FixUIUtils_h
#include "cocos2d.h"

using namespace cocos2d;
using namespace std;

class FixUIUtils
{
public:
    FixUIUtils();
    
    ~FixUIUtils();
    
    static void init();
    
    static void setFixScale(Node *node);
    
    static void setScaleMin(Node* node);
    
    static void setScaleMax(Node* node);
    
    static void setRootNodewithFIXED(Node* node);
    
    static void fixScene(Node* node);
    
    static void fixUI(Node* node);
    
};

#endif /* FixUIUtils_h */

//
//  FixUIUtils.cpp
//  MapTest
//
//  Created by Alostz on 16/4/16.
//
//

#include <FixUIUtils.h>
static cocos2d::Size designSize = cocos2d::Size(750,1334);
static cocos2d::Size screenSize;
static float minScale;
static float maxScale;

static float xScale;

static float yScale;

FixUIUtils::FixUIUtils(){

}

FixUIUtils::~FixUIUtils(){

}

void FixUIUtils::init(){
    screenSize = Director::getInstance()->getVisibleSize();
    xScale = designSize.width / screenSize.width;
    yScale = designSize.height / screenSize.height;
    minScale = MIN(screenSize.height/designSize.height,screenSize.width/designSize.width);
    maxScale = MAX(screenSize.height/designSize.height,screenSize.width/designSize.width);
    
    log("xScale = %f * xyScale = %f \n screenSize.width = %f * screenSize.height = %f \n",xScale,yScale,screenSize.width,screenSize.height);

}

//1、先适配layout层到屏幕大小
void FixUIUtils::setFixScale(Node *node){
    auto nodeX = node->getScaleX();
    auto nodeY = node->getScaleY();
    log("setFixScale nodeX = %f * nodeX = %f \n",nodeX,nodeY);
    nodeX = nodeX * xScale;
    nodeY = nodeY * yScale;
    
    log("setFixScale nodeX = %f * nodeX = %f \n",nodeY);
    node->setScaleX(nodeX);
    node->setScaleY(nodeY);
}

//屏幕宽、高分别和设计分辨率宽、高计算缩放因子,取较(大)者作为宽、高的缩放因子。
//适用于控件的缩放
void FixUIUtils::setScaleMax(Node *node){
    auto nodeX = node->getScaleX();
    auto nodeY = node->getScaleY();
    log("setScaleMax nodeX = %f * nodeX = %f \n",nodeY);

    nodeX = nodeX * maxScale;
    nodeY = nodeY * maxScale;
    log("setScaleMax nodeX = %f * nodeX = %f \n",nodeY);

    node->setScaleX(nodeX);
    node->setScaleY(nodeY);
}

//屏幕宽、高分别和设计分辨率宽、高计算缩放因子,取较(小)者作为宽、高的缩放因子。
//适用于背景的缩放
void FixUIUtils::setScaleMin(Node *node){
    auto nodeX = node->getScaleX();
    auto nodeY = node->getScaleY();
    log("setScaleMin nodeX = %f * nodeX = %f \n",nodeY);

    nodeX = nodeX * minScale;
    nodeY = nodeY * minScale;
    log("setScaleMin nodeX = %f * nodeX = %f \n",nodeY);

    node->setScaleX(nodeX);
    node->setScaleY(nodeY);
}


void FixUIUtils::setRootNodewithFIXED(Node* node){
    auto moveX = (designSize.width - screenSize.width);
    auto moveY = (designSize.height - screenSize.height);
    node->setPosition(Point(-moveX,-moveY));
}

void FixUIUtils::fixScene(Node* node){
    fixUI(node);
    FixUIUtils::setRootNodewithFIXED(node);
}


void FixUIUtils::fixUI(Node* node){
    Vector<Node*> ChildrenList = node->getChildren();

    for (Node* child: ChildrenList) {
        log("fixUI nodeX = %d \n",child->getTag());
        FixUIUtils::setScaleMin(child);
    }
    
}



调用:
//加载Cocos Studio编辑好的资源
    auto rootNode = csloader::createNode("test.csb");
    FixUIUtils::init();
    FixUIUtils::setFixScale(rootNode);
    FixUIUtils::fixScene(rootNode);
    this->addChild(rootNode);

cocos code ide config.json&方便查看不同屏幕分辨率适配效果

cocos code ide config.json&方便查看不同屏幕分辨率适配效果

config.json

{
    "init_cfg":{
       "isLandscape": true,"isWindowTop": false,"name": "mahjonghn","width": 1080,"height": 540,"entry": "src/main.lua","consolePort": 6050,"uploadPort": 6060
    },"simulator_screen_size": [
        {
            "title": "iPhone 3Gs (480x320)","width": 480,"height": 320
        },{
            "title": "iPhone 4 (960x640)","width": 960,"height": 640
        },{
            "title": "iPhone 5 (1136x640)","width": 1136,{
            "title": "iPad (1024x768)","width": 1024,"height": 768
        },{
            "title": "iPad Retina (2048x1536)","width": 2048,"height": 1536
        },{
            "title": "Android (800x480)","width": 800,"height": 480
        },{
            "title": "Android (854x480)","width": 854,{
            "title": "Android (1280x720)","width": 1280,"height": 720
        },{
            "title": "Android (1920x1080)","width": 1920,"height": 1080
        }
    ]
}

其中,


这个width和height调整为:

1080 * 540 小米mix2 2160*1080

960 * 640 普通手机 宽高比1.5

1024 * 768 ipad2 2048 * 1536

即可方便看到在不同手机上适配效果,再也不用担心公司没测试机啦

Cocos Studio 2.0以后 多分辨率适配(顶点适配)

Cocos Studio 2.0以后 多分辨率适配(顶点适配)

Cocos Studio 2.0以后,增加了四个图钉,用于固定与拉伸UI

如下图将一个按钮固定在距离左上角(100, 100)的位置

点击图钉,输入(100, 100),就可以看到预览中看到效果


接下来,来试验一下每一个角做一个按钮


由于我的设计分辨率为 960 * 640

所以在AppDelegate的applicationDidFinishLaunching中,加入

director->getopenGLView()->setDesignResolutionSize(960,640,ResolutionPolicy::FIXED_HEIGHT);


createWithRect设置成你想要的分辨率大小(createWithRect对移动平台无效)
在HelloWorldScene.cpp中加入

   auto rootNode = csloader::createNode("MainScene.csb");

    addChild(rootNode);
	Size size = Director::getInstance()->getVisibleSize();
	rootNode->setContentSize(size);
	cocos2d::ui::Helper::doLayout(rootNode);
运行:


可以看到角一个按钮都在与对应顶点的相对位置上

将分辨率改为1800 * 640

glview = GLViewImpl::createWithRect("CocosstudioTest",Rect(0,1800,640))


四个角仍然在对应顶点的相对位置上

Cocos Studio如何做分辨率适配

Cocos Studio如何做分辨率适配

最近使用了Cocos Studio(版本是3.10)设计了一个简单游戏,而令人头疼的是关于分辨率的设计。

在这里说一下我自己的理解与使用,网上文章看的不是太懂,自己摸索最终实现了我想要的效果,可能有误,请指教。

首先要知道两个概念,设备分辨率和设计分辨率:

1、设备分辨率:使用GLViewImpl::createWithRect("Test",Rect(0,640,960));设置模拟器的分辨率为640,960,当在手机上运行时以手机分辨率为准。

2、设计分辨率:因为是用Cocos Studio设计的,选择设备的分辨率,其实这个就是我们的设计分辨率,在代码里面设置设计分辨率:glview ->setDesignResolutionSize (640,ResolutionPolicy:: EXACT_FIT );最后根据ResolutionPolicy的几个参数来对我们的设备分辨率进行拉伸,例如,EXACT_FIT是充满可视屏幕,FIXED_HEIGHT和FIXED_WIDTH分别是往高和往宽处拉伸,SHOW_ALL只是单纯展示设计,但不填充可视屏。


总结:在Cocos Studio设计好我们的游戏布局后,再通过设置”glview ->setDesignResolutionSize (width,height,ResolutionPolicy:: EXACT_FIT );“其中width,height为Cocos Studio选择的分辨率,再后根据ResolutionPolicy选择需要的效果。

cocos2d-js 3.0 屏幕适配方案 分辨率适应

cocos2d-js 3.0 屏幕适配方案 分辨率适应

cocos2d-js 3.0 屏幕适配方案 分辨率适应

首先介绍一个api和相应的参数:

cc.view.setDesignResolutionSize(1024,768,cc.ResolutionPolicy.FIXED_WIDTH);

这里设置游戏制作的目标尺寸和显示的模式。

模式包括:

cc.ResolutionPolicy = {
    // The entire application is visible in the specified area without trying to preserve the original aspect ratio.
     distortion can occur,and the application may appear stretched or compressed.
    EXACT_FIT:0, The entire application fills the specified area,without distortion but possibly with some cropping,
     while maintaining the original aspect ratio of the application.
    NO_BORDER:1,0); line-height:1.5!important"> The entire application is visible in the specified area without distortion while maintaining the original
     aspect ratio of the application. Borders can appear on two sides of the application.
    SHOW_ALL:2,0); line-height:1.5!important"> The application takes the height of the design resolution size and modifies the width of the internal
     canvas so that it fits the aspect ratio of the device
     no distortion will occur however you must make sure your application works on different
     aspect ratios
    FIXED_HEIGHT:3,0); line-height:1.5!important"> The application takes the width of the design resolution size and modifies the height of the internal
     aspect ratios
    FIXED_WIDTH:4,UNKNowN:5
};

参考官方说明:http://www.cocos2d-x.org/wiki/Multi_resolution_support

EXACT_FIT会拉伸游戏,充满整个屏幕,最简单最粗暴;
SHOW_ALL保持游戏原比例,让一边占满屏幕,另外一侧黑边;
NO_BORDER跟SHOW_ALL类似,但让短边占满屏幕,另外一侧超出屏幕,不显示黑边,一部分画面在屏幕外,无法显示;
FIXED_WIDTH和FIXED_HEIGHT都是NO_BORDER的升级版,指定那一侧充满屏幕,另外一侧超出屏幕。

这里建议使用FIXED_WIDTH和FIXED_HEIGHT,其他用法请参考:

http://www.cocoachina.com/game/20130513/6180.html
http://www.cocoachina.com/cocos/20140516/8451.html
这两个方案适合UI沿着屏幕边缘布局,而游戏画面居中,游戏背景则可以裁剪(显示一部分)的情况。
通过上边的文章,我们知道winSize,visibleSize,designSize(自己的设计尺寸)。
无论什么方案,winSize和visibleSize是一致的,cc.director.getWinSize()和cc.director.getVisibleSize()获取到一样的数据。
以FIXED_WIDTH为例
布局过程中,横向就按照设计稿直接写死绝对坐标值都可以,因为cc.director.getVisibleSize().width就是我们的设计宽度,cocos2d通过缩放会让横向刚好占满屏幕;
而纵向,就利用cc.director.getVisibleSize().height来布局。
y=0表示刚好在屏幕边缘,在FIXED_WIDTH方案中,不像NO_BORDER会有visibleOrigin,这里不需要考虑这个值,因为总是0,cocos2d自动把y=0放到这个visibleOrigin位置了。
而屏幕上方则使用cc.director.getVisibleSize().height - 20类似的方式来布局。
这里的20也会随着整个画面的压缩比例而变小。
例如设计宽高为1024*768,但实际放到725*225的区域运行,那么cc.director.getVisibleSize()获取到的是(1024, 315)左右。实际上这个尺寸并不是真实屏幕尺寸,但可以按照这个数值进行布局。
屏幕尺寸
另外cc.view.getFrameSize可以获取屏幕尺寸

我们今天的关于Cocos2dx:3.10+cocostudio多屏幕分辨率适配解决方案cocos creator 分帧加载的分享就到这里,谢谢您的阅读,如果想了解更多关于cocos code ide config.json&方便查看不同屏幕分辨率适配效果、Cocos Studio 2.0以后 多分辨率适配(顶点适配)、Cocos Studio如何做分辨率适配、cocos2d-js 3.0 屏幕适配方案 分辨率适应的相关信息,可以在本站进行搜索。

本文标签: