Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Editor Coroutines #201

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/Proyecto26.RestClient/Helpers/RequestHelperExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ public bool IsAborted


private bool _defaultContentType = true;

/// <summary>
/// Enable or Disable Content Type JSON by default
/// </summary>
Expand All @@ -117,6 +116,17 @@ public bool DefaultContentType
set { _defaultContentType = value; }
}

private bool _editorCoroutine = false;
/// <summary>
/// Enable to starts en editor coroutine instead
/// </summary>
/// <value>A boolean to know if the request is working on Editor without running the game</value>
public bool EditorCoroutine
{
get { return _editorCoroutine; }
set { _editorCoroutine = value; }
}

/// <summary>
/// Abort the request manually
/// </summary>
Expand Down
16 changes: 16 additions & 0 deletions src/Proyecto26.RestClient/Helpers/StaticCoroutine.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using UnityEngine;
using System.Collections;
#if UNITY_EDITOR
using Unity.EditorCoroutines.Editor;
#endif

namespace Proyecto26
{
Expand All @@ -25,5 +28,18 @@ public static Coroutine StartCoroutine(IEnumerator coroutine)
{
return Runner.StartCoroutine(coroutine);
}

public static void Start(bool useEditor, IEnumerator coroutine)
{
#if UNITY_EDITOR
if (useEditor)
{
EditorCoroutineUtility.StartCoroutineOwnerless(coroutine);
return;
}
#endif
StartCoroutine(coroutine);
return;
}
}
}
8 changes: 4 additions & 4 deletions src/Proyecto26.RestClient/RestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public static void ClearDefaultHeaders()
/// <param name="callback">A callback function that is executed when the request is finished.</param>
public static void Request(RequestHelper options, Action<RequestException, ResponseHelper> callback)
{
StaticCoroutine.StartCoroutine(HttpBase.DefaultUnityWebRequest(options, callback));
StaticCoroutine.Start(options.EditorCoroutine, HttpBase.DefaultUnityWebRequest(options, callback));
}

/// <summary>
Expand All @@ -101,7 +101,7 @@ public static void Request(RequestHelper options, Action<RequestException, Respo
/// <typeparam name="T">The element type of the response.</typeparam>
public static void Request<T>(RequestHelper options, Action<RequestException, ResponseHelper, T> callback)
{
StaticCoroutine.StartCoroutine(HttpBase.DefaultUnityWebRequest<T>(options, callback));
StaticCoroutine.Start(options.EditorCoroutine, HttpBase.DefaultUnityWebRequest<T>(options, callback));
}

/// <summary>
Expand Down Expand Up @@ -168,7 +168,7 @@ public static void GetArray<T>(string url, Action<RequestException, ResponseHelp
public static void GetArray<T>(RequestHelper options, Action<RequestException, ResponseHelper, T[]> callback)
{
options.Method = UnityWebRequest.kHttpVerbGET;
StaticCoroutine.StartCoroutine(HttpBase.DefaultUnityWebRequest<T>(options, callback));
StaticCoroutine.Start(options.EditorCoroutine, HttpBase.DefaultUnityWebRequest<T>(options, callback));
}

/// <summary>
Expand Down Expand Up @@ -273,7 +273,7 @@ public static void PostArray<T>(string url, string bodyString, Action<RequestExc
public static void PostArray<T>(RequestHelper options, Action<RequestException, ResponseHelper, T[]> callback)
{
options.Method = UnityWebRequest.kHttpVerbPOST;
StaticCoroutine.StartCoroutine(HttpBase.DefaultUnityWebRequest(options, callback));
StaticCoroutine.Start(options.EditorCoroutine, HttpBase.DefaultUnityWebRequest(options, callback));
}

/// <summary>
Expand Down