zoukankan      html  css  js  c++  java
  • 调用摄像头并将其显示在UGUI image上自适应屏幕大小

    参考链接:http://www.cnblogs.com/Erma-king/p/5869177.html

    不过该博主是竖屏,我的是横屏

    代码修改:

    using UnityEngine;
    using System.Collections;
    using UnityEngine.UI;
    /// <summary>
    /// 调用摄像机并自适应大小的脚本
    /// </summary>
    public class STCamDeviceController : MonoBehaviour
    {
    
        WebCamTexture camTexture;
        CanvasScaler CanScaler;
        Camera ca;
        Image img;
    
        void Start()
        {
    
            img = GetComponentInChildren<Image>();
    
            CanScaler = GetComponentInChildren<CanvasScaler>();
            CanScaler.referenceResolution = new Vector2(Screen.width, Screen.height);
    
            ca = GetComponentInChildren<Camera>();
            ca.orthographicSize = Screen.width / 100.0f / 2.0f;
    
            //img.transform.localScale = new Vector3(-1, -1, -1);
    
            img.rectTransform.anchorMin = new Vector2(0.5f, 0.5f);
            img.rectTransform.anchorMax = new Vector2(0.5f, 0.5f);
            img.rectTransform.pivot = new Vector2(0.5f, 0.5f);
    
            img.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, Screen.width);
            img.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, Screen.height);
    
            // 设备不同的坐标转换
    #if UNITY_IOS || UNITY_IPHONE
            img.transform.Rotate (new Vector3 (0, 180, 90));
    #elif UNITY_ANDROID
            img.transform.Rotate (new Vector3 (0, 0, 90));
    #endif
    
            StartCoroutine(CallCamera());
        }
    
        IEnumerator CallCamera()
        {
            yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
            if (Application.HasUserAuthorization(UserAuthorization.WebCam))
            {
                if (camTexture != null)
                    camTexture.Stop();
    
                WebCamDevice[] cameraDevices = WebCamTexture.devices;
                string deviceName = cameraDevices[0].name;
    
                camTexture = new WebCamTexture(deviceName, Screen.height, Screen.width, 60);
                img.canvasRenderer.SetTexture(camTexture);
    
                camTexture.Play();
            }
        }
    }
  • 相关阅读:
    MySQL-第十四篇事务管理
    MySQL-第十三篇使用ResultSetMetaData分析结果集
    MySQL-第十二篇管理结果集
    MySQL-第十一篇JDBC典型用法
    MySQL-第十篇多表连接查询
    Java中List集合去除重复数据的方法1
    去除List集合中的重复值(四种好用的方法)(基本数据类型可用)
    去除list集合中重复项的几种方法
    Java中List集合去除重复数据的四种方法
    Java中List集合去除重复数据的方法
  • 原文地址:https://www.cnblogs.com/dsh20134584/p/7596990.html
Copyright © 2011-2022 走看看