Post

실시간 오브젝트 다운 및 배치

다른 캡스톤 팀원이 3D 모델(glb 파일) 다운 / 씬에 로드 하는 코드를 참고함

Unity Version : 2022.3.16f1 LTS

실시간으로 오브젝트 다운받기 (GLTF)

코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// ObjectCRUD.cs

IEnumerator ChangeMyHand()
{
    while (Application.isPlaying)
    {
        string filename;
        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            filename = "duck.glb";
            LoadGltfModel(filename); 
        }

        if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            filename = "wolf.glb";
            LoadGltfModel(filename);
        }
        yield return null;
    }
}

async void LoadGltfModel(string filename)
{
    string fullFilePath = Path.Combine(Application.persistentDataPath, localDirectory, filename);
    if (!File.Exists(fullFilePath))
    {
        StartCoroutine(DownGltfModel(filename));
        // while (!File.Exists(fullFilePath)) {continue;}
    }

    if (isLoaded)
    {
        Debug.Log("Loading...");
        byte[] byteData = File.ReadAllBytes(fullFilePath);
        var gltf = new GltfImport();
        bool success = await gltf.LoadGltfBinary(byteData);
        if (success)
        {
            GameObject gltfObj = new GameObject("gltfObj");
            success = await gltf.InstantiateMainSceneAsync(gltfObj.transform);
            if (success)
            {
                MeshCollider collider = gltfObj.AddComponent<MeshCollider>();
                if (collider != null)
                    collider.convex = true;

                Rigidbody rb = gltfObj.AddComponent<Rigidbody>();
                if (rb != null)
                    rb.mass = 1.0f;

                BoxCollider boxCollider = gltfObj.AddComponent<BoxCollider>();

                Debug.Log("Asset Load Success : " + fullFilePath);
                onMyHand = gltfObj;
            }
            else
            {
                Debug.Log("Failed to Load Asset");
            }
        }
        else
        {
            Debug.Log("Failed to Load Asset");
        }
        isLoaded = false;
    }
}

IEnumerator DownGltfModel(string filename)
{
    Debug.Log("Download Start. URL : " + gltfServer + filename);
    UnityWebRequest request = UnityWebRequest.Get(gltfServer + filename);
    yield return request.SendWebRequest();

    if (request.result == UnityWebRequest.Result.Success)
    {
        string fullPath = Path.Combine(Application.persistentDataPath, localDirectory, filename);
        
        FileStream fs = new FileStream(fullPath, System.IO.FileMode.Create);
        fs.Write(request.downloadHandler.data, 0, (int)request.downloadedBytes);
        fs.Close();

        Debug.Log("Asset Down Success : " + fullPath);
        isLoaded = true;
    }
    else
    {
        Debug.LogError("Failed to Download GLTF File : " + request.error);
    }
    yield return null;
}

참고자료

준혁이가 올려준 코드

This post is licensed under CC BY 4.0 by the author.