Multi-resolution support

本文介绍Cocos2d-x中多分辨率支持的基本原理和实现方式,包括设计分辨率大小、内容缩放因子的概念及不同策略的应用场景,如Exact fit、No border等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Multi-resolution support

The APIs used in this document are available since cocos2d-2.0-x-2.0.4.

The diverse set of resolutions on Android devices is hard to adapt to. Cocos2d-x offers CCEGLView::setDesignResolutionSize() and CCDirector::setContentScaleFactor() to help your game run on different resolutions with minimal amount of work.

The principle

We have removed all the code related to enableRetina method since v2.0.4. On iOS, if the device supports Retina display, we enable it by default.
You can get the real screen size in pixels with CCEGLView::sharedOpenGLView()->getFrameSize(). For example, the function returns ‘960x640’ for iPhone 4S in landscape mode.

But how does one use non-Retina coordinates for Retina devices? There are two concepts you have to know. One is designResolutionSize; the other is contentScaleFactor.

designResolutionSize

All of your game’s coordinates are based on design resolution, no matter what the size of your device screen. If the UI layout of your game is the same on all resolutions, you only need a single set of coordinates. Design resolution is set using CCEGLView::sharedOpenGLView()->setDesignResolutionSize(width, height, policy). The first and second parameters are the width and height of design resolution, and the third parameter is the resizing policy you want to use. I will explain the third parameter later.

Additionally, you can use more than one set of resources for different devices, to make the best use of each device’s display. This is achieved by using searchPath.push_back(largeResource.directory);, in combination with contentScaleFactor.

Here are some code snippets in HelloCpp project.


 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
    typedef struct tagResource
    {
        cocos2d::CCSize size;
        char directory[100];
    }Resource;

    static Resource smallResource  =  { cocos2d::CCSizeMake(480, 320),   "iphone" };
    static Resource mediumResource =  { cocos2d::CCSizeMake(1024, 768),  "ipad"   };
    static Resource largeResource  =  { cocos2d::CCSizeMake(2048, 1536), "ipadhd" };
    static cocos2d::CCSize designResolutionSize = cocos2d::CCSizeMake(480, 320);

    bool AppDelegate::applicationDidFinishLaunching() {
        // initialize director
        CCDirector* pDirector = CCDirector::sharedDirector();
        CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();

        pDirector->setOpenGLView(pEGLView);

        // Set the design resolution
        pEGLView->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, kResolutionNoBorder);

        CCSize frameSize = pEGLView->getFrameSize();

        // In this demo, we select resource according to the frame's height.
        // If the resource size is different from design resolution size, you need to set contentScaleFactor.
        // We use the ratio of resource's height to the height of design resolution,
        // this can make sure that the resource's height could fit for the height of design resolution.

        // if the frame's height is larger than the height of medium resource size, select large resource.
        if (frameSize.height > mediumResource.size.height)
        { 
            searchPath.push_back(largeResource.directory);
            pDirector->setContentScaleFactor(largeResource.size.height/designResolutionSize.height);
        }
        // if the frame's height is larger than the height of small resource size, select medium resource.
        else if (frameSize.height > smallResource.size.height)
        { 
            searchPath.push_back(mediumResource.directory);
            pDirector->setContentScaleFactor(mediumResource.size.height/designResolutionSize.height);
        }
        // if the frame's height is smaller than the height of medium resource size, select small resource.
        else
        { 
            searchPath.push_back(smallResource.directory);
            pDirector->setContentScaleFactor(smallResource.size.height/designResolutionSize.height);
        }
        ...................
        ...................
    }

contentScaleFactor

ContentScaleFactor describes the ratio from ResourcesSize to designResolutionSize. Normally, it can be set by ‘ResourceBackGround.height/DesignResolution.height’ or ‘ResourceBackGround.width/DesignResolution.width’. To choose which one is based on your game design.
For illustration below, we use height to calculate the factor.

Chart 1: Resource Size = 960x640 ; Design Resolution Size = 480x320 ; Target Device Screen Size = 800x480 ; RH/DH=RW/DW=2.0f; NoBorder Policy

When using NoBorder mode, there are some areas of the backgroud out of scope. If you use absolute coordinates based on design resolution size(480x320),
some of your game UI will not be shown completely. To resolve this issue, you have to make the coordinates base on ‘visible rectangle’. You can get the origin point of ‘visible rectange’ by ‘CCDirector::sharedDirector()->getVisibleOrign()’.
Together with ‘CCDirector::sharedDirector()->getVisibleSize()’,you will be able to confirm 9 points on the screen. They are ‘Left’,‘Right’, ‘Top’,‘Bottom’,‘Center’,‘LeftTop’,‘RightTop’,‘LeftBottom’ and ‘RightBottom’.
Your game will be really full screen display if all the coordinates of your game is base on these nine points.
About how to calculate these points, please refer to ‘VisibleRect’ class in TestCpp project.


