【问题标题】:How to create custom 3D name using c# script in Unity 3D如何在 Unity 3D 中使用 c# 脚本创建自定义 3D 名称
【发布时间】:2017-05-31 09:08:57
【问题描述】:

我在 Unity 中遇到了一个我想解决的 c# 脚本问题。

这就是我想要实现的目标:

  1. 在 InputField 上输入名称
  2. 从输入字段中获取字符
  3. 对于每个角色:设置已在层次结构中创建的角色的 3D 游戏对象
  4. SetCharacterPosition 彼此相邻,以便他们可以创建名称
  5. 按此顺序(蓝色、绿色、红色、黄色)设置材料并重新开始
  6. 在屏幕上以 3D 彩色方式查看给定的输入名称

这是我到目前为止所做的:

  1. 为每个角色创建 3D 预制件并加载到层次结构中
  2. 使用 InputField 和按钮 (+Script) 创建了 GUI
  3. 创建一个按钮脚本 (button_scr) 以获取名称并拆分为字符

图形界面 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. 名称可以在元音上带有重音符号(Á、É、Í、Ó、Ú)
  3. 名称可以包含“ñ”字符

这是我想了解一些方向以解决问题的地方:

  • 我考虑过创建一个 GameObject Array 并用所有 每个字符的引用如下:
  • GameObject[0] = A 3D Character prefab
  • GameObject[1] = B 3D Character prefab
  • 然后创建一个 for 循环以在 GameObject Array 中查找名称字符并制作副本以便在游戏中进行设置
  • 设置 3D 角色位置
  • 按顺序(蓝、绿、红、黄)将材质设置为 3D 角色

有没有我想念的更简单的方法来做到这一点?

我想获得一些方向/建议或一些示例代码,我可以在其中检查类似的问题解决方案。

【问题讨论】:

  • 我正在尝试确定您的问题是否过于宽泛和/或应该迁移到codereview.SE。除非您有要解决的特定问题,否则我会考虑后者。

标签: c# unity3d 3d unity5 gameobject


【解决方案1】:

这是一个快速的解决方案。未测试。我没有安装统一。只是为了你有一个想法。

是的。最好将所有字母分配在一个数组中以进行优化。同时停用 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);  
}

}

【讨论】:

    猜你喜欢
    • 2015-01-10
    • 2021-12-24
    • 1970-01-01
    • 1970-01-01
    • 2016-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多