Make A WORD

using UnityEngine;
using System.Collections;

public class HolderValue : MonoBehaviour {
    public int order;

    public void SetOrder(int value)
    {
        order = value;
    }

    public int GetOrder()
    {
        return order;
    }

    void OnMouseExit()
    {
        GameManager.Instance.letterOrder = -1;
    }

    void OnMouseOver()
    {
        GameManager.Instance.letterOrder = order;
    }
}
using UnityEngine;
using System.Collections;

public class HolderValue : MonoBehaviour {
    public int order;

    public void SetOrder(int value)
    {
        order = value;
    }

    public int GetOrder()
    {
        return order;
    }

    void OnMouseExit()
    {
        GameManager.Instance.letterOrder = -1;
    }

    void OnMouseOver()
    {
        GameManager.Instance.letterOrder = order;
    }
}

using UnityEngine;
using System;
using System.Linq;//与超文本把标记语言有关
using System.Collections;
using System.Collections.Generic;

public class GameManager : MonoBehaviour
{
    public bool useDictionary;                      // Should we use the dictionary?
    public TextAsset dictionary;                    // If useDictionary is true, this is the dictionary that will be used.将字典txt文件赋值
    public string[] words;
    public bool rotateTiles = false;                // if true, tiles with letters will be rotated. This can make game harder.
    public GameObject letterTile;
    public GameObject letterHolder;
    public GameObject parentObject;
    public float spacing;                           // Spacing between the letter fields
    //自己添加的:
    public Dictionary
   
   
    
    subject=new Dictionary
    
    
     
     ( );
    public Dictionary
     
     
      
       predicate = new Dictionary
      
      
       
       ();
    public Dictionary
       
       
         accusative = new Dictionary 
        
          (); // don't change following variables // if not sure what you are doing private bool showMsg, showHint = false; private int hintPos; private Ray ray; private RaycastHit hit; private GameObject current, backObj, parentObj; private string _word;//改动 private Vector3 center; private string[] tempValues; [HideInInspector] public bool container = false; private string tileName = "tileName"; private string holderName = "holderName"; private string word, message, hintString; [HideInInspector] public string letterHolderValue; [HideInInspector] public int letterOrder; [HideInInspector] public bool selected = false; private static GameManager instance; public static GameManager Instance { get { return instance; } } void Awake() { instance = this; } public void Dic() { subject.Add("0","你"); subject.Add("1", "我"); // predicate.Add("0","去"); predicate.Add("1","喜欢"); // accusative.Add("0","西湖"); accusative.Add("1","天堂"); } void Start() { Dic(); // If we use dictionary, explode it into array... if (useDictionary) { words = dictionary.text.Split(';');//每个词之间以;划分 } // ...else stop if there are no user defined words. else if (words.Length==0) { Debug.LogError("You don't have any words!");//没有预先存储的字典 return; } // if everything's OK, load game LoadGame();//载入字典 } void LoadGame() { GameObject tempObj; Vector2 pos; // define center position center = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width/2, Screen.height/2, Camera.main.nearClipPlane));//屏幕的中心位置 showHint = false; // Instantiate parent object for other game objects parentObj = Instantiate(parentObject, transform.position, Quaternion.identity) as GameObject;//生成一个空物体,下含holdername titlename // randomize the words in our list... Shuffle(words);//打乱字典的单词顺序 // ...pick one word... word = words[UnityEngine.Random.Range(0, words.Length)];//从字典中随机挑一个单词给word // ...and split to the array _word = word;//存储到新数组中 toCharArray()的用法:将字符串对象中的字符转换为一个字符数组 改动:_word = word.tocharArray; _word = subject[_word]; // spacing between tiles can never be below 1 spacing = Mathf.Clamp(spacing, 1, float.MaxValue);//float.maxvalue float的最大精度 使两个字母间的空间大于一 // center field elements as much as possible bool left = false; int leftv = 0, rightv = 1; Dictionary 
         
           fields = new Dictionary 
          
            (); for (int i = 0; i < _word.Length; i++) //按照单词字母个数生成holderUI并将holder物体及位置加到字典中 { left = !left;//使生成holder是从中间开始一左一右,一左一右这样生成 if (left) { tempObj = Instantiate(letterHolder, Camera.main.ScreenToWorldPoint(new Vector3(((Screen.width / 2)) - (leftv * 38 * letterHolder.transform.localScale.x * spacing), center.y, 200)), Quaternion.identity) as GameObject; leftv++; } else { tempObj = Instantiate(letterHolder, Camera.main.ScreenToWorldPoint(new Vector3(((Screen.width / 2)) + (rightv * 38 * letterHolder.transform.localScale.x * spacing), center.y, 200)), Quaternion.identity) as GameObject; rightv++; } fields.Add(tempObj, tempObj.transform.position.x);//将holder物体及位置加到字典中 } // sort field elements fields = fields.OrderBy(x => x.Value).ToDictionary(x => x.Key, x => x.Value);//使用了Linq leftv = 0; foreach (KeyValuePair 
           