Chart 2: *Resource Size = 1024x768 ; Design Resolution Size = 480x320 ; Target Device Screen Size = 800x480 ; RH/DH ![](= RW/DW; NoBorder Policy_

When RH/DH isn't equal to RW/RH, You should decide to select Width or Height ratio of resource to design resolution.
In the chart 2, we still use height ratio to calculate contentScaleFator, so the height of the resource background will fill for the height of design resolution.
Mark① shows two black rectangle will be in design resolution.
After mapping design resolution to screen, Mark① --> Mark②, and Mark③ will be out of screen.
Now, you have two choices to make your game becomes full screen. One is to make your background picture wider.
Another is to set the contentScaleFactor based on the ratio of width.

Policies

Now cocos2d-x supports five different policies.

Exact fit

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.

No border

The entire application fills the specified area, without distortion but possibly with some cropping, while maintaining the original aspect ratio of the application.

Show all

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.

Chart 3:*Resource Size = 1024x768 ; Design Resolution Size = 480x320 ; Target Device Screen Size = 800x480 ; RH/DH != RW/DW; ShowAll Policy_

Mark② and Mark③ are both black rectangle areas. But they are different, Mark③ is out of Opengl Viewport, so you can’t put any game elements onto it.
Mark② appears because RW/RH(1024÷768=1.333) isn’t equal to DW/DH(480÷320=1.5), it’s in the Opengl Viewport, you can place your game elements onto it.

Fixed Height

The width parameter of the design resolution size is ignored and recalculated based on the height of the design resolution and the aspect ratio of the device.
The entire application is visible without distortion. However developers must make sure that they design their UI to match different aspect ratios.
Using this resolution policy will give you a different window width across devices, the window height is fixed. The visibility rect will start at 0/0.

Example:
Device Resolution: 800x480px
Design Resolution: 480x320px
CCDirector Window Origin: 0/0
CCDirector Window Size: 534x320px

Device Resolution: 854x480px
Design Resolution: 480x320px
CCDirector Window Origin: 0/0
CCDirector Window Size: 570x320px

Device Resolution: 1024x768px
Design Resolution: 480x320px
CCDirector Window Origin: 0/0
CCDirector Window Size: 427x320px

As you can see the height of the display stays the same on all possible device resolutions, only the window width will be adjusted to match the device aspect ratio.
In general you can use this mode like the No Boder mode, but there is no need to use VisibleRect methods. If you place a sprite at (0/0) it will always
be in the lower left corner of the screen, If you place the sprite at (winSize.width/2, winSize.height/2) it will be in the center of the screen.
If you place the spite at (winSize.width, 0) it will be at the bottom-right corner of the screen.

Avaiable since cocos2d-x v2.1rc0-x-2.1.3

Fixed Width

This mode works the same as the fixed height mode. The difference is, that this mode will ignore the height of the design resolution size and recalcualte the height depending
on the device aspect ratio.
While the fixed height mode is useful for landscape games, you may want to prefer this resolution policy for portrait games.

Avaiable since cocos2d-x v2.1rc0-x-2.1.3

  • You need to use relative coordinates only in NoBorder, FixedHeight and FixedWidth mode. Generally, developer uses NoBorder mode for better display
<think>好的,我现在需要帮助用户了解如何在Unity中实现他们提到的“Touchscreen collaboration on large displays”项目。首先,用户之前已经询问过基于Unity的一般性介绍,现在他们有一个具体的项目需求,需要指导学习哪些技术。 首先,我得仔细阅读用户提供的项目描述。项目重点是在大屏幕上设计协作解决方案,使用三个电视大小的触摸屏和一个用于总结的墙显。目标是增强团队协作、生产力和沟通,利用多用户交互的大型触摸界面。学生将在Catalyst Decision Theatre中使用独特的硬件设置开发协作体验,鼓励提出自己的应用场景。 接下来,我需要确定用户可能需要哪些Unity相关的技术。首先,Unity本身支持多平台,但触摸屏协作可能涉及多点触控输入。因此,用户需要学习Unity的输入系统,特别是多点触控的处理。可能需要使用Unity的新输入系统,因为它更灵活,支持多设备输入。 然后是跨屏幕协作,这涉及到多显示器设置。Unity默认支持多摄像头视图,但如何同步不同屏幕的内容可能需要额外的设置。可能需要学习如何配置多摄像头以及屏幕之间的数据同步,比如使用网络功能或共享数据存储。 多用户交互部分,可能需要网络功能来实现不同用户之间的数据同步和互动。Unity的Netcode或第三方解决方案如Photon可能适用。不过,如果所有用户都在同一局域网内,可能不需要复杂的网络设置,而是使用本地网络或共享输入处理。 UI设计方面,大屏幕的UI需要适配不同分辨率和触摸操作。用户需要学习Canvas的适配模式,比如使用锚点和缩放模式,以及触摸事件的响应,比如按钮、滑动等交互元素。 硬件集成部分,Catalyst Decision Theatre可能有特定的硬件配置,比如多个显示器的连接方式。用户需要学习如何在Unity中设置多显示器输出,可能需要编写脚本控制不同相机的渲染目标。 性能优化也是关键,大屏幕和高分辨率对渲染压力大。需要学习优化技术,比如遮挡剔除、LOD、GPU Instancing等,以及减少Draw Calls的方法,比如合并网格或使用更高效的着色器。 数据同步和协作逻辑方面,可能需要使用Unity的Scriptable Objects或事件系统来处理实时数据更新,或者利用JSON、XML进行数据存储和传输。 扩展性方面,用户可能需要设计模块化的架构,比如使用MVC模式,方便替换不同场景或功能。版本控制工具如Git也是必须的,尤其是团队协作时。 最后,用户可能需要学习相关案例和资源,参考Unity官方文档和社区案例,加速开发进程。同时,硬件测试也很重要,确保软件与硬件兼容,处理触摸精度和延迟问题。 总结下来,用户需要掌握的技术点包括:多点触控输入处理、多显示器配置、UI适配、网络同步、性能优化、硬件集成、数据同步逻辑、模块化设计等。需要逐步引导用户学习这些方面,并提供资源推荐,如Unity Learn平台、官方手册、Photon文档等。还要提醒用户注意实际测试,确保触摸响应和跨屏协作的流畅性。</think>若基于Unity开发“**大屏幕触屏协作**”项目(涉及多用户、多触摸屏交互与数据同步),需掌握以下核心技术点: --- ### **1. Unity基础技术** - **多点触控输入处理** - 学习Unity的`Input System`(新输入系统),支持多指触控、手势识别(如滑动、缩放)。 - 代码示例:监听多指触摸事件 ```csharp public class TouchHandler : MonoBehaviour { void Update() { foreach (Touch touch in Input.touches) { if (touch.phase == TouchPhase.Moved) { // 根据touch.fingerId区分不同用户操作 } } } } ``` - **跨屏幕协作逻辑** - 配置多摄像头(`Camera`)分别渲染不同屏幕内容,需设置`targetDisplay`属性。 - 同步不同屏幕间的数据(如共享画布、实时数据更新),可通过`Unity Netcode`或`Photon`实现局域网/互联网通信。 --- ### **2. 多用户交互设计** - **用户身份区分** - 为每个触控设备或用户分配唯一ID,通过触摸点位置或设备IP映射用户操作区域。 - **协作逻辑实现** - 使用事件系统(`Event System`)或脚本通信(如`UnityEvent`)传递用户操作事件。 - 示例:共享画布中多人同时绘制时,需同步笔触坐标和颜色数据。 --- ### **3. 大屏UI适配与性能优化** - **UI适配** - 使用`Canvas`的`Scale With Screen Size`模式,结合锚点(Anchors)适配不同分辨率。 - 针对触控优化UI元素(如按钮大小、滑动灵敏度)。 - **渲染优化** - 启用动态批处理(Dynamic Batching)、遮挡剔除(Occlusion Culling)减少GPU负载。 - 对大屏内容分块加载(如`Addressables`资源管理系统)。 --- ### **4. 硬件集成与测试** - **多显示器配置** - 在Unity Editor中通过`Player Settings > Resolution and Presentation`设置多屏输出。 - 代码控制多显示器激活: ```csharp // 激活第二个屏幕(需硬件支持) if (Display.displays.Length > 1) Display.displays[1].Activate(); ``` - **触控硬件兼容性** - 测试不同厂商的触摸屏协议(如TUIO协议),必要时编写自定义输入驱动。 --- ### **5. 数据同步与存储** - **实时同步方案** - 局域网内使用`UNet`或`Mirror`网络框架;广域网可选`Photon Fusion`或`Fish-Networking`。 - **数据持久化** - 使用`JSON`或`SQLite`存储协作结果,支持导出到墙显总结界面。 --- ### **6. 推荐学习路径** 1. **Unity基础**:官方教程《Unity Essentials》掌握场景编辑、脚本编写。 2. **输入系统**:学习《Input System Workshop》处理多设备输入。 3. **多屏与网络**:参考《Unity Multiplayer Networking》实现跨屏通信。 4. **性能优化**:阅读《Unity Profiler手册》分析渲染瓶颈。 5. **硬件对接**:研究Unity官方文档《Multiple Displays Support》。 --- ### **7. 工具与资源推荐** - **插件**: - TouchScript(高级手势库) - Zenject(依赖注入,管理多用户逻辑) - **案例参考**: - Unity官方案例《Big Screen Multiplayer》 - GitHub开源项目“Collaborative Whiteboard” --- 通过以上技术栈,可实现多用户在大屏触控设备上的实时协作,最终构建类似“数字决策沙盘”的高效交互环境。建议从单屏原型开始迭代,逐步扩展至多屏联动。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值