Skip to content

Instantly share code, notes, and snippets.

@Lait-au-Cafe
Created December 30, 2017 13:29
Show Gist options
  • Save Lait-au-Cafe/5d39ad125f6dde0fdaf867b878a2cf60 to your computer and use it in GitHub Desktop.
Save Lait-au-Cafe/5d39ad125f6dde0fdaf867b878a2cf60 to your computer and use it in GitHub Desktop.
AR Background
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Background : MonoBehaviour {
public WebCamTexture wtex;
// Use this for initialization
void Start () {
if (WebCamTexture.devices.Length <= 0)
{
Debug.LogError("Cannot find a camera. ");
return;
}
this.wtex = new WebCamTexture();
wtex.Play();
//tagでUIを表示するCameraを判断
Camera cam = GameObject.FindGameObjectWithTag("BattleCamera").GetComponent<Camera>();
Canvas canvas = transform.parent.GetComponent<Canvas>();
canvas.renderMode = RenderMode.ScreenSpaceCamera;
canvas.planeDistance = cam.farClipPlane-0.01f;
canvas.worldCamera = cam;
RawImage rawImage = GetComponent<RawImage>();
rawImage.texture = wtex;
//rawImage.SetNativeSize();
RectTransform recTrans = GetComponent<RectTransform>();
//recTrans.anchoredPosition = new Vector2(0.5f, 0.5f);
float scale = ((wtex.width / wtex.height) > cam.aspect) ? ((float)cam.pixelHeight/wtex.width) : ((float)cam.pixelWidth / wtex.height);
recTrans.sizeDelta = new Vector2(scale * wtex.width, scale*wtex.height);
recTrans.Rotate(new Vector3(0, 0, -90));
wtex.Stop();
}
// Update is called once per frame
void Update () {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment