< Summary

Class:DynamicOBJLoaderController
Assembly:MainScripts
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/DynamicOBJLoaderController.cs
Covered lines:28
Uncovered lines:7
Coverable lines:35
Total lines:87
Line coverage:80% (28 of 35)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
DynamicOBJLoaderController()0%110100%
Awake()0%3.333066.67%
LoadAsset(...)0%5.395075%
LoadAsset()0%3.143075%
OnDestroy()0%220100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/DynamicOBJLoaderController.cs

#LineLine coverage
 1using DCL;
 2using UnityEngine;
 3
 4public class DynamicOBJLoaderController : MonoBehaviour
 5{
 16    public bool loadOnStart = true;
 17    public string OBJUrl = "";
 8    public GameObject loadingPlaceholder;
 9
 10    public event System.Action OnFinishedLoadingAsset;
 11
 12    [HideInInspector] public bool alreadyLoadedAsset = false;
 13    [HideInInspector] public GameObject loadedOBJGameObject;
 14
 15    IWebRequestAsyncOperation asyncOp = null;
 16
 17    void Awake()
 18    {
 119        if (loadOnStart && !string.IsNullOrEmpty(OBJUrl))
 20        {
 021            LoadAsset();
 22        }
 123    }
 24
 25    public void LoadAsset(string url = "", bool loadEvenIfAlreadyLoaded = false)
 26    {
 127        if (alreadyLoadedAsset && !loadEvenIfAlreadyLoaded)
 028            return;
 29
 130        if (!string.IsNullOrEmpty(url))
 31        {
 132            OBJUrl = url;
 33        }
 34
 135        if (asyncOp != null)
 36        {
 037            asyncOp.Dispose();
 38        }
 39
 140        LoadAsset();
 141    }
 42
 43    void LoadAsset()
 44    {
 145        if (!string.IsNullOrEmpty(OBJUrl))
 46        {
 147            Destroy(loadedOBJGameObject);
 48
 149            asyncOp = Environment.i.platform.webRequest.Get(
 50                url: OBJUrl,
 51                OnSuccess: (webRequestResult) =>
 52                {
 153                    loadedOBJGameObject = OBJLoader.LoadOBJFile(webRequestResult.webRequest.downloadHandler.text, true);
 154                    loadedOBJGameObject.name = "LoadedOBJ";
 155                    loadedOBJGameObject.transform.SetParent(transform);
 156                    loadedOBJGameObject.transform.localPosition = Vector3.zero;
 57
 158                    OnFinishedLoadingAsset?.Invoke();
 159                    alreadyLoadedAsset = true;
 160                },
 61                OnFail: (webRequestResult) =>
 62                {
 063                    Debug.Log("Couldn't get OBJ, error: " + webRequestResult.webRequest.error + " ... " + OBJUrl);
 064                });
 165        }
 66        else
 67        {
 068            Debug.Log("couldn't load OBJ because url is empty");
 69        }
 70
 171        if (loadingPlaceholder != null)
 72        {
 073            loadingPlaceholder.SetActive(false);
 74        }
 175    }
 76
 77    void OnDestroy()
 78    {
 179        if (asyncOp != null)
 80        {
 181            asyncOp.Dispose();
 82        }
 83
 184        Destroy(loadingPlaceholder);
 185        Destroy(loadedOBJGameObject);
 186    }
 87}