2017-05-31 47 views
0

我困在Unity中的一個c#腳本問題,我想解決這個問題。如何在Unity 3D中使用c#腳本創建自定義3D名稱

這就是我想要達到的目標:

  1. 從輸入字段
  2. 上InputField
  3. 獲取漢字輸入一個名稱,每個字符:設置在已創建的角色的3D遊戲對象層次
  4. SetCharacterPosition彼此相鄰,使他們能夠創建一個名稱
  5. SetMaterial順序(藍,綠,紅,黃),並開始在
  6. 在3D彩色方式查看給定的輸入名稱在屏幕上

這是我迄今所做的:

  1. 創建的每個字符的3D預製件和裝入層次
  2. 創建與InputField和一個按鈕(+腳本)
  3. ,以獲取名稱和分裂成字符創建一個按鈕腳本(button_scr)的GUI

GUI The setup so far SCRIPT button_scr

public class button_scr : MonoBehaviour 
{ 
    public Button myButton; 
    public InputField un; 
    public GameObject go; 

    void Start() 
    { 
     myButton = GetComponent<Button>(); 
     myButton.onClick.AddListener(ProcessText); 
    } 

    void ProcessText() 
    { 
     Debug.Log("Your name is : "+un.text); 
     getCharacters(un.text); 
    } 

    void getCharacters(string text) 
    { 
     foreach (char c in text) 
     { 
      go = GameObject.Find(""+char.ToUpper(c)); 
      go.SetActive (true); 
      // setPosicionNext2EachOther 
      // setColoredMaterial(Blue,Green,Red,Yellow) in this order 
      Debug.Log("GameObject name Log: "+go.name);  
     } 
    } 
} 

一些注意事項:

  1. 名可以有一些重複的字符
  2. 名稱可以有元音重音符號(A,E,I ,Ó,Ú)
  3. 名稱可以有「ñ」字符

這是我想獲得一些學習的,爲了解決這個問題:

  • 我已經考慮創建和填充一個遊戲對象數組與所有 每個字符,像這樣的引用:
  • GameObject[0] = A 3D Character prefab
  • GameObject[1] = B 3D Character prefab
  • 然後創建一個for循環來查找遊戲對象數組名稱的字符,然後複印一份,以安裝在遊戲
  • 設置3D字符位置
  • 設定材料到3D角色的順序(藍,綠,紅,黃)

有沒有更簡單的方法來做到這一點,我失蹤?

我想得到一些方向/建議或一些示例代碼,我可以檢查類似的問題解決。

+0

我試圖決定,如果你的問題是過於寬泛和/或應該被遷移到[codereview.SE(https://開頭代碼審查.stackexchange.com /)。我想後者,除非你有一個具體的問題,你試圖解決。 – Draco18s

回答

2

這是一個快速解決方案。未經測試。我沒有統一安裝。只是爲了你有一個想法。

是的。最好分配一個數組中的所有字母進行優化。此外停用清醒()中的所有字母

public Material materials[]; // assign the materials in the order you want 

public void getCharacters (string name) 
{ 
int materialIndex = 0; 

for (int i = 0; i < text.lenght; i++) 
{ 
    char c = char.ToUpper(text[i]); 
    GameObject go = GameObject.Find("" + c); 
    go.SetActive (true); 

    go.transform.position = new Vector3(i, 0, 0); // place the letters in the x axis separated by one meter 

    // change the material 
    go.GetCOmponent<Renderer>().sharedMaterial = materials[materialIndex];  
    materialIndex = (materialIndex + 1) % materials.length; // cycle the materials 

    // setPosicionNext2EachOther 
    // setColoredMaterial(Blue,Green,Red,Yellow) in this order 
    //Debug.Log("GameObject name Log: "+go.name); 
} 

}