From d148fd24a0c3d96f941f4072a8231fc87e566450 Mon Sep 17 00:00:00 2001 From: Bilal Arikan Date: Sat, 29 Jan 2022 22:17:30 +0300 Subject: [PATCH] feat: upm package --- README.md | 45 ++++- Runtime/Example/ApiExample.cs | 156 +++++++++--------- Runtime/Example/SampleScene.unity.meta | 2 +- .../Scripts/{ImagesApi.cs => DuckDuckGo.cs} | 19 ++- .../{ImagesApi.cs.meta => DuckDuckGo.cs.meta} | 0 Runtime/Scripts/Models/ImageSearchResult.cs | 2 +- Runtime/Scripts/Models/ImageSummaryModel.cs | 2 +- SS.gif => SS~/SS.gif | Bin package.json | 6 +- 9 files changed, 141 insertions(+), 91 deletions(-) rename Runtime/Scripts/{ImagesApi.cs => DuckDuckGo.cs} (95%) rename Runtime/Scripts/{ImagesApi.cs.meta => DuckDuckGo.cs.meta} (100%) rename SS.gif => SS~/SS.gif (100%) diff --git a/README.md b/README.md index 5e373bb..355af8a 100644 --- a/README.md +++ b/README.md @@ -4,4 +4,47 @@ Unity3d wrapper for duckduckgo-images-api ### Screenshot -![Alt text](/SS.gif?raw=true "Example Gif") +![Alt text](SS~/SS.gif?raw=true "Example Gif") + +How to Import: + - Add to manifest.json +``` +"com.arikan.duckduckgo.images" : "https://github.com/bilal-arikan/duckduckgo-images-api-unity.git", +``` + +How to Use: + +```C# +DuckDuckGo.Search( + "banana", // search input + SafeSearch.Off, // safesearch + 0, // page + "us-en", // locations + OnSearchCallback // callback +); + +void OnSearchCallback(ImageSearchResult result) +{ + if (result == null) + { + Debug.LogError("Result Null", this); + return; + } + + Debug.Log($"Result Count: " + result.results.Count, this); + Debug.Log("First Result:\n" + JsonUtility.ToJson(result.results[0], true), this); + foreach (var item in result.results.Take(ShowMaxResultAmount)) + { + var request = UnityWebRequestTexture.GetTexture(item.thumbnail).SendWebRequest(); + request.completed += + (ao) => + { + if (ao.isDone) + { + RawImage image = Instantiate(ResultImagePrefab, ResultsLayout.transform); + image.texture = DownloadHandlerTexture.GetContent(request.webRequest); + } + }; + } +} +``` \ No newline at end of file diff --git a/Runtime/Example/ApiExample.cs b/Runtime/Example/ApiExample.cs index 9d72e4a..a75eba2 100644 --- a/Runtime/Example/ApiExample.cs +++ b/Runtime/Example/ApiExample.cs @@ -1,98 +1,104 @@ -using System.Linq; -using System.Collections; +using System.Collections; using System.Collections.Generic; +using System.Linq; using UnityEngine; -using UnityEngine.UI; using UnityEngine.Networking; -using Arikan.Duckduckgo.Api; +using UnityEngine.UI; -public class ApiExample : MonoBehaviour +namespace Arikan.Example { - public int ShowMaxResultAmount = 15; - public string location = "us-en"; - [Space] - public InputField SearchInput; - public Button SearchButton; - public LayoutGroup ResultsLayout; - public RawImage ResultImagePrefab; - [Header("Paging")] - public Button prevButton; - public Button nextButton; - public Text pageNo; - [Space] - public int lastPageNo; + using Arikan.Models; - private List resultImages = new List(); - - private void Start() + public class ApiExample : MonoBehaviour { - SearchButton.onClick.AddListener(SendExample); - nextButton.onClick.AddListener(NextPage); - prevButton.onClick.AddListener(PreviousPage); - } + public int ShowMaxResultAmount = 15; + public string location = "us-en"; + [Space] + public InputField SearchInput; + public Button SearchButton; + public LayoutGroup ResultsLayout; + public RawImage ResultImagePrefab; + [Header("Paging")] + public Button prevButton; + public Button nextButton; + public Text pageNo; + [Space] + public int lastPageNo; - void SendExample() - { - foreach (Transform child in ResultsLayout.transform) - Destroy(child.gameObject); - Resources.UnloadUnusedAssets(); + private List resultImages = new List(); - Debug.Log($"Searching : {SearchInput.text}", this); + private void Start() + { + SearchButton.onClick.AddListener(SendExample); + nextButton.onClick.AddListener(NextPage); + prevButton.onClick.AddListener(PreviousPage); + } - SearchButton.interactable = false; - lastPageNo = 1; - ClearResults(); - ImagesApi.Search(SearchInput.text, SafeSearch.Off, lastPageNo, location, OnSearchCallback); - } + void SendExample() + { + foreach (Transform child in ResultsLayout.transform) + Destroy(child.gameObject); + Resources.UnloadUnusedAssets(); - void NextPage() - { - lastPageNo++; - ClearResults(); - ImagesApi.Search(SearchInput.text, SafeSearch.Off, lastPageNo, location, OnSearchCallback); - } - void PreviousPage() - { - lastPageNo--; - ClearResults(); - ImagesApi.Search(SearchInput.text, SafeSearch.Off, lastPageNo, location, OnSearchCallback); - } + Debug.Log($"Searching : {SearchInput.text}", this); - void ClearResults(){ - foreach (var img in resultImages) - { - Destroy(img.gameObject); + SearchButton.interactable = false; + lastPageNo = 1; + ClearResults(); + DuckDuckGo.Search(SearchInput.text, SafeSearch.Off, lastPageNo, location, OnSearchCallback); } - resultImages.Clear(); - } - void OnSearchCallback(ImageSearchResult result) - { - SearchButton.interactable = true; - prevButton.interactable = lastPageNo > 1; - pageNo.text = lastPageNo.ToString("00"); + void NextPage() + { + lastPageNo++; + ClearResults(); + DuckDuckGo.Search(SearchInput.text, SafeSearch.Off, lastPageNo, location, OnSearchCallback); + } + void PreviousPage() + { + lastPageNo--; + ClearResults(); + DuckDuckGo.Search(SearchInput.text, SafeSearch.Off, lastPageNo, location, OnSearchCallback); + } - if (result == null) + void ClearResults() { - Debug.LogError("Result Null", this); - return; + foreach (var img in resultImages) + { + Destroy(img.gameObject); + } + resultImages.Clear(); } - Debug.Log($"Result Count: " + result.results.Count, this); - Debug.Log("First Result:\n" + JsonUtility.ToJson(result.results[0], true), this); - foreach (var item in result.results.Take(ShowMaxResultAmount)) + void OnSearchCallback(ImageSearchResult result) { - var request = UnityWebRequestTexture.GetTexture(item.thumbnail).SendWebRequest(); - request.completed += - (ao) => + SearchButton.interactable = true; + prevButton.interactable = lastPageNo > 1; + pageNo.text = lastPageNo.ToString("00"); + + if (result == null) { - if (ao.isDone) + Debug.LogError("Result Null", this); + return; + } + + Debug.Log($"Result Count: " + result.results.Count, this); + Debug.Log("First Result:\n" + JsonUtility.ToJson(result.results[0], true), this); + foreach (var item in result.results.Take(ShowMaxResultAmount)) + { + var request = UnityWebRequestTexture.GetTexture(item.thumbnail).SendWebRequest(); + request.completed += + (ao) => { - var image = Instantiate(ResultImagePrefab, ResultsLayout.transform); - image.texture = DownloadHandlerTexture.GetContent(request.webRequest); - resultImages.Add(image); - } - }; + if (ao.isDone) + { + var image = Instantiate(ResultImagePrefab, ResultsLayout.transform); + image.texture = DownloadHandlerTexture.GetContent(request.webRequest); + resultImages.Add(image); + } + }; + } } } -} + +} \ No newline at end of file diff --git a/Runtime/Example/SampleScene.unity.meta b/Runtime/Example/SampleScene.unity.meta index 952bd1e..42e1060 100644 --- a/Runtime/Example/SampleScene.unity.meta +++ b/Runtime/Example/SampleScene.unity.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 9fc0d4010bbf28b4594072e72b8655ab +guid: 72050db2a4971fb4a9ee5e401fb0167a DefaultImporter: externalObjects: {} userData: diff --git a/Runtime/Scripts/ImagesApi.cs b/Runtime/Scripts/DuckDuckGo.cs similarity index 95% rename from Runtime/Scripts/ImagesApi.cs rename to Runtime/Scripts/DuckDuckGo.cs index b38d6c8..b7aa702 100644 --- a/Runtime/Scripts/ImagesApi.cs +++ b/Runtime/Scripts/DuckDuckGo.cs @@ -1,15 +1,16 @@ -using System.Linq; -using System.Text.RegularExpressions; -using System.Net; +using System; using System.Collections; using System.Collections.Generic; +using System.Linq; using System.Text; -using System; +using System.Text.RegularExpressions; using UnityEngine; using UnityEngine.Networking; -namespace Arikan.Duckduckgo.Api +namespace Arikan { + using Arikan.Models; + public enum SafeSearch { // Strict = 1, // Not Available yet @@ -17,9 +18,9 @@ public enum SafeSearch Off = -2 } - public class ImagesApi : MonoBehaviour + public class DuckDuckGo : MonoBehaviour { - private static ImagesApi instance; + private static DuckDuckGo instance; private const string url = "https://duckduckgo.com/"; private KeyValuePair lastSearch; // keyword : token @@ -40,9 +41,9 @@ public static void Search(string text, SafeSearch safeSearch, int pageNo, string { if (!instance) { - instance = new GameObject("DuckDuckGoAPI").AddComponent(); + instance = new GameObject("DuckDuckGoAPI").AddComponent(); } - if(string.IsNullOrWhiteSpace(location)) + if (string.IsNullOrWhiteSpace(location)) { location = "us-en"; } diff --git a/Runtime/Scripts/ImagesApi.cs.meta b/Runtime/Scripts/DuckDuckGo.cs.meta similarity index 100% rename from Runtime/Scripts/ImagesApi.cs.meta rename to Runtime/Scripts/DuckDuckGo.cs.meta diff --git a/Runtime/Scripts/Models/ImageSearchResult.cs b/Runtime/Scripts/Models/ImageSearchResult.cs index 345e344..e3b728f 100644 --- a/Runtime/Scripts/Models/ImageSearchResult.cs +++ b/Runtime/Scripts/Models/ImageSearchResult.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using UnityEngine; -namespace Arikan.Duckduckgo.Api +namespace Arikan.Models { [Serializable] public class ImageSearchResult diff --git a/Runtime/Scripts/Models/ImageSummaryModel.cs b/Runtime/Scripts/Models/ImageSummaryModel.cs index 792ce16..38195ea 100644 --- a/Runtime/Scripts/Models/ImageSummaryModel.cs +++ b/Runtime/Scripts/Models/ImageSummaryModel.cs @@ -1,7 +1,7 @@ using System; using UnityEngine; -namespace Arikan.Duckduckgo.Api +namespace Arikan.Models { [Serializable] public class ImageSummary diff --git a/SS.gif b/SS~/SS.gif similarity index 100% rename from SS.gif rename to SS~/SS.gif diff --git a/package.json b/package.json index c2cfd02..d7eb75e 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,10 @@ { - "name": "com.arikan.duckduckgo.api.images", + "name": "com.arikan.duckduckgo.images", "displayName": "DuckDuckGo Images API", "version": "1.0.0", - "unity": "2019.1", + "unity": "2018.1", "author": "Bilal Arikan", - "description": "DuckDuckGo Images API Codes", + "description": "DuckDuckGo Images API", "repository": { "type": "git", "url": "https://github.com/bilal-arikan/duckduckgo-images-api-unity"