- Home /
Convert Input Field to String
ok so ive got this code working for manual input of username and scores but i would like to use the ui input field for users to input there name....i just cant seem to figure out how to do this any help would be nice
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Highscores : MonoBehaviour {
const string privateCode = "";
const string publicCode = "";
const string webURL = "";
DisplayHighscores highscoreDisplay;
public Highscore[] highscoresList;
static Highscores instance;
void Awake() {
highscoreDisplay = GetComponent<DisplayHighscores> ();
instance = this;
}
public static void AddNewHighscore(string username, int score) {
instance.StartCoroutine(instance.UploadNewHighscore(username,score));
}
IEnumerator UploadNewHighscore(string username, int score) {
WWW www = new WWW(webURL + privateCode + "/add/" + WWW.EscapeURL(username) + "/" + score);
yield return www;
if (string.IsNullOrEmpty(www.error)) {
print ("Upload Successful");
DownloadHighscores();
}
else {
print ("Error uploading: " + www.error);
}
}
public void DownloadHighscores() {
StartCoroutine("DownloadHighscoresFromDatabase");
}
IEnumerator DownloadHighscoresFromDatabase() {
WWW www = new WWW(webURL + publicCode + "/pipe/");
yield return www;
if (string.IsNullOrEmpty (www.error)) {
FormatHighscores (www.text);
highscoreDisplay.OnHighscoresDownloaded(highscoresList);
}
else {
print ("Error Downloading: " + www.error);
}
}
void FormatHighscores(string textStream) {
string[] entries = textStream.Split(new char[] {'\n'}, System.StringSplitOptions.RemoveEmptyEntries);
highscoresList = new Highscore[entries.Length];
for (int i = 0; i <entries.Length; i ++) {
string[] entryInfo = entries[i].Split(new char[] {'|'});
string username = entryInfo[0];
int score = int.Parse(entryInfo[1]);
highscoresList[i] = new Highscore(username,score);
print (highscoresList[i].username + ": " + highscoresList[i].score);
}
}
}
public struct Highscore {
public string username;
public int score;
public Highscore(string _username, int _score) {
username = _username;
score = _score;
}
}
[1]: http://dreamlo.com/
We need to ask us a specific things for us to help, try not to just paste your entire code and ask how to fix a random thing to it.
You will need to drag in a text UI and assign a var to it and change with get GetComponent()
http://answers.unity3d.com/questions/777335/46-ui-changing-the-text-component-via-script.html
i apoligize im new to posting questions here.... to mannually upload a score i user the awake funtion like below but i would like to user userinput ins$$anonymous$$d
public Awake()
{
AddNewHighscore("Ryan",500);
}
public static void AddNewHighscore(string username, int score)
{
instance.StartCoroutine(instance.UploadNewHighscore(username,score)); }
IEnumerator UploadNewHighscore(string username, int score)
{
WWW www = new WWW(webURL + privateCode + "/add/" + WWW.EscapeURL(username) + "/" + score);
yield return www;
if (string.IsNullOrEmpty(www.error)) {
print ("Upload Successful");
DownloadHighscores();
}
else {
print ("Error uploading: " + www.error);
}
}
No problem, So if I understand your question correct you want to upload the score when a user inputs their name?
You are using UI anyways? Why not just use their button functions ins$$anonymous$$d so when a player inputs their name and clicks a button you can pass their name and score as an argument to whatever function you want.
https://unity3d.com/learn/tutorials/modules/beginner/ui/ui-events-and-event-triggers
Once you create a button in unity and assign the call function when a user presses down you can call AddNewHighscore You just have to make the function public and have the class attached to the object. Either way watch the video.
From there you can make anything: public void AddNewHighscore(string userName, int Score) { //Upload score and name }
yes that is what im trying to do everything i have tried has not worked for instance in android when i pass user input i do something like
String username = userName.getText.toString;
is it sorta the same for unity user input?
Oh, Okay I am not too familiar with android string formatting but you can try String username = userName.toString();