简体   繁体   English

如何在Unity 3D中使用C#脚本创建自定义3D名称

[英]How to create custom 3D name using c# script in Unity 3D

I'm stuck on ac# scripting problem in Unity that I would like to solve. 我陷入了要解决的Unity中的ac#脚本问题。

This is what I want to achieve: 这是我要实现的目标:

  1. Enter a name on an InputField 在InputField上输入名称
  2. Get Characters from the Input field 从输入字段获取字符
  3. For each Character: set the 3D GameObject of the Character already created in the Hierarchy 对于每个角色:设置已在层次结构中创建的角色的3D游戏对象
  4. SetCharacterPosition next to each other so they can create a name SetCharacterPosition彼此相邻,因此他们可以创建一个名称
  5. SetMaterial in this order (Blue, Green,Red,Yellow) and start over 依次设置材料(蓝色,绿色,红色,黄色)并重新开始
  6. View the given input name in a 3D Colored way on the screen 在屏幕上以3D彩色方式查看给定的输入名称

This is what I have done so far: 到目前为止,这是我所做的:

  1. Created 3D Prefabs of each Character and loaded into Hierarchy 为每个角色创建3D预制件并加载到层次结构中
  2. Created the GUI with an InputField and a Button (+Script) 用一个InputField和一个按钮(+脚本)创建了GUI
  3. Create a Button Script (button_scr) in order to get the name and split into Characters 创建一个按钮脚本(button_scr)以获取名称并将其拆分为字符

GUI 图形用户界面 到目前为止的设置 SCRIPT button_scr 脚本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);     
        }
    }
}

Some considerations: 一些注意事项:

  1. Names can have some repeated Characters 名称可以有一些重复的字符
  2. Names can have accent marks on vowels (Á, É,Í,Ó,Ú) 名称在元音上可以带有重音符号(Á,É,Í,Ó,Ú)
  3. Names can have "ñ" character 名称可以带有“ñ”字符

This is where I would like to get some orientation in order to solve the problem: 这是我想要解决问题的方向:

  • I have considered creating and filling a GameObject Array with all the references of each character like so: 我考虑过用每个角色的所有引用创建并填充GameObject数组,如下所示:
  • GameObject[0] = A 3D Character prefab
  • GameObject[1] = B 3D Character prefab
  • Then create a for loop to find the name character in the GameObject Array and make a copy in order to setup in the game 然后创建一个for循环以在GameObject Array中找到名称角色并进行复制以在游戏中进行设置
  • Set 3D character position 设定3D角色位置
  • Set Material to 3D character in a sequential order (Blue,Green,Red,Yellow) 按顺序将“材质”设置为3D字符(蓝色,绿色,红色,黄色)

Is there any easier way to do this that I'm missing? 有什么更简单的方法可以实现我所缺少的吗?

I would like to get some orientation/advice or some sample code where I can check similar issue solving. 我想获得一些方向/建议或一些示例代码,在这里我可以检查类似的问题解决方法。

this is a quick solution. 这是一个快速的解决方案。 not tested. 未经测试。 i dont have unity installed. 我没有统一安装。 just for you to have an idea. 只为您有一个主意。

Yes. 是。 is better to assign all the letters in a array for optimization. 最好在数组中分配所有字母以进行优化。 Also deactivate all the letters in Awake() 同时停用Awake()中的所有字母

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);  
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM