제이커브(Jcurve)
카멜레온 개발자 이야기
제이커브(Jcurve)
전체 방문자
오늘
어제
  • 분류 전체보기 (26)
    • Programming skills (19)
      • Unity (8)
      • C# (1)
      • 자료구조 (3)
      • 알고리즘 (1)
      • Git (1)
      • CS(Computer Science) (4)
      • Unreal (0)
      • C++ (1)
    • Literacy Review (1)
    • Finance (1)
      • Metaverse (0)
      • 주식 (1)
      • 가상화폐 (0)
    • Certificates (0)
      • 기사 (0)
      • Coursera (0)
      • Fast campus (0)
    • Architecture (5)
      • 건축시공학 (4)
      • 철근콘크리트구조 (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • c++문법
  • 건설시공관리자
  • ㅊ++기본문법
  • C#과 유니티로 배우는 게임 개발 올인원 패키지 Online
  • 직장인자기계발
  • 주식
  • 건축공학
  • 역타공법
  • 건축
  • 건설사업관리
  • 싱글톤 # 유니티 #c# #Singleton
  • VR #메타버스 #metaverse #Eyetracking #아이트래킹 #데이터추출
  • Literacy review
  • 패스트캠퍼스후기
  • 패스트캠퍼스
  • Z세대
  • 스마트건설기술
  • 자료구조 # 스택과 큐
  • 유니티
  • 유니티강의
  • 백준
  • 건설현장조직구성
  • 건축시공학
  • cmatrisk
  • 게임개발
  • 건축전문가
  • 일상 #초보 #기록# 일기
  • 직장인인강
  • 패캠챌린지
  • 일상

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
제이커브(Jcurve)

카멜레온 개발자 이야기

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

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

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);
}

 

'Programming skills > Unity' 카테고리의 다른 글

[Unity] VR EyeTracking Gaze data 추출  (0) 2024.01.10
[Unity] 람다식과 델리게이트(delegate)  (0) 2023.10.18
[Unity]Awake 와 Start의 차이  (0) 2023.10.12
[Unity] 생명주기 Life Cycle  (0) 2023.10.11
유니티 오브젝트 생성시 하위 오브젝트로 생성하기  (0) 2023.08.17
    'Programming skills/Unity' 카테고리의 다른 글
    • [Unity] VR EyeTracking Gaze data 추출
    • [Unity] 람다식과 델리게이트(delegate)
    • [Unity]Awake 와 Start의 차이
    • [Unity] 생명주기 Life Cycle
    제이커브(Jcurve)
    제이커브(Jcurve)
    미래지향적인 성향으로 VR/AR, XR 등 가상현실에서 살아가는 사람들에 대한 공간을 연구하는 이야기입니다. 가상현실에 대한 공부와 연구를 주로 합니다.

    티스토리툴바