添加设备
Explains how to define a new device to simulate and use it in the Device Simulator.
阅读时间12 分钟最后更新于 1 个月前
要将新设备添加到设备模拟器,请创建一个设备定义和设备覆盖层。
设备定义是 Unity 项目中扩展名为 的文本文件。它包含描述设备属性的 JSON。
.device设备覆盖层是指包含设备屏幕边框的图像,此外还包括缺口、开孔以及屏幕矩形区域外的其他任何附加部分。可以选择将其与设备定义一起使用,以可视化呈现硬件元素如何遮挡设备屏幕,并确定触控输入何时因此失效。
创建设备定义
设备定义是表示设备的 JSON 文件。它具有必要属性和一些可选属性。如果设备定义文件包含任何错误,则在选择文件时,错误将显示在检视器中。
框架
属性 | 必需 | 描述 |
|---|---|---|
| friendlyName | 是 | 要在此设备的 UI 中显示的名称。 |
| version | 是 | 指示设备定义文件的版本。目前,版本为 |
| screens | 是 | 对象列表,其中每个对象都描述了要模拟设备的屏幕。必须包含至少一个屏幕。有关每个屏幕对象的框架的信息,请参阅屏幕。 |
| systemInfo | 是 | 描述设备功能的对象。此对象中的值映射到 SystemInfo。有关 systemInfo 对象框架的信息,请参阅 systemInfo。 |
屏幕
属性 | 必需 | 描述 |
|---|---|---|
| width | 是 | 屏幕的宽度(以像素为单位)。 |
| height | 是 | 屏幕的高度(以像素为单位)。 |
| navigationBarHeight | 否 | 屏幕 Android 导航栏的高度(以像素为单位),在某些设备上非全屏显示时会出现此导航栏。 |
| dpi | 是 | 屏幕的 dpi。 |
| orientations | 否 | 对象列表,其中每个对象都描述了屏幕可以模拟的方向。如果没有为此属性设置值,则屏幕支持模拟所有方向。有关每个方向对象的框架的信息,请参阅方向。 |
| presentation | 否 | 描述设备覆盖层的对象。有关此对象的框架的信息,请参阅演示。 |
方向
属性 | 必需 | 描述 |
|---|---|---|
| orientation | 是 | 屏幕方向。此属性的值是一个映射到 ScreenOrientation 枚举的数字。 |
| safeArea | 否 | 确定屏幕安全区域的矩形。如果没有为此属性设置值,模拟器会默认整个屏幕都是安全的。 |
| Cutout | 否 | Rect 列表,指定了屏幕中无法显示内容的区域。 |
演示
属性 | 必需 | 描述 |
|---|---|---|
| overlayPath | 否 | 从设备定义文件到要用作设备覆盖层的图像的相对路径。 |
| borderSize | 否 | 从覆盖层到屏幕开始位置的距离(以像素为单位)。 |
systeminfo
此对象中的属性描述了设备的功能和系统信息。由于它们描述了系统信息,因此许多都映射到 SystemInfo 中的属性。
属性 | 必需 | 描述 |
|---|---|---|
| deviceModel | 否 | 请参阅 Device.SystemInfo.deviceModel。 |
| deviceType | 否 | 请参阅 Device.SystemInfo.deviceType。 |
| operatingSystem | 是 | 请参阅 Device.SystemInfo.operatingSystem。 |
| operatingSystemFamily | 否 | 请参阅 Device.SystemInfo.operatingSystemFamily。 |
| processorCount | 否 | 请参阅 Device.SystemInfo.processorCount。 |
| processorFrequency | 否 | 请参阅 Device.SystemInfo.processorFrequency。 |
| processorType | 否 | 请参阅 Device.SystemInfo.processorType。 |
| supportsAccelerometer | 否 | 请参阅 Device.SystemInfo.supportsAccelerometer。 |
| supportsAudio | 否 | SystemInfo.supportsAudio |
| supportsGyroscope | 否 | 请参阅 Device.SystemInfo.supportsGyroscope。 |
| supportsLocationService | 否 | 请参阅 Device.SystemInfo.supportsLocationService。 |
| supportsVibration | 否 | 请参阅 Device.SystemInfo.supportsVibration。 |
| systemMemorySize | 否 | 请参阅 Device.SystemInfo.systemMemorySize。 |
| unsupportedIdentifier | 否 | 请参阅 Device.SystemInfo.unsupportedIdentifier。 |
| graphicsDependentData | 否 | 对象列表,其中每个对象都描述了设备支持的图形 API。有关每个对象的框架的信息,请参阅 graphicsDependentData。 |
graphicsDependentData
对象中的属性描述了设备支持的图形 API。
最小设备定义
以下设备定义包含每个必要属性,没有可选属性。这是您可以拥有的最小设备定义。
注意:此设备定义不提供方向数据,因此模拟器假定设备支持所有方向,并且安全区域覆盖整个屏幕。
{ "friendlyName": "Minimal Device", "version": 1, "screens": [ { "width": 1080, "height": 1920, "dpi": 450.0 } ], "systemInfo": { "operatingSystem": "Android" }}
完整设备定义
以下设备定义包含每个必要属性和可选属性。
{ "friendlyName": "Apple iPhone XR", "version": 1, "screens": [ { "width": 828, "height": 1792, "navigationBarHeight": 0, "dpi": 326.0, "orientations": [ { "orientation": 1, "safeArea": { "serializedVersion": "2", "x": 0.0, "y": 68.0, "width": 828.0, "height": 1636.0 }, "cutouts": [ { "serializedVersion": "2", "x": 184.0, "y": 1726.0, "width": 460.0, "height": 66.0 } ] }, { "orientation": 3, "safeArea": { "serializedVersion": "2", "x": 88.0, "y": 42.0, "width": 1616.0, "height": 786.0 }, "cutouts": [ { "serializedVersion": "2", "x": 0.0, "y": 184.0, "width": 66.0, "height": 460.0 } ] }, { "orientation": 4, "safeArea": { "serializedVersion": "2", "x": 88.0, "y": 42.0, "width": 1616.0, "height": 786.0 }, "cutouts": [ { "serializedVersion": "2", "x": 1726.0, "y": 184.0, "width": 66.0, "height": 460.0 } ] } ], "presentation": { "overlayPath": "Apple iPhone 11_Overlay.png", "borderSize": { "x": 51.0, "y": 51.0, "z": 51.0, "w": 51.0 } } } ], "systemInfo": { "deviceModel": "iPhone11,8", "deviceType": 1, "operatingSystem": "iOS 12.0", "operatingSystemFamily": 0, "processorCount": 6, "processorFrequency": 0, "processorType": "arm64e", "supportsAccelerometer": true, "supportsAudio": true, "supportsGyroscope": true, "supportsLocationService": true, "supportsVibration": true, "systemMemorySize": 2813, "unsupportedIdentifier": "n/a", "graphicsDependentData": [ { "graphicsDeviceType": 16, "graphicsMemorySize": 1024, "graphicsDeviceName": "Apple A12 GPU", "graphicsDeviceVendor": "Apple", "graphicsDeviceID": 0, "graphicsDeviceVendorID": 0, "graphicsUVStartsAtTop": true, "graphicsDeviceVersion": "Metal", "graphicsShaderLevel": 50, "graphicsMultiThreaded": true, "renderingThreadingMode": 0, "hasHiddenSurfaceRemovalOnGPU": true, "hasDynamicUniformArrayIndexingInFragmentShaders": true, "supportsShadows": true, "supportsRawShadowDepthSampling": true, "supportsMotionVectors": true, "supports3DTextures": true, "supports2DArrayTextures": true, "supports3DRenderTextures": true, "supportsCubemapArrayTextures": true, "copyTextureSupport": 31, "supportsComputeShaders": true, "supportsGeometryShaders": false, "supportsTessellationShaders": true, "supportsInstancing": true, "supportsHardwareQuadTopology": false, "supports32bitsIndexBuffer": true, "supportsSparseTextures": false, "supportedRenderTargetCount": 8, "supportsSeparatedRenderTargetsBlend": true, "supportedRandomWriteTargetCount": 8, "supportsMultisampledTextures": 1, "supportsMultisampleAutoResolve": false, "supportsTextureWrapMirrorOnce": 0, "usesReversedZBuffer": true, "npotSupport": 2, "maxTextureSize": 16384, "maxCubemapSize": 16384, "maxComputeBufferInputsVertex": 8, "maxComputeBufferInputsFragment": 8, "maxComputeBufferInputsGeometry": 0, "maxComputeBufferInputsDomain": 8, "maxComputeBufferInputsHull": 8, "maxComputeBufferInputsCompute": 8, "maxComputeWorkGroupSize": 1024, "maxComputeWorkGroupSizeX": 1024, "maxComputeWorkGroupSizeY": 1024, "maxComputeWorkGroupSizeZ": 1024, "supportsAsyncCompute": false, "supportsGraphicsFence": true, "supportsAsyncGPUReadback": true, "supportsParallelPSOCreation": true, "supportsRayTracing": false, "supportsRayTracingShaders": false, "supportsInlineRayTracing": false, "supportsSetConstantBuffer": true, "hasMipMaxLevel": true, "supportsMipStreaming": true, "usesLoadStoreActions": true, "supportedTextureFormats": [1, 2, 3, 4, 5], "supportedRenderTextureFormats": [1, 2, 3, 4, 5], "ldrGraphicsFormat": 59, "hdrGraphicsFormat": 74 } ] }}
创建设备覆盖层
设备覆盖层是包含设备屏幕边框和其他功能(例如缺口、开孔以及屏幕矩形区域外的其他任何附加部分)的图像。可以选择将其与设备定义一起使用,以可视化呈现硬件元素如何遮挡设备屏幕,并确定触控输入何时因此失效。
设备模拟器将透明像素解释为可以点击的屏幕区域,而将任何其他颜色的不透明像素解释为硬件遮挡的区域。纹理本身可以是任何形状。
以下示例显示了两种 iPhone 型号的设备覆盖层。
注意:为了模拟使用设备覆盖层时看到的效果,这些示例在屏幕上可以点击的区域显示了 Unity 的默认天空盒。在实际设备覆盖层中,这些像素应该是透明的。
Apple iPhone 8 Overlay | Apple iPhone XS Overlay |
|---|---|
![]() | ![]() |
使用设备覆盖层
创建设备覆盖纹理后,要将其与设备定义一起使用,必须首先将设备覆盖纹理文件导入到项目中。
注意:当设备模拟器加载设备覆盖纹理时,设备模拟器会尝试为设备覆盖纹理启用 Read/Write。如果无法实现,则设备模拟器会显示纹理,但无法使用纹理来遮罩输入。这意味着,如果单击设备覆盖层应遮挡的屏幕缺口和其他区域,则设备模拟器会检测输入。为确保不会发生这种情况,导入设备覆盖纹理时,请在 Texture Import Settings 窗口中启用 Read/Write。
当设备覆盖纹理位于项目中时,打开设备定义文件,然后在定义了设备所支持的屏幕的对象中添加 presentation 属性。在此处,设置图像文件的路径 () 和边框的大小 ()。有关如何执行此操作的示例,请参阅以下设备定义文件:
overlayPathborderSize{ "friendlyName": "Minimal Device with Overlay", "version": 1, "screens": [ { "width": 1080, "height": 1920, "dpi": 450.0, "presentation": { "overlayPath": "Overlays/MinimalDeviceOverlay.png", "borderSize": { "x": 51.0, "y": 51.0, "z": 51.0, "w": 130.0 } } } ], "systemInfo": { "operatingSystem": "Android" }}
注意:设备覆盖纹理文件的路径可以是相对于设备定义文件的路径,也可以是相对于包含 Assets 或 Packages 目录的 Unity 项目目录的路径。例如,如果设备定义文件位于 Assets/Devices 目录中,而设备覆盖文件位于 Assets/Devices/Overlays 目录中,则以下文件路径均有效:
- 相对于设备定义文件:Overlays/MinimalDeviceOverlay.png
- 相对于包含 Assets 目录的目录:Assets/Devices/Overlays/MinimalDeviceOverlay.png

