Tải bản đầy đủ (.pdf) (2 trang)

Lecture 229 number wizard UI unityscript translation tủ tài liệu training

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (94.34 KB, 2 trang )

Translating Number Wizard UI to UnityScript

Don’t forget that the project is attached to this lecture!

What’s new?
In translating Number Wizard UI from C# to UnityScript, we encounter no new features of the UnityScript
language. In particular, the NumberWizard UnityScript class is almost identical to the previous section’s solution.
We simply remove a few logging statements and re-order some of the execution to avoid some pitfalls. Similarly, the
LevelManager class we introduce in this section translates to UnityScript in a straight forward manner. The only
thing worth noting is how we have access to the complete UnityEngine APIs from UnityScript, so the logic of the
code is almost identical. For example, accessing the UnityEngie.UI namespace is straight-forward.
Worth noticing is that we correct a slight typo in the class naming from the C# codebase by changing NumberWizards
in the C# project to NumberWizard.
That said, the effort to translate the project was not in the code translation, but in recreating the connections
made in the editor, using drag and drop in the inspector. For example, reconnecting the guess Text object to the
NumberWizard script in the Game scene. Doing this methodically required going through every object in the scenes
and checking for their missing scripts manually. This is error prone and the compiler will rarely catch missing objects
and null references for us. This workload can potentially be reduced in two ways:
• Link and reference scripts programmatically instead of using the editor’s drag and drop.
• Use Prefabs, which would allow scripts to be changed only on the prefab itself without the need to reconnect
scene objects individually.
In practice, translating an entire project is a rare undertaking, as most projects will use one main language or will
keep old files as they are when switching to a new language and only translate them when they are being edited for
another reason anyway, so the problems with scripts needing to be reconnected will be dealt with piecemeal or not
at all.
For completeness, here are the NumberWizard.js and LevelManager.js files:

NumberWizard
#pragma strict
import UnityEngine.UI;
public var guessText: Text;


private var max: int = 1000;
private var min: int = 1;
private var guess: int;
private function Start() {
StartGame();
}
private function StartGame() {
max = max + 1;
NextGuess();
}
public function GuessHigher(){
min = guess;
NextGuess();
}

Learn more at www.CompleteUnityDeveloper.com


Translating Number Wizard UI to UnityScript

public function GuessLower(){
max = guess;
NextGuess ();
}
public function GuessCorrect(){
StartGame ();
}
private function NextGuess() {
guess = Random.Range(min, max);
print ("Next guess is " + guess);

guessText.text = guess.ToString();
}

LevelManager
#pragma strict
public function LoadLevel(name: String){
Debug.Log ("New Level load: " + name);
Application.LoadLevel (name);
}
public function QuitRequest(){
Debug.Log ("Quit requested");
Application.Quit ();
}

Learn more at www.CompleteUnityDeveloper.com



×