diff --git a/Runtime/Example/ApiExample.cs b/Runtime/Example/ApiExample.cs index 0e47401..9d72e4a 100644 --- a/Runtime/Example/ApiExample.cs +++ b/Runtime/Example/ApiExample.cs @@ -9,6 +9,8 @@ public class ApiExample : MonoBehaviour { public int ShowMaxResultAmount = 15; + public string location = "us-en"; + [Space] public InputField SearchInput; public Button SearchButton; public LayoutGroup ResultsLayout; @@ -40,20 +42,20 @@ void SendExample() SearchButton.interactable = false; lastPageNo = 1; ClearResults(); - ImagesApi.Search(SearchInput.text, SafeSearch.Off, lastPageNo, OnSearchCallback); + ImagesApi.Search(SearchInput.text, SafeSearch.Off, lastPageNo, location, OnSearchCallback); } void NextPage() { lastPageNo++; ClearResults(); - ImagesApi.Search(SearchInput.text, SafeSearch.Off, lastPageNo, OnSearchCallback); + ImagesApi.Search(SearchInput.text, SafeSearch.Off, lastPageNo, location, OnSearchCallback); } void PreviousPage() { lastPageNo--; ClearResults(); - ImagesApi.Search(SearchInput.text, SafeSearch.Off, lastPageNo, OnSearchCallback); + ImagesApi.Search(SearchInput.text, SafeSearch.Off, lastPageNo, location, OnSearchCallback); } void ClearResults(){ diff --git a/Runtime/Example/SampleScene.unity b/Runtime/Example/SampleScene.unity index 1860bcb..1c26620 100644 --- a/Runtime/Example/SampleScene.unity +++ b/Runtime/Example/SampleScene.unity @@ -771,6 +771,7 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: ShowMaxResultAmount: 24 + location: us-en SearchInput: {fileID: 313931728} SearchButton: {fileID: 1956213752} ResultsLayout: {fileID: 322455885} diff --git a/Runtime/Scripts/ImagesApi.cs b/Runtime/Scripts/ImagesApi.cs index 22ed8f7..b38d6c8 100644 --- a/Runtime/Scripts/ImagesApi.cs +++ b/Runtime/Scripts/ImagesApi.cs @@ -35,14 +35,18 @@ public class ImagesApi : MonoBehaviour public static void Search(string text, int pageNo, Action onCompleted) - => Search(text, SafeSearch.Moderate, pageNo, onCompleted); - public static void Search(string text, SafeSearch safeSearch, int pageNo, Action onCompleted) + => Search(text, SafeSearch.Moderate, pageNo, "us-en", onCompleted); + public static void Search(string text, SafeSearch safeSearch, int pageNo, string location, Action onCompleted) { if (!instance) { instance = new GameObject("DuckDuckGoAPI").AddComponent(); } - instance.SearchFromInstance(text, safeSearch, pageNo, onCompleted); + if(string.IsNullOrWhiteSpace(location)) + { + location = "us-en"; + } + instance.SearchFromInstance(text, safeSearch, pageNo, location, onCompleted); } @@ -51,11 +55,11 @@ private void Awake() instance = this; DontDestroyOnLoad(gameObject); } - private void SearchFromInstance(string text, SafeSearch safeSearch, int pageNo, Action onCompleted) + private void SearchFromInstance(string text, SafeSearch safeSearch, int pageNo, string location, Action onCompleted) { - StartCoroutine(SearchCoRo(text, safeSearch, pageNo, onCompleted)); + StartCoroutine(SearchCoRo(text, safeSearch, pageNo, location, onCompleted)); } - private IEnumerator SearchCoRo(string keyword, SafeSearch safeSearch, int pageNo, Action onCompleted) + private IEnumerator SearchCoRo(string keyword, SafeSearch safeSearch, int pageNo, string location, Action onCompleted) { string token = lastSearch.Value; if (lastSearch.Key != keyword) @@ -66,7 +70,7 @@ private IEnumerator SearchCoRo(string keyword, SafeSearch safeSearch, int pageNo } // Debug.Log("SrcOb:" + currentToken); - yield return RequestSearchResult(keyword, token, safeSearch, pageNo, onCompleted); + yield return RequestSearchResult(keyword, token, safeSearch, pageNo, location, onCompleted); } private IEnumerator RequestToken(string keyword, SafeSearch safeSearch, Action tokenCallback) @@ -96,11 +100,11 @@ private IEnumerator RequestToken(string keyword, SafeSearch safeSearch, Action callback) + private IEnumerator RequestSearchResult(string keyword, string token, SafeSearch safeSearch, int pageNo, string location, Action callback) { pageNo = Mathf.Clamp(pageNo, 1, int.MaxValue); Dictionary parameters = new Dictionary(){ - {"l", "us-en"}, + {"l", location}, {"o", "json"}, {"q", keyword}, {"vqd", token},