unity3d AR 支持橫豎屏翻轉

廢話不多直接上代碼

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ARWebCamera : MonoBehaviour {

    public MeshRenderer meshRenderer;
    // Use this for initialization
    public void StartCamera(MeshRenderer _meshRenderer)
    {
        meshRenderer = _meshRenderer;
        if (meshRenderer != null)
        {
            Material mat = new Material(Shader.Find("Unlit/Transparent Colored"));
            meshRenderer.material = mat;
            meshRenderer.transform.localScale = new Vector3(Screen.width, Screen.height, 1);
            GetPermission();
            StartCoroutine(this.WebCam());
        }
    }
    public void GetPermission(int tipsID = 15301)
    {
       //獲取相機權限
    }
    private WebCamTexture webTexture = null;
    private Quaternion baseRotation = Quaternion.identity;
    IEnumerator WebCam()
    {
#if UNITY_EDITOR
        //meshRenderer.material.SetTexture("_Diff", new Texture2D(2, 2));
        meshRenderer.material.mainTexture = new Texture2D(2, 2);
#endif
        yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
        if (Application.HasUserAuthorization(UserAuthorization.WebCam))
        {
            WebCamDevice[] arr = WebCamTexture.devices;
            if (arr.Length > 0)
            {
                string deviceName = arr[0].name;
                webTexture = new WebCamTexture(deviceName, Screen.width, Screen.height, 30);
                //meshRenderer.material.SetTexture("_Diff", webTexture);
                meshRenderer.material.mainTexture = webTexture;
                //baseRotation = meshRenderer.transform.localRotation;
                webTexture.Play();
            }
        }
        //meshRenderer.material.SetTexture("_Diff", null);
    }
    void Update()
    {
        if (webTexture != null && meshRenderer != null)
        {
            meshRenderer.transform.localRotation = baseRotation * Quaternion.AngleAxis(webTexture.videoRotationAngle, Vector3.forward);
        }
    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章