             targ in fields)//遍历field 将生成的holder化为Parent的子物体并命名 { targ.Key.GetComponent 
            
              ().SetOrder(leftv);//SetHolder继承自HolderValue类 targ.Key.name = holderName + leftv.ToString();//给生成的holdername命名,例如:leftv=0则生成“holdername0” targ.Key.transform.parent = parentObj.transform;//将实例化的holdername 化为Parent的子物体 Vector3 p = Camera.main.WorldToScreenPoint(targ.Key.transform.position); leftv++; } tempValues = new string[_word.Length]; // finnaly, instantiate letters for (int i = 0; i < _word.Length; i++)//生成字母划到Parent下面并旋转 {//什么意思??? pos = new Vector2(UnityEngine.Random.Range(100, Screen.width - 100), UnityEngine.Random.Range(Screen.height - 50, Screen.height - 200)); tempObj = Instantiate(letterTile, Camera.main.ScreenToWorldPoint(new Vector3(pos.x, pos.y, 10)), Quaternion.identity) as GameObject; tempObj.GetComponent 
             
               ().SetValue(_word);//继承自LetterValue _word[i].ToString() tempObj.name = tileName + i.ToString(); //类似给生成的holdername命名,例如:leftv = 0则生成“holdername0” tempObj.transform.parent = parentObj.transform; if (rotateTiles)//旋转字母 { tempObj.transform.eulerAngles = new Vector3(0, UnityEngine.Random.Range(0, 360), 0);//旋转letter的UI } } } void Update() { // if left button is clicked and there's no selected letter if (Input.GetMouseButtonDown(0) && !selected) { container = false; letterOrder = -1; letterHolderValue = ""; // check if we have clicked the letter... ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit)) { // ...and select it if (hit.transform.gameObject.name.Contains(tileName))//判断是否选中了字母 { current = hit.transform.gameObject; current.layer = 2;//改变字母的层级 selected = true; } } } // selected letter draging if (Input.GetMouseButton(0) && selected)//按下鼠标且选中了 { current.transform.position = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, current.transform.position.z));//拖拽 } // on left mouse release if (Input.GetMouseButtonUp(0) && selected)//放开鼠标 { current.layer = 0; // if we are not over field, drop letter if (letterOrder == -1)//值改变在HolderValue类 可是怎么做到的呢? { selected = false; return; } // else place letter in right fieled else { current.transform.eulerAngles = new Vector3(0, 0, 0);//正位 tempValues[letterOrder] = current.GetComponent 
              
                ().GetValue();//将textvalue赋给tempvaluese current.transform.position = new Vector3(GameObject.Find(holderName + letterOrder.ToString()).transform.position.x, current.transform.position.y, GameObject.Find("holderName" + letterOrder.ToString()).transform.position.z);//当前字母位置与目前holder一致 selected = false; } } } // randomaize elements in array /* private void Shuffle(char[] letters) { for (int t = 0; t < letters.Length; t++) { char tmp = letters[t]; int r = UnityEngine.Random.Range(t, letters.Length); letters[t] = letters[r]; letters[r] = tmp; } }*/ // randomaize elements in array private void Shuffle(string[] letters)//洗牌:将字母顺序重新排序 { for (int t = 0; t < letters.Length; t++) { string tmp = letters[t]; int r = UnityEngine.Random.Range(t, letters.Length);//在t与letters.length之间随机挑一个数 letters[t] = letters[r]; letters[r] = tmp; } } // check if our word is correct private bool Check()//判断答案是否正确 Check为布尔类 { bool result = true;//先假定为正确的 string answer = ""; for (int j = 0; j < tempValues.Length; j++) { answer += tempValues[j];//将目前字母搭配给answer } if (answer.ToLower() != word.ToLower())//String.ToLower 方法:返回此字符串转换为小写形式的副本。 { result = false; } return result; } // show short message private IEnumerator ShowMsg(string msg)//IEnumerator:接口 用于更新UI { message = msg; showMsg = true; yield return new WaitForSeconds(5);//等待5秒后执行下面的代码 showMsg = false; message = ""; } void OnGUI()//绘制UI { // show message with game status if (showMsg) { GUI.Box(new Rect((Screen.width / 2) - 210, Screen.height - 140, 420, 30), message); } if (showHint && !showMsg)//提示内容显示 { GUI.Box(new Rect((Screen.width / 2) - 210, Screen.height - 140, 420, 30), hintString); } if (GUI.Button(new Rect((Screen.width / 2) - 100, Screen.height - 100, 200, 30), "Hint"))//绘制提示UI { if (showHint) return; hintString = ""; for (int i = 0; i < word.Length; i++) { hintString += "*"; } hintPos = UnityEngine.Random.Range(0, word.Length); hintString = hintString.Remove(hintPos, 1); hintString = hintString.Insert(hintPos, word.Substring(hintPos, 1)); showHint = true; showMsg = false; } if (GUI.Button(new Rect((Screen.width / 2) - 320, Screen.height - 100, 200, 30), "检查"))//对错UI { showMsg = false; if (Check()) { StartCoroutine(ShowMsg("That's correct!"));//与迭代接口有关 //新游戏 Destroy(parentObj); LoadGame(); } else { StartCoroutine(ShowMsg("Not correct!")); } } if (GUI.Button(new Rect((Screen.width / 2) + 120, Screen.height - 100, 200, 30), "I don't know")) { showMsg = false; StartCoroutine(ShowMsg("The word was: " +_word)); Destroy(parentObj); LoadGame(); } } } 
               
              
             
            
           
          
         
       
      
      
     
     
    
    
   
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值