[ Android 에서 Unity App이 구동될 수 있도록 환경 구성하기 ]
https://sites.google.com/site/unity3dstudy/home/test2/smartphone/-androidgaebalhwangyeong-guseong
[ 안드로이드(Android) SDK 설치 하기 & Unity 연동 ]
[ Google VR SDK Download ]
본인 프로젝트에 Package Import
Project View : GoogleVR >> Prefabs : GvrEditorEmulator 드래그 >>(to) Hierarchy
Play 버튼을 누른 후 Alt 키를 눌러서 주위를 둘러 봄
[ 360 실사 영상 넣어 보기 ]
원하는 동영상 2개 다운 받기
동영상 파일 드래그 >> (to) Project View 에 넣기
[ 스크린 만들기 ]
구(Sphere) 스크린 만들기
GameObject >> 3D Object >> Sphere
Video Player (Component) 추가
Sphere : Inspector >> Add Component 클릭
Video Player 삽입
동영상 파일 드래그 >> (to) Video Player Component : Video Clip
Play On Awake 체크 / Wait For First Frame 체크 / Loop 체크
[ Video Player 참고 URL ]
Play 버튼
Sphere 겉표면에 영상 재생 확인
[ Sphere 안쪽에 영상이 재생될 수 있도록 하기 ]
셰이더(Shader) 추가
Assets >> Create >> Shader >> Standard Surfcae Shader
Shader "Custom/Insideout" {
Properties {
_MainTex ("Albedo (RGB)", 2D) = "white" {}
}
SubShader {
Tags { "RenderType"="Opaque" }
Cull front // ADDED BY BERNIE, TO FLIP THE SURFACES
LOD 100
Pass{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata_t
{
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
};
struct v2f {
float4 vertex : SV_POSITION;
half2 texcoord : TEXCOORD0;
};
sampler2D _MainTex;
float4 _MainTex_ST;
v2f vert(appdata_t v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
// ADDED BY BERNIE:
v.texcoord.x = 1 - v.texcoord.x;
o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.texcoord);
return col;
}
ENDCG
}
}
}
메테리얼(Material) 추가
Assets >> Create >> Material
Inspector 창의 Shader Custom/Insideout 선택
구(Sphere)에 메테리얼(Material) 추가
메테리얼(Material) 드래그 >> (to) Sphere에 넣기
카메라(Main Camera)와 구(Sphere)의 위치 이동
Camera & Sphere를 (0, 0, 0)으로 이동
문제점
시야각이 좁음
좌우 반전
Sphere Scale (-100, 100, 100)으로 변경
x 좌표 크기가 음수로 좌우 반전 문제 해결
Sphere 크기가 증가 함으로써 시야각 문제 해결
[ Android EScape 키 사용하기 ]
안드로이드에서 제공하는 Escape 버튼을 사용해서 App 종료하기
GameObject >> Create Empty
Name : GameController
Project >> Assets>> Script 폴더 우클릭 >> Create >> C# Script
Name : GameControl
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameControl : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update()
{
if(Application.platform == RuntimePlatform.Android)
{
if(Input.GetKey(KeyCode.Escape))
{
//
Application.Quit();
}
}
}
}
GameControl(Script) =>(드래그) GameController(게임오브젝트)
Prefab으로만들기
Prefabs 폴더로 옮기기
[카메라 시점을 통한 버튼 상호작용 할 수 있도록 하기]
카메라에 레이케스트(Raycast) 스크립트 추가
Assets >> Create >> C# Script
Name : raycast
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class raycast : MonoBehaviour
{
// Update is called once per frame
private void Update()
{
RaycastHit hit;
Vector3 forward = transform.TransformDirection(Vector3.forward) * 1000;
Debug.DrawRay(transform.position, forward, Color.green);
if(Physics.Raycast(transform.position, forward, out hit))
{
hit.transform.GetComponent<Button>().onClick.Invoke();
Debug.Log("hit");
}
}
}
작성된 스크립트를 드래그 >> (to) Main Camera
상호작용을 위한 버튼(Button) 생성
GameObject >> UI >> Button
캔버스(Canvas), 버튼(Button)이 생성되는 것을 확인
캔버스(Canvas) Object Setting
Inspector 창의 Canvas 컴포넌트의 Render Mode >> World Space 변경
버튼(Button) Object Setting
Position : (15, -10, 30)
Rotation : (0, 45, 0)
Scale : (0.1, 0.1, 0.1)
Button 자식 컴포넌트의 Text를 Change Scene to 01로 변경
비디오(Video)를 변경할 스크립트 추가
Assets >> Create >> C# Script
Name : changeVid
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video; // Add Video api
public class changeVid : MonoBehaviour
{
public VideoClip clip01;
public VideoClip clip02;
bool isClip;
public void change()
{
if(!isClip)
{
gameObject.GetComponent<VideoPlayer>().clip = clip02;
isClip = true;
}
else
{
gameObject.GetComponent<VideoPlayer>().clip = clip01;
isClip = false;
}
}
}
작성된 스크립트를 드래그 >> (to) Sphere
Inspector창의 ChanveVid 컴포넌트 Clip01, Clip02 변수에 360도 영상을 각각 삽입
버튼(Button)에 상호작용 기능 추가
Button (Script) 컴포넌트의 OnClick() 항목 확인 + 버튼으로 항목 추가
Sphere Object 드래그 >> (to) OnClick Component
선택 항목에서 ChangeVid >> change() 함수 선택
버튼(Button) 오브젝트에 충돌체 컴포넌트 추가
Inspector 창에 Add Component >> Box Collider 추가
Box Collider Size를 (160, 30, 1)로 변경 (버튼의 사이즈와 동일하게)
두번째 버튼(Button) 추가
Hierarchy 창에서 Button을 선택한 후 Ctrl + D를 누르면 컴포넌트를 추가된 채로 복제
Button(1) Object Setting
Position : (15, -5, 30)
Button 자식 컴포넌트의 Text를 Change Scene to 02로 변경
문제점
두 개의 버튼(Button)이 하나의 change()기능을 사용하기 때문에 프레임당 change() 함수 발생
방법 : change, change02로 구분하여 역할을 분배
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video; // Add Video api
public class changeVid : MonoBehaviour
{
public VideoClip clip01;
public VideoClip clip02;
bool isClip;
public void change()
{
if(!isClip)
{
gameObject.GetComponent<VideoPlayer>().clip = clip02;
isClip = true;
}
}
public void change02()
{
if(isClip)
{
gameObject.GetComponent<VideoPlayer>().clip = clip01;
isClip = false;
}
}
}
Button(1) Object의 Button (script) 컴포넌트 변경
ChangeVid >> change02 함수 선택
빌드 후 플레이