-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
af3da2c
commit d148fd2
Showing
9 changed files
with
141 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<RawImage> resultImages = new List<RawImage>(); | ||
|
||
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<RawImage> resultImages = new List<RawImage>(); | ||
|
||
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); | ||
} | ||
}; | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters