浏览代码

Merge pull request #74 from Unity-Technologies/camera_config

Add a sample for the camera configuration API
/1.5-preview
GitHub 6 年前
当前提交
828c45a3
共有 4 个文件被更改,包括 885 次插入43 次删除
  1. 811
      Assets/Scenes/CameraImage.unity
  2. 17
      Assets/Scripts/TestCameraImage.cs
  3. 89
      Assets/Scripts/CameraConfigController.cs
  4. 11
      Assets/Scripts/CameraConfigController.cs.meta

811
Assets/Scenes/CameraImage.unity
文件差异内容过多而无法显示
查看文件

17
Assets/Scripts/TestCameraImage.cs


// See CameraImage.FormatSupported for a complete list of supported formats.
var format = TextureFormat.RGBA32;
if (m_Texture == null)
if (m_Texture == null || m_Texture.width != image.width || m_Texture.height != image.height)
m_Texture = new Texture2D(image.width, image.height, format, false);
// Convert the image to format, flipping the image across the Y axis.

// Texture2D allows us write directly to the raw texture data
// This allows us to do the conversion in-place without making any copies.
var rawTextureData = m_Texture.GetRawTextureData<byte>();
image.Convert(conversionParams, new IntPtr(rawTextureData.GetUnsafePtr()), rawTextureData.Length);
// We must dispose of the CameraImage after we're finished
// with it to avoid leaking native resources.
image.Dispose();
try
{
image.Convert(conversionParams, new IntPtr(rawTextureData.GetUnsafePtr()), rawTextureData.Length);
}
finally
{
// We must dispose of the CameraImage after we're finished
// with it to avoid leaking native resources.
image.Dispose();
}
// Apply the updated texture data to our texture
m_Texture.Apply();

89
Assets/Scripts/CameraConfigController.cs


using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.XR.ARExtensions;
using UnityEngine.XR.ARFoundation;
/// <summary>
/// Populates a drop down UI element with all the supported
/// camera configurations and changes the active camera
/// configuration when the user changes the selection in the dropdown.
///
/// The camera configuration affects the resolution (and possibly framerate)
/// of the hardware camera during an AR session.
/// </summary>
[RequireComponent(typeof(Dropdown))]
public class CameraConfigController : MonoBehaviour
{
List<string> m_ConfigurationNames;
Dropdown m_Dropdown;
/// <summary>
/// Callback invoked when <see cref="m_Dropdown"/> changes. This
/// lets us change the camera configuration when the user changes
/// the selection in the UI.
/// </summary>
/// <param name="dropdown">The <c>Dropdown</c> which changed.</param>
public void OnValueChanged(Dropdown dropdown)
{
var cameraSubsystem = ARSubsystemManager.cameraSubsystem;
if (cameraSubsystem == null)
return;
var configurationIndex = dropdown.value;
// Check that the value makes sense
var configurations = cameraSubsystem.Configurations();
if (configurationIndex >= configurations.count)
return;
// Get that configuration by index
var configuration = configurations[configurationIndex];
// Make it the active one
cameraSubsystem.SetCurrentConfiguration(configuration);
}
void Awake()
{
m_Dropdown = GetComponent<Dropdown>();
m_Dropdown.ClearOptions();
m_ConfigurationNames = new List<string>();
}
void PopulateDropdown()
{
var cameraSubsystem = ARSubsystemManager.cameraSubsystem;
if (cameraSubsystem == null)
return;
// No configurations available probably means this feature
// isn't supported by the current device.
var configurations = cameraSubsystem.Configurations();
if (configurations.count == 0)
return;
// There are two ways to enumerate the camera configurations.
// 1. Use a foreach to iterate over all the available configurations
foreach (var config in configurations)
m_ConfigurationNames.Add(config.ToString());
m_Dropdown.AddOptions(m_ConfigurationNames);
// 2. Use a normal for...loop
var currentConfig = cameraSubsystem.GetCurrentConfiguration();
for (int i = 0; i < configurations.count; i++)
{
// Find the current configuration and update the drop down value
if (currentConfig == configurations[i])
m_Dropdown.value = i;
}
}
void Update()
{
if (m_ConfigurationNames.Count == 0)
PopulateDropdown();
}
}

11
Assets/Scripts/CameraConfigController.cs.meta


fileFormatVersion: 2
guid: 94b653a1faa6f4749ad83e77bcddfab4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存