文档

Unity Engine

Unity Engine

添加设备

Explains how to define a new device to simulate and use it in the Device Simulator.
阅读时间12 分钟最后更新于 1 个月前

要将新设备添加到设备模拟器,请创建一个设备定义和设备覆盖层。
设备定义是 Unity 项目中扩展名为
.device
的文本文件。它包含描述设备属性的 JSON。
设备覆盖层是指包含设备屏幕边框的图像,此外还包括缺口、开孔以及屏幕矩形区域外的其他任何附加部分。可以选择将其与设备定义一起使用,以可视化呈现硬件元素如何遮挡设备屏幕,并确定触控输入何时因此失效。

创建设备定义

设备定义是表示设备的 JSON 文件。它具有必要属性和一些可选属性。如果设备定义文件包含任何错误,则在选择文件时,错误将显示在检视器中。

框架

属性

必需

描述

friendlyName是要在此设备的 UI 中显示的名称。
version是指示设备定义文件的版本。目前,版本为
1
。
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。

属性

必需

描述

graphicsDeviceType是请参阅 Device.SystemInfo.graphicsDeviceType。
graphicsMemorySize否请参阅 Device.SystemInfo.graphicsMemorySize。
graphicsDeviceName否请参阅 Device.SystemInfo.graphicsDeviceName。
graphicsDeviceVendor否请参阅 Device.SystemInfo.graphicsDeviceVendor。
graphicsDeviceID否请参阅 Device.SystemInfo.graphicsDeviceID。
graphicsDeviceVendorID否请参阅 Device.SystemInfo.graphicsDeviceVendorID。
graphicsUVStartsAtTop否请参阅 Device.SystemInfo.graphicsUVStartsAtTop。
graphicsDeviceVersion否请参阅 Device.SystemInfo.graphicsDeviceVersion。
graphicsShaderLevel否请参阅 Device.SystemInfo.graphicsShaderLevel。
graphicsMultiThreaded否请参阅 Device.SystemInfo.graphicsMultiThreaded。
renderingThreadingMode否请参阅 Device.SystemInfo.renderingThreadingMode。
hasHiddenSurfaceRemovalOnGPU否请参阅 Device.SystemInfo.hasHiddenSurfaceRemovalOnGPU。
hasDynamicUniformArrayIndexingInFragmentShaders否请参阅 Device.SystemInfo.hasDynamicUniformArrayIndexingInFragmentShaders。
supportsShadows否请参阅 Device.SystemInfo.supportsShadows。
supportsRawShadowDepthSampling否请参阅 Device.SystemInfo.supportsRawShadowDepthSampling。
supportsMotionVectors否请参阅 Device.SystemInfo.supportsMotionVectors。
supports3DTextures否请参阅 Device.SystemInfo.supports3DTextures。
supports2DArrayTextures否请参阅 Device.SystemInfo.supports2DArrayTextures。
supports3DRenderTextures否请参阅 Device.SystemInfo.supports3DRenderTextures。
supportsCubemapArrayTextures否请参阅 Device.SystemInfo.supportsCubemapArrayTextures。
copyTextureSupport否请参阅 Device.SystemInfo.copyTextureSupport。
supportsComputeShaders否请参阅 Device.SystemInfo.supportsComputeShaders。
supportsGeometryShaders否请参阅 Device.SystemInfo.supportsGeometryShaders。
supportsTessellationShaders否请参阅 Device.SystemInfo.supportsTessellationShaders。
supportsInstancing否请参阅 Device.SystemInfo.supportsInstancing。
supportsHardwareQuadTopology否请参阅 Device.SystemInfo.supportsHardwareQuadTopology。
supports32bitsIndexBuffer否请参阅 Device.SystemInfo.supports32bitsIndexBuffer。
supportsSparseTextures否请参阅 Device.SystemInfo.supportsSparseTextures。
supportedRenderTargetCount否请参阅 Device.SystemInfo.supportedRenderTargetCount。
supportsSeparatedRenderTargetsBlend否请参阅 Device.SystemInfo.supportsSeparatedRenderTargetsBlend。
supportedRandomWriteTargetCount否请参阅 Device.SystemInfo.supportedRandomWriteTargetCount。
supportsMultisampledTextures否请参阅 Device.SystemInfo.supportsMultisampledTextures。
supportsMultisampleAutoResolve否请参阅 Device.SystemInfo.supportsMultisampleAutoResolve。
supportsTextureWrapMirrorOnce否请参阅 Device.SystemInfo.supportsTextureWrapMirrorOnce。
usesReversedZBuffer否请参阅 Device.SystemInfo.usesReversedZBuffer。
npotSupport否请参阅 Device.SystemInfo.npotSupport。
maxTextureSize否请参阅 Device.SystemInfo.maxTextureSize。
maxCubemapSize否请参阅 Device.SystemInfo.maxCubemapSize。
maxComputeBufferInputsVertex否请参阅 Device.SystemInfo.maxComputeBufferInputsVertex。
maxComputeBufferInputsFragment否请参阅 Device.SystemInfo.maxComputeBufferInputsFragment。
maxComputeBufferInputsGeometry否请参阅 Device.SystemInfo.maxComputeBufferInputsGeometry。
maxComputeBufferInputsDomain否请参阅 Device.SystemInfo.maxComputeBufferInputsDomain。
maxComputeBufferInputsHull否请参阅 Device.SystemInfo.maxComputeBufferInputsHull。
maxComputeBufferInputsCompute否请参阅 Device.SystemInfo.maxComputeBufferInputsCompute。
maxComputeWorkGroupSize否请参阅 Device.SystemInfo.maxComputeWorkGroupSize。
maxComputeWorkGroupSizeX否请参阅 Device.SystemInfo.maxComputeWorkGroupSizeX。
maxComputeWorkGroupSizeY否请参阅 Device.SystemInfo.maxComputeWorkGroupSizeY。
maxComputeWorkGroupSizeZ否请参阅 Device.SystemInfo.maxComputeWorkGroupSizeZ。
supportsAsyncCompute否请参阅 Device.SystemInfo.supportsAsyncCompute。
supportsGraphicsFence否请参阅 Device.SystemInfo.supportsGraphicsFence。
supportsAsyncGPUReadback否请参阅 Device.SystemInfo.supportsAsyncGPUReadback。
supportsParallelPSOCreation否请参阅 Device.SystemInfo.supportsParallelPSOCreation。
supportsRayTracing否请参阅 Device.SystemInfo.supportsRayTracing。
supportsRayTracingShaders否请参阅 Device.SystemInfo.supportsRayTracingShaders。
supportsInlineRayTracing否请参阅 Device.SystemInfo.supportsInlineRayTracing。
supportsSetConstantBuffer否请参阅 Device.SystemInfo.supportsSetConstantBuffer。
hasMipMaxLevel否请参阅 Device.SystemInfo.hasMipMaxLevel。
supportsMipStreaming否请参阅 Device.SystemInfo.supportsMipStreaming。
usesLoadStoreActions否请参阅 Device.SystemInfo.usesLoadStoreActions。

最小设备定义

以下设备定义包含每个必要属性,没有可选属性。这是您可以拥有的最小设备定义。
注意:此设备定义不提供方向数据,因此模拟器假定设备支持所有方向,并且安全区域覆盖整个屏幕。
{ "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 属性。在此处,设置图像文件的路径 (
overlayPath
) 和边框的大小 (
borderSize
)。有关如何执行此操作的示例,请参阅以下设备定义文件:
{ "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