diff --git a/.codacy.yml b/.codacy.yml new file mode 100644 index 0000000..5658121 --- /dev/null +++ b/.codacy.yml @@ -0,0 +1,2 @@ +exclude_paths: + - '*.md' diff --git a/README.md b/README.md index 5f13dcf..7979e67 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,13 @@

Gameframe.Giphy 👋

-

- Version - - Twitter: coryleach - -

+ + +[![Codacy Badge](https://app.codacy.com/project/badge/Grade/d2749fdbc70f422a9d1efccb56d48bff)](https://www.codacy.com/manual/coryleach/UnityGiphy?utm_source=github.com&utm_medium=referral&utm_content=coryleach/UnityGiphy&utm_campaign=Badge_Grade) +![version](https://img.shields.io/github/package-json/v/coryleach/UnityGiphy) +[![openupm](https://img.shields.io/npm/v/com.gameframe.giphy?label=openupm&registry_uri=https://package.openupm.com)](https://openupm.com/packages/com.gameframe.giphy/) +[![license](https://img.shields.io/github/license/coryleach/UnityGiphy)](https://github.com/coryleach/UnityGiphy/blob/master/LICENSE) + +[![twitter](https://img.shields.io/twitter/follow/coryleach.svg?style=social)](https://twitter.com/coryleach) + This package contains a simple implementation of the Giphy API required to display random Gif images in Unity as MP4 videos. This package does not provide a means to display a gif directly but does provide urls to gif version of the images. @@ -15,7 +18,7 @@ Instead this package uses the mp4 links provided by giphy's api to display the g #### Using UnityPackageManager (for Unity 2019.3 or later) Open the package manager window (menu: Window > Package Manager)
Select "Add package from git URL...", fill in the pop-up with the following link:
-https://github.com/coryleach/UnityGiphy.git#1.0.1
+https://github.com/coryleach/UnityGiphy.git#2.0.0
#### Using UnityPackageManager (for Unity 2019.1 or later) @@ -23,7 +26,7 @@ Find the manifest.json file in the Packages folder of your project and edit it t ```js { "dependencies": { - "com.gameframe.giphy": "https://github.com/coryleach/UnityGiphy.git#1.0.1", + "com.gameframe.giphy": "https://github.com/coryleach/UnityGiphy.git#2.0.0", ... }, } diff --git a/Runtime/GiphyConfig.cs b/Runtime/GiphyConfig.cs index a08ea00..66a2956 100644 --- a/Runtime/GiphyConfig.cs +++ b/Runtime/GiphyConfig.cs @@ -1,15 +1,21 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; +using UnityEngine; namespace Gameframe.Giphy { [CreateAssetMenu(menuName = "Gameframe/Giphy/Config")] - public class GiphyConfig : ScriptableObject + public class GiphyConfig : ScriptableObject, IGiphyConfig { - public string apiKey = ""; - public GiphyRating rating = GiphyRating.PG13; - public string lang = "en"; + [SerializeField] + private string apiKey = ""; + public string ApiKey => apiKey; + + [SerializeField] + private GiphyRating rating = GiphyRating.PG13; + public GiphyRating Rating => rating; + + [SerializeField] + private string lang = "en"; + public string Lang => lang; } } diff --git a/Runtime/GiphyQuery.cs b/Runtime/GiphyQuery.cs index bc12dff..cdec08c 100644 --- a/Runtime/GiphyQuery.cs +++ b/Runtime/GiphyQuery.cs @@ -10,9 +10,9 @@ public static class GiphyQuery private const string searchApiUrl = "api.giphy.com/v1/gifs/search"; private const string randomApiUrl = "api.giphy.com/v1/gifs/random"; - public static Task Random(GiphyConfig config, string tag) + public static Task Random(IGiphyConfig config, string tag) { - var requestUrl = $"https://{randomApiUrl}?api_key={config.apiKey}&tag={UnityWebRequest.EscapeURL(tag)}&rating={config.rating.ToQueryString()}"; + var requestUrl = $"https://{randomApiUrl}?api_key={config.ApiKey}&tag={UnityWebRequest.EscapeURL(tag)}&rating={config.Rating.ToQueryString()}"; return Query(requestUrl); } @@ -22,9 +22,9 @@ public static Task Random(string apiKey, string tag, Gip return Query(requestUrl); } - public static Task Search(GiphyConfig config, string query, int limit = 25, int offset = 0) + public static Task Search(IGiphyConfig config, string query, int limit = 25, int offset = 0) { - var requestUrl = $"https://{searchApiUrl}?api_key={config.apiKey}&q={UnityWebRequest.EscapeURL(query)}&limit={limit}&offset={offset}&rating={config.rating.ToQueryString()}&lang={config.lang}"; + var requestUrl = $"https://{searchApiUrl}?api_key={config.ApiKey}&q={UnityWebRequest.EscapeURL(query)}&limit={limit}&offset={offset}&rating={config.Rating.ToQueryString()}&lang={config.Lang}"; return Query(requestUrl); } diff --git a/Runtime/GiphyQueryCommonResultData.cs b/Runtime/GiphyQueryCommonResultData.cs index bb34aca..d4df409 100644 --- a/Runtime/GiphyQueryCommonResultData.cs +++ b/Runtime/GiphyQueryCommonResultData.cs @@ -1,39 +1,63 @@ using System; +using UnityEngine; namespace Gameframe.Giphy { [Serializable] public class GiphyQueryCommonResultData { - public string type; - public string id; - public string url; - public string slug; - public string bitly_gif_url; - public string bitly_url; - public string embed_url; - public string username; - public string source; - public string title; - public string rating; - public string content_url; - public string source_tld; - public int is_sticker; + [SerializeField] private string type; + [SerializeField] private string id; + [SerializeField] private string url; + [SerializeField] private string slug; + [SerializeField] private string bitly_gif_url; + [SerializeField] private string bitly_url; + [SerializeField] private string embed_url; + [SerializeField] private string username; + [SerializeField] private string source; + [SerializeField] private string title; + [SerializeField] private string rating; + [SerializeField] private string content_url; + [SerializeField] private string source_tld; + [SerializeField] private int is_sticker; + + public int IsSticker => is_sticker; + public string SourceTld => source_tld; + public string ContentUrl => content_url; + public string Rating => rating; + public string Title => title; + public string Source => source; + public string Username => username; + public string EmbedUrl => embed_url; + public string BitlyUrl => bitly_url; + public string BitlyGifUrl => bitly_gif_url; + public string Slug => slug; + public string Url => url; + public string Id => id; + public string Type1 => type; } [Serializable] public class GiphyQueryMetaData { - public int status; - public string msg; - public string response_id; + [SerializeField] private int status; + [SerializeField] private string msg; + [SerializeField] private string response_id; + + public int Status => status; + public string Msg => msg; + public string ResponseId => response_id; } [Serializable] public class GiphyQueryPaginationData { - public int total_count; - public int count; - public int offset; + [SerializeField] private int total_count; + [SerializeField] private int count; + [SerializeField] private int offset; + + public int TotalCount => total_count; + public int Count => count; + public int Offset => offset; } } diff --git a/Runtime/GiphyQueryRandomResultData.cs b/Runtime/GiphyQueryRandomResultData.cs index facd2ec..fdd1178 100644 --- a/Runtime/GiphyQueryRandomResultData.cs +++ b/Runtime/GiphyQueryRandomResultData.cs @@ -1,44 +1,72 @@ using System; +using UnityEngine; namespace Gameframe.Giphy { [Serializable] public class GiphyQueryRandomResult { - public GiphyQueryRandomResultData data; - public GiphyQueryMetaData meta; + [SerializeField] private GiphyQueryRandomResultData data; + [SerializeField] private GiphyQueryMetaData meta; + + public GiphyQueryRandomResultData Data => data; + public GiphyQueryMetaData Meta => meta; } [Serializable] public class GiphyQueryRandomResultData : GiphyQueryCommonResultData { - public GiphyGifResultImagesData images; - - public string image_original_url; - public string image_url; - public string image_mp4_url; - public int image_frames; - public int image_width; - public int image_height; - - public string fixed_height_downsampled_url; - public int fixed_height_downsampled_width; - public int fixed_height_downsampled_height; - - public string fixed_width_downsampled_url; - public int fixed_width_downsampled_width; - public int fixed_width_downsampled_height; - - public string fixed_height_small_url; - public string fixed_height_small_still_url; - public int fixed_height_small_width; - public int fixed_height_small_height; - - public string fixed_width_small_url; - public string fixed_width_small_still_url; - public int fixed_width_small_width; - public int fixed_width_small_height; - - public string caption; + [SerializeField] private GiphyGifResultImagesData images; + + [SerializeField] private string image_original_url; + [SerializeField] private string image_url; + [SerializeField] private string image_mp4_url; + [SerializeField] private int image_frames; + [SerializeField] private int image_width; + [SerializeField] private int image_height; + + [SerializeField] private string fixed_height_downsampled_url; + [SerializeField] private int fixed_height_downsampled_width; + [SerializeField] private int fixed_height_downsampled_height; + + [SerializeField] private string fixed_width_downsampled_url; + [SerializeField] private int fixed_width_downsampled_width; + [SerializeField] private int fixed_width_downsampled_height; + + [SerializeField] private string fixed_height_small_url; + [SerializeField] private string fixed_height_small_still_url; + [SerializeField] private int fixed_height_small_width; + [SerializeField] private int fixed_height_small_height; + + [SerializeField] private string fixed_width_small_url; + [SerializeField] private string fixed_width_small_still_url; + [SerializeField] private int fixed_width_small_width; + [SerializeField] private int fixed_width_small_height; + + [SerializeField] private string caption; + + public GiphyGifResultImagesData Images => images; + public string ImageOriginalUrl => image_original_url; + public string ImageUrl => image_url; + public string ImageMp4Url => image_mp4_url; + public int ImageFrames => image_frames; + public int ImageWidth => image_width; + public int ImageHeight => image_height; + + public string FixedHeightDownsampledUrl => fixed_height_downsampled_url; + public int FixedHeightDownsampledWidth => fixed_height_downsampled_width; + public int FixedHeightDownsampledHeight => fixed_height_downsampled_height; + public string FixedWidthDownsampledUrl => fixed_width_downsampled_url; + public int FixedWidthDownsampledWidth => fixed_width_downsampled_width; + public int FixedWidthDownsampledHeight => fixed_width_downsampled_height; + public string FixedHeightSmallUrl => fixed_height_small_url; + public string FixedHeightSmallStillUrl => fixed_height_small_still_url; + public int FixedHeightSmallWidth => fixed_height_small_width; + public int FixedHeightSmallHeight => fixed_height_small_height; + public string FixedWidthSmallUrl => fixed_width_small_url; + public string FixedWidthSmallStillUrl => fixed_width_small_still_url; + public int FixedWidthSmallWidth => fixed_width_small_width; + public int FixedWidthSmallHeight => fixed_width_small_height; + public string Caption => caption; } } \ No newline at end of file diff --git a/Runtime/GiphyQuerySearchResultData.cs b/Runtime/GiphyQuerySearchResultData.cs index ab7a553..7749e2f 100644 --- a/Runtime/GiphyQuerySearchResultData.cs +++ b/Runtime/GiphyQuerySearchResultData.cs @@ -1,60 +1,100 @@ using System; using System.Collections.Generic; +using UnityEngine; namespace Gameframe.Giphy { [Serializable] public class GiphyQuerySearchResult { - public List data; - public GiphyQueryPaginationData pagination; - public GiphyQueryMetaData meta; + [SerializeField] private List data; + [SerializeField] private GiphyQueryPaginationData pagination; + [SerializeField] private GiphyQueryMetaData meta; + + public IReadOnlyList Data => data; + public GiphyQueryPaginationData Pagination => pagination; + public GiphyQueryMetaData Meta => meta; } [Serializable] public class GiphyQuerySearchResultData : GiphyQueryCommonResultData { - public GiphyGifResultImagesData images; + [SerializeField] private GiphyGifResultImagesData images; + + public GiphyGifResultImagesData Images => images; } [Serializable] public class GiphyGifResultImagesData { - public GiphyImageData original_mp4; - public GiphyImageData downsized_large; - public GiphyImageData fixed_height_small_still; - public GiphyImageData original; - public GiphyImageData fixed_height_downsampled; - public GiphyImageData downsized_still; - public GiphyImageData fixed_height_still; - public GiphyImageData downsized_medium; - public GiphyImageData downsized; - public GiphyImageData preview_webp; - public GiphyImageData fixed_height_small; - public GiphyImageData fixed_height; - public GiphyImageData downsized_small; - public GiphyImageData preview; - public GiphyImageData fixed_width_downsampled; - public GiphyImageData fixed_width_small_still; - public GiphyImageData fixed_width_small; - public GiphyImageData original_still; - public GiphyImageData fixed_width_still; - public GiphyImageData looping; - public GiphyImageData fixed_width; - public GiphyImageData preview_gif; + [SerializeField] private GiphyImageData original_mp4; + [SerializeField] private GiphyImageData downsized_large; + [SerializeField] private GiphyImageData fixed_height_small_still; + [SerializeField] private GiphyImageData original; + [SerializeField] private GiphyImageData fixed_height_downsampled; + [SerializeField] private GiphyImageData downsized_still; + [SerializeField] private GiphyImageData fixed_height_still; + [SerializeField] private GiphyImageData downsized_medium; + [SerializeField] private GiphyImageData downsized; + [SerializeField] private GiphyImageData preview_webp; + [SerializeField] private GiphyImageData fixed_height_small; + [SerializeField] private GiphyImageData fixed_height; + [SerializeField] private GiphyImageData downsized_small; + [SerializeField] private GiphyImageData preview; + [SerializeField] private GiphyImageData fixed_width_downsampled; + [SerializeField] private GiphyImageData fixed_width_small_still; + [SerializeField] private GiphyImageData fixed_width_small; + [SerializeField] private GiphyImageData original_still; + [SerializeField] private GiphyImageData fixed_width_still; + [SerializeField] private GiphyImageData looping; + [SerializeField] private GiphyImageData fixed_width; + [SerializeField] private GiphyImageData preview_gif; + + public GiphyImageData OriginalMp4 => original_mp4; + public GiphyImageData DownsizedLarge => downsized_large; + public GiphyImageData FixedHeightSmallStill => fixed_height_small_still; + public GiphyImageData Original => original; + public GiphyImageData FixedHeightDownsampled => fixed_height_downsampled; + public GiphyImageData DownsizedStill => downsized_still; + public GiphyImageData FixedHeightStill => fixed_height_still; + public GiphyImageData DownsizedMedium => downsized_medium; + public GiphyImageData Downsized => downsized; + public GiphyImageData PreviewWebp => preview_webp; + public GiphyImageData FixedHeightSmall => fixed_height_small; + public GiphyImageData FixedHeight => fixed_height; + public GiphyImageData DownsizedSmall => downsized_small; + public GiphyImageData Preview => preview; + public GiphyImageData FixedWidthDownsampled => fixed_width_downsampled; + public GiphyImageData FixedWidthSmallStill => fixed_width_small_still; + public GiphyImageData FixedWidthSmall => fixed_width_small; + public GiphyImageData OriginalStill => original_still; + public GiphyImageData FixedWidthStill => fixed_width_still; + public GiphyImageData Looping => looping; + public GiphyImageData FixedWidth => fixed_width; + public GiphyImageData PreviewGif => preview_gif; } [Serializable] public class GiphyImageData { - public int height; - public int width; - public int size; - public string hash; - public string url; - public string mp4; - public int mp4_size; - public string webp; - public int webp_size; + [SerializeField] private int height; + [SerializeField] private int width; + [SerializeField] private int size; + [SerializeField] private string hash; + [SerializeField] private string url; + [SerializeField] private string mp4; + [SerializeField] private int mp4_size; + [SerializeField] private string webp; + [SerializeField] private int webp_size; + + public int Height => height; + public int Width => width; + public int Size => size; + public string Hash => hash; + public string Url => url; + public string Mp4 => mp4; + public int Mp4Size => mp4_size; + public string Webp => webp; + public int WebpSize => webp_size; } } \ No newline at end of file diff --git a/Runtime/GiphyRawImageController.cs b/Runtime/GiphyRawImageController.cs index 5918eba..9fcf18f 100644 --- a/Runtime/GiphyRawImageController.cs +++ b/Runtime/GiphyRawImageController.cs @@ -9,7 +9,7 @@ namespace Gameframe.Giphy public class GiphyRawImageController : MonoBehaviour { [SerializeField] - private GiphyConfig config = null; + private GiphyConfig config; [SerializeField] private QueryType queryType = QueryType.Random; @@ -119,7 +119,7 @@ public async void PlayRandom() return; } - if (string.IsNullOrEmpty(config.apiKey)) + if (string.IsNullOrEmpty(config.ApiKey)) { Debug.LogError("Giphy ApiKey is not configured. You must create a developer app and get an API key from the giphy developer website and add it to your GiphyConfig file.",this); return; @@ -127,7 +127,7 @@ public async void PlayRandom() Alpha = 0; var result = await GiphyQuery.Random(config, searchQuery); - if (string.IsNullOrEmpty(result.data.image_mp4_url)) + if (string.IsNullOrEmpty(result.Data.ImageMp4Url)) { Debug.Log("No MP4 result"); return; @@ -140,15 +140,15 @@ public async void PlayRandom() if (videoPlayer.isLooping) { - videoPlayer.url = result.data.images.looping.mp4; - width = result.data.image_width; - height = result.data.image_height; + videoPlayer.url = result.Data.Images.Looping.Mp4; + width = result.Data.ImageWidth; + height = result.Data.ImageHeight; } else { - videoPlayer.url = result.data.images.original_mp4.mp4; - width = result.data.image_width; - height = result.data.image_height; + videoPlayer.url = result.Data.Images.OriginalMp4.Mp4; + width = result.Data.ImageWidth; + height = result.Data.ImageHeight; } SetImageSize(width,height); @@ -164,7 +164,7 @@ public async void PlaySearchRandom() return; } - if (string.IsNullOrEmpty(config.apiKey)) + if (string.IsNullOrEmpty(config.ApiKey)) { Debug.LogError("Giphy ApiKey is not configured. You must create a developer app and get an API key from the giphy developer website and add it to your GiphyConfig file.",this); return; @@ -172,9 +172,9 @@ public async void PlaySearchRandom() Alpha = 0; var result = await GiphyQuery.Search(config, searchQuery); - var validResults = result.data.Where(x => !string.IsNullOrEmpty(x.images.original_mp4.mp4)).ToList(); + var validResults = result.Data.Where(x => !string.IsNullOrEmpty(x.Images.OriginalMp4.Mp4)).ToList(); var randomResult = validResults[UnityEngine.Random.Range(0, validResults.Count)]; - if (string.IsNullOrEmpty(randomResult.images.original_mp4.mp4)) + if (string.IsNullOrEmpty(randomResult.Images.OriginalMp4.Mp4)) { Debug.Log("No MP4 result"); return; @@ -187,15 +187,15 @@ public async void PlaySearchRandom() if (videoPlayer.isLooping) { - videoPlayer.url = randomResult.images.looping.mp4; - width = randomResult.images.original.width; - height = randomResult.images.original.height; + videoPlayer.url = randomResult.Images.Looping.Mp4; + width = randomResult.Images.Original.Width; + height = randomResult.Images.Original.Height; } else { - videoPlayer.url = randomResult.images.original_mp4.mp4; - width = randomResult.images.original_mp4.width; - height = randomResult.images.original_mp4.height; + videoPlayer.url = randomResult.Images.OriginalMp4.Mp4; + width = randomResult.Images.OriginalMp4.Width; + height = randomResult.Images.OriginalMp4.Height; } SetImageSize(width,height); diff --git a/Runtime/IGiphyConfig.cs b/Runtime/IGiphyConfig.cs new file mode 100644 index 0000000..7935853 --- /dev/null +++ b/Runtime/IGiphyConfig.cs @@ -0,0 +1,13 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace Gameframe.Giphy +{ + public interface IGiphyConfig + { + string ApiKey { get; } + GiphyRating Rating { get; } + string Lang { get; } + } +} diff --git a/Runtime/IGiphyConfig.cs.meta b/Runtime/IGiphyConfig.cs.meta new file mode 100644 index 0000000..e92afbe --- /dev/null +++ b/Runtime/IGiphyConfig.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 67f78c5bbbf62f94ca38b0aaaae05280 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/UnityWebRequestAwaiter.cs b/Runtime/UnityWebRequestAwaiter.cs index d77886a..3b12b21 100644 --- a/Runtime/UnityWebRequestAwaiter.cs +++ b/Runtime/UnityWebRequestAwaiter.cs @@ -7,8 +7,8 @@ namespace Gameframe.Giphy { public class UnityWebRequestAwaiter : INotifyCompletion { - private UnityWebRequestAsyncOperation asyncOp; - private Action continuation; + private readonly UnityWebRequestAsyncOperation asyncOp; + private Action _continuation; public UnityWebRequestAwaiter(UnityWebRequestAsyncOperation asyncOp) { @@ -18,16 +18,22 @@ public UnityWebRequestAwaiter(UnityWebRequestAsyncOperation asyncOp) public bool IsCompleted { get { return asyncOp.isDone; } } - public void GetResult() { } + public void GetResult() + { + do + { + //Block until operation is completed + } while (!IsCompleted); + } public void OnCompleted(Action continuation) { - this.continuation = continuation; + this._continuation = continuation; } private void OnRequestCompleted(AsyncOperation obj) { - continuation(); + _continuation(); } } diff --git a/Runtime/csc.rsp b/Runtime/csc.rsp new file mode 100644 index 0000000..2f6b821 --- /dev/null +++ b/Runtime/csc.rsp @@ -0,0 +1 @@ +-nowarn:CS0649 diff --git a/Runtime/csc.rsp.meta b/Runtime/csc.rsp.meta new file mode 100644 index 0000000..972fdc9 --- /dev/null +++ b/Runtime/csc.rsp.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 33299207efb5b1e41b234769338f9a23 +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/package.json b/package.json index bb63e57..622844d 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "com.gameframe.giphy", "displayName": "Gameframe.Giphy", - "version": "1.0.1", + "version": "2.0.0", "description": "This package contains a simple implementation of the Giphy API required to display random Gif images in Unity as MP4 videos.\nThis package does not provide a means to display a gif directly but does provide urls to gif version of the images.\nInstead this package uses the mp4 links provided by giphy's api to display the gifs as movies", "unity": "2019.1", "keywords": [],