'프로그래밍/Unity'에 해당되는 글 4건
- 2017.08.03 :: 유니티 VideoPlayer.
- 2016.07.20 :: unity JsonUtility 사용법. (저장 / 불러오기)
- 2016.07.15 :: 오브젝트 풀
- 2016.07.10 :: LitJSON 파일 있는지 없는지 확인 함수
이 문서는 Unity 2017.1.0f3 ver.에 쓴 문서이다.
VideoPlayer는 Unity 5.6f이상 부터 지원한 기능이다.
* Android: Supported Media Formats. See additional notes below.
* Windows: H.264 Video Decoder (see Format Constraints)
* iPhone 6-7: Compare iPhone Models (see TV and Video)
위에는 각 기기별 지원하는 동영상의 형식이다.
( .dv , .mp4 , .mpg, .mpeg, .m4v , .ogv , .vp8 , .webm )
● Source : Video Clip / URL이 있다.
Video Clip을 이용하면 간단하게 유니티 프로젝트 폴더에 동영상 파일을 넣은 다음 그걸 Drag & Drop 해주면 된다.
URL의 경우에는 http:// 혹은 file:// 을 이용해서 동영상을 찾아서 가져온다.
(주의사항 : http를 이용할 경우 파일 형식이 .mp4등 실제 동영상 주소를 가져와야한다. 예를들어 유튜브 주소로는 영상을 불러오지 못한다.)
● Play On Awake : Awake때 실행 할지의 여부를 묻는다.
● Wait For First Frame : Play On Awake이 켜져있을때 번째 프레임이 텍스처에 로드 될 때까지 기다릴 지 여부를 결정한다. 끌 경우 초기 몇 프레임이 짤려나가는 불상사가 있을 수 있다.
● Loop : 반복 여부를 묻는다.
● Playback Speed : 영상 재생 속도를 결정한다. 1은 기본 속도이다.
● Render Mode
Render Mode | |||
Camera Far Plane | 영상을 카메라의 Far Plane에서 실행한다. | ||
Camera Near Plane | 영상을 카메라의 Near Plane에서 실행한다. | ||
- Camera | 영상을 보여줄 카메라를 넣어준다. | ||
- Alpha | 영상의 알파 값을 적용한다.(알파 값을 낮추면 영상 뒤에 있는 Plane을 볼 수 있다.) | ||
Render Texture | 영상을 Texture에서 실행한다. | ||
- Target Texture | 영상을 실행할 Texture을 넣어준다. | ||
Material Override | 선택된 GameObject의 Material에서 영상을 실행한다. | ||
- Renderer | 영상을 실행할 GameObject를 넣는다. None일 경우에는 Video Player Component가 적용된 GameObject의 메터리얼에서 실행된다. | ||
- Material Property | 비디오를 재생할 메터리얼을 선택한다. | ||
API Only | 비디오를 재생할 객체를 스크립트만으로 적용하게 한다. |
● Audio Output Mode : 소리를 어떻게 재생 할 것인지 결정한다.
Audio Output Mode | |||
None | 소리를 재생하지 않는다. | ||
Audio Source | 오디오 소스를 삽입하여 재생 시킬 수 있다. (Unity 내부의 오디오 처리가 적용 될 수 있다. | ||
Direct | 유니티의 내부의 오디오 처리를 거치지 않고 바로 오디오 출력 하드웨어로 전송된다. |
비디오의 오디오 트랙 수이다.
소스가 비디오 클립인 경우 비디오 파일을 검사하여 트랙 수를 결정한다.
기본적인 스크립트 사용법
VideoPlayer는 UnityEngine.Video에 있다.
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;
public class VideoController : MonoBehaviour
{
RawImage Image;
VideoPlayer vidio;
AudioSource audio;
void Awake()
{
Image = GetComponent<RawImage>();
vidio = gameObject.AddComponent<VideoPlayer>();
audio = gameObject.AddComponent<AudioSource>();
vidio.playOnAwake = false;
audio.playOnAwake = false;
audio.Pause();
PlayVideo();
}
public void PlayVideo()
{
StartCoroutine(playVideo());
}
IEnumerator playVideo()
{
vidio.source = VideoSource.Url;
vidio.url = "file://D:/하늘.mp4";
vidio.audioOutputMode = VideoAudioOutputMode.AudioSource;
vidio.EnableAudioTrack(0, true);
vidio.SetTargetAudioSource(0, audio);
vidio.Prepare();
WaitForSeconds waitTime = new WaitForSeconds(1.0f);
while (!vidio.isPrepared)
{
Debug.Log("동영상 준비중...");
yield return waitTime;
}
Debug.Log("동영상이 준비가 끝났습니다.");
Image.texture = vidio.texture;
vidio.Play();
audio.Play();
Debug.Log("동영상이 재생됩니다.");
while (vidio.isPlaying)
{
Debug.Log("동영상 재생 시간 : " + Mathf.FloorToInt((float)vidio.time));
yield return null;
}
Debug.Log("영상이 끝났습니다.");
}
}
'프로그래밍 > Unity' 카테고리의 다른 글
unity JsonUtility 사용법. (저장 / 불러오기) (0) | 2016.07.20 |
---|---|
오브젝트 풀 (0) | 2016.07.15 |
LitJSON 파일 있는지 없는지 확인 함수 (0) | 2016.07.10 |
필자가 JSON을 알게 된 것은 nodejs+mongodb를 연환한 서버를 구축할 때 였다.
이 당시에는 JSON의 개념조차 잡혀있지 않았고 JSON이 파일까지 만들어주는 줄 알았다.
(JSON은 파싱 즉 값을 string으로 변환만 해준다.)
결국엔 실패했다.
각설하고 이번 게임 제작할때 SAVE / LOAD를 구현하려고 다시 자료를 찾던중 playerprefabs와 json을 활용한 자료 저장 xml이 있었다. 그중 필자는 저번의 실패를 만회하기 위해 json을 다시 도전했고 자료를 찾던중 유니티 5.3이상부터 지원되는 JsonUtility 기능에 대해서 알게되었다.
https://docs.unity3d.com/ScriptReference/JsonUtility.html <= 유니티 공식홈페이지
JsonUtility 레퍼런스
영어로 되어있다.
해석하자면
Create an object from its JSON representation. |
FromJsonOverwrite | Overwrite data in an object by reading from its JSON representation. |
ToJson | Generate a JSON representation of the public fields of an object. |
여기서 오브젝트란 클래스 / 구조체 / 값 전부 된다.
이제 사용 방법을 보겠다.
우선 필자는 클래스를 오브젝트로 잡았다.
[SerializeField]
public class Data
{
public string charname;
public string birthday;
public int stress;
public int stamina;
public int year;
public int month;
public int money;
public bool istired;
public bool isseek;
}
아 그리고 모든 오브젝트는 public 과 [SerializeField]로 선언되어야 한다. (이유는 잘 모르겠으나 레퍼런스를 읽어보니 JsonUtility에서 읽고 쓰는 오브젝트들은 SerializeField / public으로 선언되어있어야 사용가능하다고 나옴)
아무튼 필자는 세이브 버튼을 누르면 함수가 호출되도록 설계했다.
public void Button_save()
{
var saveData = new Data();
saveData.charname = m_charactorname;
saveData.birthday = m_birthday;
saveData.stress = m_stress;
saveData.stamina = m_stamina;
saveData.year = m_year;
saveData.month = m_month;
saveData.money = m_money;
saveData.istired = m_istired;
saveData.isseek = m_isseek;
string save = JsonUtility.ToJson(saveData, prettyPrint: true);
Debug.Log(save);
pathForDocumentsFile("save.txt"); //파일 입출력 용 함수
writeStringToFile(save1, "save.txt"); //파일 입출력 용 함수
}
이런 식으로 만들었다.
우선적으로 클래스를 saveData로 동적생성 해주고 각각의 값을 저장했다.
그 후 string save = JsonUtility.ToJson(saveData, prettyPrint: true); 이렇게 적었다.
JsonUtility 레퍼런스에서 JsonUtility.ToJson의 반환 값이 string이므로 string 변수를 생성 후 그 곳에 저장한다. 그리고 Debug.Log()를 해보면 Json형식으로 바뀐 값을 볼 수있다.
여기서 그만두면 string에만 저장되있고 파일로는 저장이 안된다. 따라서 이제 string을 파일입출력을 통해 txt파일로 저장하면된다!
이제 저장을 했으니 불러오기를 할 차례이다.
이거 또한 Load 버튼을 누르면 함수가 호출되도록 설계했다.
public void Button_load()
{
pathForDocumentsFile("save.txt"); //파일 입출력 용 함수
string load = readStringFromFile("save.txt"); //파일 입출력 용 함수
var loadData = JsonUtility.FromJson<Data>(load);
m_charactorname = loadData.charname;
m_birthday = loadData.birthday;
m_stress = loadData.stress;
m_stamina = loadData.stamina;
m_year = loadData.year;
m_month = loadData.month;
m_money = loadData.money;
m_istired = loadData.istired;
m_isseek = loadData.isseek;
}
세이브와는 정 반대이다. var loadData = JsonUtility.FromJson<Data>(load);
Data클래스의 형식으로 Json으로 되어있는 파일을 바꿔준다.
JsonUtility.FromJson<T>(string );
레퍼런스에서는 이러한 구조로 되어있다. 따라서 T 부분에 자기가 원하는 저장시 데이터 상태를 넣어주고 string 부분에 Json형식이 저장되 있는 것을 넣어주면된다.
'프로그래밍 > Unity' 카테고리의 다른 글
유니티 VideoPlayer. (0) | 2017.08.03 |
---|---|
오브젝트 풀 (0) | 2016.07.15 |
LitJSON 파일 있는지 없는지 확인 함수 (0) | 2016.07.10 |
유니티는 플랫폼이 다양하다.
그 중 모바일 플랫폼도 쓰는데, 모바일 플랫폼에서 게임오브젝트 또는 프리팹을 동적으로 생성하는 작업(Instantiate 함수)은 플랫폼 특성상 과부하가 걸릴 수 밖에 없다. 따라서 주기적 / 반복적으로 생성하는 객체는 씬이 처음 로드할 때 모두 생성한 후 사용하는 방식이 속도면에서 유리하다(렉을 방지한다). 이처럼 사용할 객체를 미리 만들어 놓은 후 필요할 때 가져다 사용하는 방식을 오브젝트 풀링이라고 한다.
ex) GameManager.cs
using UnityEngine;using System.Collections;using System.Collections.Generic; // List 자료형을 사용하기 위해 추가해야 하는 네임스페이스public class GameManager : MonoBehaviour{public Transform[] points;public GameObject monsterPrefab;public List<GameObject> monsterPool = new List<GameObject>(); //몬스터를 미리 생성해 저장할 리스트 자료형public float createTime = 2.0f;public int MaxMonster = 10;public bool isGameOver = false;public static GameManager instace - null; // 싱글턴 패턴을 위한 인스턴스 변수 선언void Awake(){//GameManager 클래스를 인스턴스에 대입instance = this;}void Start(){points = GameObject.Find("SpawnPoint").GetComponentsInChildren<Tranform>();for(int i=0;i<maxMonster;i++){GameObject monster = (GameObject)Instantiate(monsterPrefab);monster.name = "Monster_" + i.ToString();monster.SetActive(false);monsterPool.Add(monster);}if(points.Length>0){StartCoroutine(this.CreateMonster());}}IEnumerator CreateMonster(){while(!isGameOver){int monsterCount = (int) GameObject.FindGameObjectsWithTag("Monster").Length;if(monsterCount < maxMonster){yield return new WaitForSeconds(createTime);int idx = Random.Range(1,points.Length);Instantiate(monsterPrefab,points[idx].position,points[idx].rotation);}else{yield return null;}}}}
'프로그래밍 > Unity' 카테고리의 다른 글
유니티 VideoPlayer. (0) | 2017.08.03 |
---|---|
unity JsonUtility 사용법. (저장 / 불러오기) (0) | 2016.07.20 |
LitJSON 파일 있는지 없는지 확인 함수 (0) | 2016.07.10 |
'프로그래밍 > Unity' 카테고리의 다른 글
유니티 VideoPlayer. (0) | 2017.08.03 |
---|---|
unity JsonUtility 사용법. (저장 / 불러오기) (0) | 2016.07.20 |
오브젝트 풀 (0) | 2016.07.15 |