Add ultimate xr
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// <copyright file="AsyncOperationExt.cs" company="VRMADA">
|
||||
// Copyright (c) VRMADA, All rights reserved.
|
||||
// </copyright>
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using UltimateXR.Extensions.System.Threading;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UltimateXR.Extensions.Unity.IO
|
||||
{
|
||||
/// <summary>
|
||||
/// <see cref="AsyncOperation" /> extensions.
|
||||
/// </summary>
|
||||
public static class AsyncOperationExt
|
||||
{
|
||||
#region Public Methods
|
||||
|
||||
/// <summary>
|
||||
/// Creates an awaitable <see cref="Task" /> that finishes when the given <see cref="AsyncOperation" /> finished.
|
||||
/// </summary>
|
||||
/// <param name="self">Unity asynchronous operation object</param>
|
||||
/// <param name="ct">Optional cancellation token, to cancel the operation</param>
|
||||
/// <returns>Awaitable <see cref="Task" /> returning the caller <see cref="AsyncOperation" /> object</returns>
|
||||
public static async Task<AsyncOperation> Wait(this AsyncOperation self, CancellationToken ct = default)
|
||||
{
|
||||
await TaskExt.WaitUntil(() => self.isDone, ct);
|
||||
return self;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d767412986bc9d24c8cea81a16a3a34d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,36 @@
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// <copyright file="ResourcesExt.cs" company="VRMADA">
|
||||
// Copyright (c) VRMADA, All rights reserved.
|
||||
// </copyright>
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UltimateXR.Extensions.Unity.IO
|
||||
{
|
||||
/// <summary>
|
||||
/// <see cref="Resources" /> extensions.
|
||||
/// </summary>
|
||||
public class ResourcesExt
|
||||
{
|
||||
#region Public Methods
|
||||
|
||||
/// <summary>
|
||||
/// Loads a resource asynchronously.
|
||||
/// </summary>
|
||||
/// <param name="filePath">The path relative to a Resources folder</param>
|
||||
/// <param name="ct">Optional cancellation token, to cancel the operation</param>
|
||||
/// <typeparam name="T">Resource type to load</typeparam>
|
||||
/// <returns>Awaitable <see cref="Task" /> that returns the loaded resource</returns>
|
||||
public static async Task<T> Load<T>(string filePath, CancellationToken ct = default)
|
||||
where T : Object
|
||||
{
|
||||
ResourceRequest op = Resources.LoadAsync<T>(filePath);
|
||||
await op.Wait(ct);
|
||||
return (T)op.asset;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ed867f5e61aa8644a94500ca8d1d8bb0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,244 @@
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// <copyright file="UnityWebRequestExt.cs" company="VRMADA">
|
||||
// Copyright (c) VRMADA, All rights reserved.
|
||||
// </copyright>
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using UltimateXR.Exceptions;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
namespace UltimateXR.Extensions.Unity.IO
|
||||
{
|
||||
/// <summary>
|
||||
/// <see cref="UnityWebRequest" /> extensions to read data synchronously and asynchronously.
|
||||
/// </summary>
|
||||
public static class UnityWebRequestExt
|
||||
{
|
||||
#region Public Methods
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether a given URI can be read using a <see cref="UnityWebRequest" />.
|
||||
/// </summary>
|
||||
/// <param name="uri">URI to check</param>
|
||||
/// <returns>Whether the URI is compatible with <see cref="UnityWebRequest" /></returns>
|
||||
public static bool IsUwrUri(string uri)
|
||||
{
|
||||
return uri.Contains(Application.streamingAssetsPath)
|
||||
|| uri.StartsWith(FilePrefix)
|
||||
|| uri.StartsWith(HttpPrefix)
|
||||
|| uri.StartsWith(HttpsPrefix);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends a <see cref="UnityWebRequest" />.
|
||||
/// </summary>
|
||||
/// <param name="self">Request to send</param>
|
||||
/// <exception cref="HttpUwrException">HttpError flag is on</exception>
|
||||
/// <exception cref="NetUwrException">NetworkError flag is on</exception>
|
||||
public static void Fetch(this UnityWebRequest self)
|
||||
{
|
||||
UnityWebRequestAsyncOperation request = self.SendWebRequest();
|
||||
|
||||
while (!request.isDone)
|
||||
{
|
||||
// Active wait
|
||||
Thread.Sleep(0);
|
||||
}
|
||||
|
||||
if (self.result == UnityWebRequest.Result.ConnectionError)
|
||||
{
|
||||
throw new NetUwrException(self.error);
|
||||
}
|
||||
|
||||
if (self.result == UnityWebRequest.Result.ProtocolError)
|
||||
{
|
||||
throw new HttpUwrException(self.error, self.responseCode);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends a <see cref="UnityWebRequest" /> asynchronously.
|
||||
/// </summary>
|
||||
/// <param name="self">Request to send</param>
|
||||
/// <param name="ct">Cancellation token, to cancel the operation</param>
|
||||
/// <returns>Awaitable task that will finish when the request was sent</returns>
|
||||
/// <exception cref="OperationCanceledException">The task was canceled using <paramref name="ct" /></exception>
|
||||
/// <exception cref="HttpUwrException">HttpError flag is on</exception>
|
||||
/// <exception cref="NetUwrException">NetworkError flag is on</exception>
|
||||
public static async Task FetchAsync(this UnityWebRequest self, CancellationToken ct = default)
|
||||
{
|
||||
if (ct.IsCancellationRequested)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await self.SendWebRequest().Wait(ct);
|
||||
if (ct.IsCancellationRequested)
|
||||
{
|
||||
self.Abort();
|
||||
}
|
||||
|
||||
if (self.result == UnityWebRequest.Result.ConnectionError)
|
||||
{
|
||||
throw new NetUwrException(self.error);
|
||||
}
|
||||
|
||||
if (self.result == UnityWebRequest.Result.ProtocolError)
|
||||
{
|
||||
throw new HttpUwrException(self.error, self.responseCode);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads an <see cref="AudioClip" /> asynchronously from an URI.
|
||||
/// </summary>
|
||||
/// <param name="uri">Location of the audio clip</param>
|
||||
/// <exception cref="HttpUwrException">HttpError flag is on</exception>
|
||||
/// <exception cref="NetUwrException">NetworkError flag is on</exception>
|
||||
public static AudioClip LoadAudioClip(string uri)
|
||||
{
|
||||
using UnityWebRequest req = UnityWebRequestMultimedia.GetAudioClip(FixUri(uri), AudioType.UNKNOWN);
|
||||
req.Fetch();
|
||||
|
||||
AudioClip result = DownloadHandlerAudioClip.GetContent(req);
|
||||
result.name = Path.GetFileNameWithoutExtension(uri);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads an <see cref="AudioClip" /> asynchronously from an URI.
|
||||
/// </summary>
|
||||
/// <param name="uri">Location of the audio clip</param>
|
||||
/// <param name="ct">Optional cancellation token, to cancel the operation</param>
|
||||
/// <returns>Awaitable <see cref="Task" /> that returns the loaded audio clip</returns>
|
||||
/// <exception cref="HttpUwrException">HttpError flag is on</exception>
|
||||
/// <exception cref="NetUwrException">NetworkError flag is on</exception>
|
||||
/// <exception cref="OperationCanceledException">The task was canceled using <paramref name="ct" /></exception>
|
||||
public static async Task<AudioClip> LoadAudioClipAsync(string uri, CancellationToken ct = default)
|
||||
{
|
||||
ct.ThrowIfCancellationRequested();
|
||||
using UnityWebRequest req = UnityWebRequestMultimedia.GetAudioClip(FixUri(uri), AudioType.UNKNOWN);
|
||||
await req.FetchAsync(ct);
|
||||
|
||||
ct.ThrowIfCancellationRequested();
|
||||
AudioClip result = DownloadHandlerAudioClip.GetContent(req);
|
||||
result.name = Path.GetFileNameWithoutExtension(uri);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads bytes from an URI.
|
||||
/// </summary>
|
||||
/// <param name="uri">Location of the data</param>
|
||||
/// <exception cref="HttpUwrException">
|
||||
/// HttpError flag is on
|
||||
/// </exception>
|
||||
/// <exception cref="NetUwrException">
|
||||
/// NetworkError flag is on
|
||||
/// </exception>
|
||||
public static byte[] ReadBytes(string uri)
|
||||
{
|
||||
using UnityWebRequest req = UnityWebRequest.Get(FixUri(uri));
|
||||
req.Fetch();
|
||||
return req.downloadHandler.data;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads bytes asynchronously from an URI.
|
||||
/// </summary>
|
||||
/// <param name="uri">Location of the data</param>
|
||||
/// <param name="ct">Optional cancellation token, to cancel the operation</param>
|
||||
/// <returns>Awaitable task that returns the bytes read</returns>
|
||||
/// <exception cref="OperationCanceledException">The task was canceled using <paramref name="ct" /></exception>
|
||||
/// <exception cref="HttpUwrException">
|
||||
/// HttpError flag is on
|
||||
/// </exception>
|
||||
/// <exception cref="NetUwrException">
|
||||
/// NetworkError flag is on
|
||||
/// </exception>
|
||||
public static async Task<byte[]> ReadBytesAsync(string uri, CancellationToken ct = default)
|
||||
{
|
||||
ct.ThrowIfCancellationRequested();
|
||||
using UnityWebRequest req = UnityWebRequest.Get(FixUri(uri));
|
||||
await req.FetchAsync(ct);
|
||||
|
||||
ct.ThrowIfCancellationRequested();
|
||||
return req.downloadHandler.data;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads a string from an URI.
|
||||
/// </summary>
|
||||
/// <param name="uri">Text location</param>
|
||||
/// <exception cref="HttpUwrException">
|
||||
/// HttpError flag is on
|
||||
/// </exception>
|
||||
/// <exception cref="NetUwrException">
|
||||
/// NetworkError flag is on
|
||||
/// </exception>
|
||||
public static string ReadText(string uri)
|
||||
{
|
||||
using UnityWebRequest req = UnityWebRequest.Get(FixUri(uri));
|
||||
req.Fetch();
|
||||
return req.downloadHandler.text;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads a string asynchronously from an URI.
|
||||
/// </summary>
|
||||
/// <param name="uri">Text location</param>
|
||||
/// <param name="ct">Optional cancellation, to cancel the operation</param>
|
||||
/// <returns>Awaitable task that returns the string read</returns>
|
||||
/// <exception cref="OperationCanceledException">The task was canceled using <paramref name="ct" /></exception>
|
||||
/// <exception cref="HttpUwrException">
|
||||
/// HttpError flag is on
|
||||
/// </exception>
|
||||
/// <exception cref="NetUwrException">
|
||||
/// NetworkError flag is on
|
||||
/// </exception>
|
||||
public static async Task<string> ReadTextAsync(string uri, CancellationToken ct = default)
|
||||
{
|
||||
ct.ThrowIfCancellationRequested();
|
||||
using UnityWebRequest req = UnityWebRequest.Get(FixUri(uri));
|
||||
await req.FetchAsync(ct);
|
||||
|
||||
ct.ThrowIfCancellationRequested();
|
||||
return req.downloadHandler.text;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Methods
|
||||
|
||||
/// <summary>
|
||||
/// Fixes an URI string.
|
||||
/// </summary>
|
||||
/// <param name="uri">String to fix</param>
|
||||
/// <returns>Fixed URI string</returns>
|
||||
private static string FixUri(string uri)
|
||||
{
|
||||
string result = uri.Trim('\\', '/', ' ');
|
||||
if (!IsUwrUri(uri))
|
||||
{
|
||||
result = FilePrefix + uri;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Types & Data
|
||||
|
||||
private const string FilePrefix = "file://";
|
||||
private const string HttpPrefix = "http://";
|
||||
private const string HttpsPrefix = "https://";
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 881eec29ea48c504eaea7d53b6a9ab96
|
||||
timeCreated: 1598879590
|
||||
Reference in New Issue
Block a user