Hello everyone, I was trying to “translate” a JS into a C# script. But I’ve just learned unity for 1 month and I kept making mistakes. I’ve finished tranlating most of it but there are still mistakes I wasn’t able to correct, can anybody help me plz? REALLY need help here…with a desperate and thankful heart~!
Original one:
#pragma strict
public var objname = ""; //this is the field where you put the name to request
var formText = ""; //this field is where the messages sent by PHP script will be in
var URL = "XXX"; //the distant script URL
public var textrect = Rect (10, 90, 500, 50); //just make a GUI object rectangle
function OnGUI() {
GUI.Label( Rect (10, 10, 80, 20), "Object name" ); //text with object name
var c = gameObject.GetComponent("OnScreenKeyboardExample");
//objname = OnScreenKeyboard.editableString;
//objname.GetType().GetField(editableString).GetValue(objname);
//objname =
//GUI.TextField ( Rect (90, 10, 100, 20), objname ); //here you will insert the new value to variable objname
if ( GUI.Button ( Rect (10, 50, 100, 20) , "Try" ) ) //just a button
{
Login();
}
GUI.TextArea( textrect, formText );
}
function Login() {
var form = new WWWForm(); //here you create a new form connection
form.AddField( "objectname", objname );
var w = WWW(URL, form); //here we create a var called 'w' and we sync with our URL and the form
yield w; //we wait for the form to check the PHP file, so our script dont just hang
if (w.error != null) {
print(w.error); //if there is an error, tells us
} else {
print("Test OK");
formText = w.data; //here we return the data our PHP told us
w.Dispose(); //clear our form in game
}
objname = ""; //just clean our variables
}
Modified one:
using UnityEngine;
using System.Collections;
public class asker : MonoBehaviour
{
public string objname = ""; //this is the field where you put the name to request
public string formText = ""; //this field is where the messages sent by PHP script will be in
public string url = "XXX"; //the distant script URL
public Rect textrect = new Rect(10, 90, 500, 50); //just make a GUI object rectangle
void OnGUI() {
GUI.Label(new Rect (10, 10, 80, 20), "Object name" ); //text with object name
// string c = gameObject.GetComponent("OnScreenKeyboardExample");
objname = OnScreenKeyboardExample.editableString;
//objname.GetType().GetField(editableString).GetValue(objname);
//objname =
//GUI.TextField ( Rect (90, 10, 100, 20), objname ); //here you will insert the new value to variable objname
if ( GUI.Button (new Rect (10, 50, 100, 20) , "Try" ) ) //just a button
{
Login();
}
GUI.TextArea( textrect, formText );
}
IEnumerator Login() {
WWWForm form = new WWWForm(); //here you create a new form connection
form.AddField( "objectname", objname );
static function w = new WWW(URL, form); //here we create a var called 'w' and we sync with our URL and the form
yield return w; //we wait for the form to check the PHP file, so our script dont just hang
if (w.error != null) {
print(w.error); //if there is an error, tells us
} else {
print("Test OK");
formText = w.data; //here we return the data our PHP told us
w.Dispose(); //clear our form in game
}
objname = ""; //just clean our variables
}
}