Programming skills/Unity

[유니티] 서버와의 영상 정보 불러오기

제이커브(Jcurve) 2023. 10. 28. 23:23

 

유니티 서버 영상 조회 구조도

유니티 프로젝트 중 서버의 영상 정보를 불러 들어오는 작업이 있어 구조도를 작성해보았다.

 

크게 영상 정보, ID 정보등 다양한 정보들을 담고 있는 DataManager가 있다.

[System.Serializable] // 직렬화
public class VideolInfo //비디오 정보
{

    public int shortFormNo; //영상 순서

    public string title; //제목

    public string thumbnailUrl; //썸네일 url

    public string url; //url

    public string description; //영상 설명

    public string memberName; // 닉네임

    public string date; //날짜

    public bool isInteractive; //인터랙티브 여부
}

이 데이터매니저에서 정보를 불러들어오는 방식을 두가지로 나누어놨다. 

테스트용인 CSV파일JSON파일을 불러들이는 함수를 데이터매니저에서 선언한다.

 public void SetRealVideoInfoListByCSV(string data)
 {
     thumbnailInfoList = J_CSVLoader.instance.ParseString<ThumbnailInfo>(data);
 }

 public void SetRealVideoInfoListByJSON(string data)
 {
     JsonArray<ThumbnailInfo> arrayData = JsonUtility.FromJson<JsonArray<ThumbnailInfo>>(data);
     thumbnailInfoList = arrayData.data;
 }

다음은 데이터를 분석해주는 VideoReceiver 스크립트에서 클라이언트 쪽은 비디오가 보여지도록 작업과 동시에 서버 Http로 받는 작업을 수행한다. 즉 , 클래스의 Uri instance 만들고 사용하여 GET 요청으로 HttpClient를 수행한다.

 

        // 서버한테 영상 정보 요청
        HttpInfo httpInfo = new HttpInfo();
        Uri uri = new Uri(Application.streamingAssetsPath + "/TestData/" + "RealVideoInfo.csv");
        string url = "short-form";
        httpInfo.Set(RequestType.GET, uri.AbsoluteUri, OnCompleteSearchVideo, false);
        HttpManager.Get().SendRequest(httpInfo);

여기서 DownloadHandler 클래스로 UnityWebRequest 에 연결하면 원격 서버에서 수신된 HTTP 응답 본문 데이터를 처리하는 방법으로 비디오를 만든다. 

    void OnCompleteSearchVideo1(DownloadHandler downloadHandler)
    {
        //CSV로부터 실제 비디오 정보 설정
        DataManager.instance.SetRealVideoInfoListByCSV(downloadHandler.text);

        //JSON으로부터 실제 비디오 정보설정
        DataManager.instance.SetRealVideoInfoListByJSON(downloadHandler.text);

    }

 

 

그리고 비디오 정보들이 담긴 스크립트를 만들어서 서버에 정보를 요청한다.

 

1. 비디오

    //숏폼 비디오 서버 
    public void SetItem(VideolInfo Info)
    {
        videoInfo = Info;
        // 영상 다운로드
        HttpInfo httpInfo = new HttpInfo();
        httpInfo.Set(RequestType.GET, videoInfo.url, (downloadHandler) =>
        {

            byte[] videoBytes = downloadHandler.data;

            FileStream file = new FileStream(Application.dataPath + "/" + videoInfo.title + ".mp4", FileMode.Create);
            //byteData 를 file 에 쓰자
            file.Write(videoBytes, 0, videoBytes.Length);
            file.Close();

            RenderTexture rt = new RenderTexture(1920, 1080, 24);

            videoPlayer.targetTexture = rt;
            videoPlayer.GetComponentInChildren<RawImage>().texture = rt;
            videoPlayer.url = Application.dataPath + "/" + videoInfo.title + ".mp4";
            videoPlayer.Play();

        }, false);

        HttpManager.Get().SendRequest(httpInfo);
    }

2. 이미지

//썸네일 이미지 다운로드
public void SetItem(VideolInfo Info)
{
    videoInfo = Info;

    title.text = videoInfo.title;
    date.text = videoInfo.date;
    description.text = videoInfo.description;

    HttpInfo info = new HttpInfo();
    info.Set(RequestType.TEXTURE, videoInfo.thumbnailUrl, null,false);
    info.onReceiveImage = OnCompleteDownloadTexture;
    HttpManager.Get().SendRequest(info);
}
void OnCompleteDownloadTexture(DownloadHandler downloadHandler, int id)
{
    //다운로드된 이미지 데이터를 sprite로 만든다
    Texture2D texture = ((DownloadHandlerTexture)downloadHandler).texture;
    downloadImage.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
}