/// <summary> /// This function called when searching for users finished. /// </summary> private void OnSearchForUsersFinished(HTTPRequest request, HTTPResponse response) { searchStarted = false; if (response != null) { searchText = string.Empty; JsonReader reader = new JsonReader(response.DataAsText); reader.SkipNonMembers = true; searchResult = JsonMapper.ToObject <GithubUserSearchResult>(reader); if (searchResult == null || searchResult.users == null) { GithubMessage msg = JsonMapper.ToObject <GithubMessage>(response.DataAsText); if (msg != null) { searchText = msg.message; } } } else { if (request.Exception != null) { searchText = request.Exception.Message; Debug.LogError(string.Format("{0}: {1}", request.Exception.Message, request.Exception.StackTrace)); } else { searchText = "Search failed!"; } } }
void OnGUI() { GUI.Label(new Rect(5, 5, 100, Screen.height / 10), "Search for user:"******"Search")) { searchResult = null; selectedUser = null; // Send search request ti Github HTTPRequest request = new HTTPRequest(new System.Uri(string.Format(Api_SearchUser, searchFor)), OnSearchForUsersFinished); request.UseAlternateSSL = true; request.Send(); searchStarted = true; searchText = "Searching"; } GUI.Box(new Rect(10, 5 + Screen.height / 10, Screen.width / 2, Screen.height - 35), string.Empty); if (!string.IsNullOrEmpty(searchText)) { GUI.Label(new Rect(10, 5 + Screen.height / 10, Screen.width / 2, Screen.height - 35), searchText); } if (searchResult != null && searchResult.users != null) { GUI.Label(new Rect(), "Users found:"); scrollPosition = GUI.BeginScrollView(new Rect(10, 5 + Screen.height / 10, Screen.width / 2, Screen.height - 35), scrollPosition, new Rect(10, 30, Screen.width / 2, searchResult.users.Count * 30), false, true); for (int i = 0; i < searchResult.users.Count; ++i) { if (GUI.Button(new Rect(10, i * 30, (Screen.width / 2) - 20, 25), searchResult.users[i].fullname)) { // Button pressed: change the selected user, and start download the avartar_url selectedUser = searchResult.users[i]; if (selectedUser.texture == null) { selectedUser.StartGetAvatarUrl(); } } } GUI.EndScrollView(); if (selectedUser != null) { float userinfoLeft = (Screen.width / 2) + 20; float userinfoWidth = (Screen.width / 2) - 35; GUI.Box(new Rect(userinfoLeft, 5 + Screen.height / 10, userinfoWidth, Screen.height - (Screen.height / 10) - 205), string.Empty); GUI.Label(new Rect(userinfoLeft + 5, 8 + Screen.height / 10, userinfoWidth, 25), selectedUser.fullname + "'s GitHub Profile"); // Draw user's profile picture if it's downloaded if (selectedUser.texture != null) { GUI.DrawTexture(new Rect(userinfoLeft + 3, 45 + Screen.height / 10, 128, 128), selectedUser.texture, ScaleMode.ScaleToFit); } else { GUI.Box(new Rect(userinfoLeft + 3, 45 + Screen.height / 10, 128, 128), "Loading..."); } GUI.Label(new Rect(userinfoLeft + 3, 180 + Screen.height / 10, userinfoWidth - 6, 25), string.Format("Username: {0}", selectedUser.username)); GUI.Label(new Rect(userinfoLeft + 3, 210 + Screen.height / 10, userinfoWidth - 6, 25), string.Format("Full name: {0}", selectedUser.fullname)); GUI.Label(new Rect(userinfoLeft + 3, 230 + Screen.height / 10, userinfoWidth - 6, 50), string.Format("Location: {0}", selectedUser.location)); } } // Go back to the demo selector if (GUI.Button(new Rect(20 + Screen.width / 2, Screen.height - 200, -30 + Screen.width / 2, 195), "Back")) { Application.LoadLevel(0); } }