public async Task <FileReadResult> ReadFileFromResource(string fileName)
        {
            FileReadResult fileResult = null;

            if (_packingInOneFile != null)
            {
                var data = _packingInOneFile.GetFileBytes(fileName);
                fileResult = new FileReadResult {
                    data = data, IsError = data == null
                };
            }
            else
            {
                string fullPath = _rootResourceFolder + "/" + fileName;

                fileResult = await ReadResourceFileUtils.ReadFileFromStreamingAssetsWithPlatform(fullPath);

                if (!fileResult.IsError)
                {
                    _dictCatch[fullPath] = fileResult;
                }
            }

            return(fileResult);
        }
示例#2
0
        public override async Task <ResponseData> MakeResponseData()
        {
            var          pathWithoutParams = context.AbsolutePath;
            ResponseData responseData      = new ResponseData();

            string filePath = pathWithoutParams.Remove(0, 1);

            if (string.IsNullOrEmpty(filePath))
            {
                filePath = "index.html";
            }

            FileReadResult fileReadResult = await _resourceFileStorage.ReadFileFromResource(filePath);

            if (!fileReadResult.IsError)
            {
                responseData.data = fileReadResult.data;
            }
            else
            {
                Debug.LogError($"Error load file - {filePath}");
                responseData.data = ResponseTools.ConvertStringToResponseData($"Error load file {filePath}");
            }

            return(responseData);
        }
示例#3
0
        public static async Task <FileReadResult> ReadFileFromStreamingAssetsWithPlatform(string fileName)
        {
            await TaskSwitcher.SwitchToMainThread();

            var finalPath      = GetPlatformPathToStreamingAssets(fileName);
            var fileReadResult = new FileReadResult();

            UnityWebRequest loadFile = null;

            try
            {
                loadFile = UnityWebRequest.Get(finalPath);
                var asyncOperation = loadFile.SendWebRequest();
                while (!asyncOperation.isDone)
                {
                    await Task.Delay(25);
                }
            }
            catch (Exception)
            {
                fileReadResult.IsError = true;
                Debug.LogError($"UnityWebRequest {fileName}");
            }

            if (loadFile != null && loadFile.isDone)
            {
                if (loadFile.isNetworkError || loadFile.isHttpError)
                {
                    fileReadResult = new FileReadResult()
                    {
                        error   = loadFile.error,
                        IsError = true,
                    };
                }
                else
                {
                    fileReadResult = new FileReadResult()
                    {
                        text = loadFile.downloadHandler.text,
                        data = loadFile.downloadHandler.data,
                    };
                }
            }

            return(fileReadResult);
        }