WordScramble.cs
[System.Serializable]
public class Result
{
public int totalScore = 0;
[Header(“REF UI”)]
public Text textTime;
public Text textTotalScore;
[Header("REF RESULT SCREEN")]
public GameObject resultCanvas;
public Image[] stars;
public Text textResultScore;
public Text textInfo;
[Space(10)]
public Color starOn;
public Color starOff;
public void ShowResult()
{
textResultScore.text = totalScore.ToString();
textInfo.text = "You finished " + WordScramble.main.words.Length + " words!";
int allTimeLimit = WordScramble.main.GetAllTimeLimit();
for (int i = 0; i < stars.Length; i++)
{
stars*.color = totalScore >= allTimeLimit / (3 - i) ? starOn : starOff;*
}
resultCanvas.SetActive(true);
}
}
[System.Serializable]
public class Word
{
- public string word;*
- [Header(“leave empty if you want randomized”)]*
- public string desiredRandom;*
- [Space(10)]*
- public float timeLimit;*
- public string GetString()*
- {*
-
if (!string.IsNullOrEmpty (desiredRandom))*
-
{*
-
return desiredRandom;*
-
}*
-
string result = word;*
-
result = "";*
while (result == word)
-
{*
-
List<char> characters = new List<char> (word.ToCharArray ());*
-
while (characters.Count > 0)*
-
{*
-
int indexChar = Random.Range (0, characters.Count - 1);*
-
result += characters [indexChar];*
-
characters.RemoveAt (indexChar);*
-
}*
-
}*
-
return result;*
-
}*
}
public class WordScramble : MonoBehaviour { -
public Word words;*
-
[Space(10)]*
-
public Result result;*
[Header(“UI REFERENCE”)]
public GameObject wordCanvas; -
public CharObject prefab;*
-
public Transform container;*
-
public float space;*
-
public float lerpSpeed = 5;*
-
List charObjects = new List ();*
-
CharObject firstSelected;*
-
public int currentWord;*
-
public static WordScramble main;*
-
public static float totalScore;*
-
void Awake()*
-
{*
-
main = this;*
-
}*
-
// Use this for initialization*
-
void Start () {*
-
ShowScramble (currentWord);*
-
result.textTotalScore.text = result.totalScore.ToString ();*
-
}*
-
// Update is called once per frame*
-
void Update ()*
-
{*
-
RepositionObject ();*
_ totalScore = Mathf.Lerp (totalScore, result.totalScore, Time.deltaTime * 5);_
-
result.textTotalScore.text = Mathf.RoundToInt (totalScore).ToString ();*
- }*
public int GetAllTimeLimit()
{
float result = 0;
foreach (Word w in words)
{
result += w.timeLimit / 2;
}
return Mathf.RoundToInt(result);
} - //
* - //show word from collection with desired index*
- //*
- void RepositionObject()*
- {*
-
if (charObjects.Count == 0)*
-
{*
-
return;*
-
}*
-
float center = (charObjects.Count - 1) / 2;*
-
for (int i = 0; i < charObjects.Count; i++)*
-
{*
charObjects .rectTransform.anchoredPosition = Vector2.Lerp(charObjects_.rectTransform.anchoredPosition, new Vector2 ((i - center) * space, 0), lerpSpeed * Time.deltaTime);
charObjects .index = i;
* }
}
//Show a random word to the screen*
* public void ShowScramble()
{
ShowScramble (Random.Range (0, words.Length - 1));
}*_
* //show word from collection with desired index*
* public void ShowScramble(int index)*
* {*
* charObjects.Clear ();*
* foreach (Transform child in container)*
* {*
* Destroy (child.gameObject);*
* }*
* //WORDS FINISHED*
//SHOW RESULT SCREEN
* if (index > words.Length - 1)*
* {*
result.ShowResult();
wordCanvas.SetActive(false);
* //Debug.Log (“index out of range, please enter range between 0-” + (words.Length - 1).ToString());*
* return;*
* }*
* char[] chars = words [index].GetString ().ToCharArray ();*
* foreach (char c in chars)*
* {*
* CharObject clone = Instantiate (prefab.gameObject).GetComponent ();*
* clone.transform.SetParent (container);*
* charObjects.Add (clone.Init (c));*
* }*
* currentWord = index;*
* StartCoroutine (TimeLimit());*
* }*
* public void Swap (int indexA, int indexB)*
* {*
* CharObject tmpA = charObjects [indexA];*
* charObjects [indexA] = charObjects [indexB];*
* charObjects [indexB] = tmpA;*
* charObjects [indexA].transform.SetAsLastSibling ();*
* charObjects [indexB].transform.SetAsLastSibling ();*
* CheckWord ();*
* }*
* public void Select(CharObject charObject)*
* {*
* if (firstSelected)*
* {*
* Swap (firstSelected.index, charObject.index);*
* //Unselected*
* firstSelected.Select ();*
* charObject.Select ();*
* }*
* else*
* {*
* firstSelected = charObject;*
* }*
* }*
* public void UnSelect()*
* {*
* firstSelected = null;*
* }*
* public void CheckWord()*
* {*
* StartCoroutine (CoCheckWord ());*
* }*
* IEnumerator CoCheckWord ()*
* {*
* yield return new WaitForSeconds (0.5f);*
* string word = “”;*
* foreach (CharObject charObject in charObjects)*
* {*
* word += charObject.character;*
* }*
* if (timeLimit <= 0)*
* {*
* currentWord++;*
* ShowScramble (currentWord);*
* yield break;*
* }*
* if (word == words [currentWord].word)*
* {*
* currentWord++;*
* result.totalScore += Mathf.RoundToInt (timeLimit);*
* //StopCoroutine (TimeLimit ());*
* ShowScramble (currentWord);*
* }*
* }*
* float timeLimit;*
* IEnumerator TimeLimit()*
* {*
* timeLimit = words [currentWord].timeLimit;*
* result.textTime.text = Mathf.RoundToInt (timeLimit).ToString ();*
* int myWord = currentWord;*
* yield return new WaitForSeconds (1);*
* while (timeLimit > 0)*
* {*
* if (myWord != currentWord) { yield break; }*
* timeLimit -= Time.deltaTime;*
* result.textTime.text = Mathf.RoundToInt (timeLimit).ToString ();*
* yield return null;*
* }*
* CheckWord ();*
* }*
}
/////////////////////////////////////////////////////////////////////////////////////////////
CharObject.cs
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Collections;
using System.Collections.Generic;
public class CharObject : MonoBehaviour {
* public char character;*
* public Text text;*
* public Image image;*
* public RectTransform rectTransform;*
* public int index;*
* [Header(“Appearance”)]*
* public Color normalColor;*
* public Color selectedColor;*
* bool isSelected = false;*
* public CharObject Init (char c)*
* {*
* character = c;*
* text.text = c.ToString ();*
* gameObject.SetActive (true);*
* return this;*
* }*
* public void Select()*
* {*
* isSelected = !isSelected;*
* image.color = isSelected ? selectedColor : normalColor;*
* if (isSelected) {*
* WordScramble.main.Select (this);*
* } else {*
* WordScramble.main.UnSelect ();*
* }*
* }*
}