如何在Unity 3D中使用c#脚本创建自定义3D名称
我在Unity中遇到了一个c#脚本问题,我想解决这个问题 这就是我想要实现的目标:如何在Unity 3D中使用c#脚本创建自定义3D名称,c#,unity3d,3d,unity5,gameobject,C#,Unity3d,3d,Unity5,Gameobject,我在Unity中遇到了一个c#脚本问题,我想解决这个问题 这就是我想要实现的目标: 在输入字段中输入名称 从输入字段中获取字符 对于每个角色:设置已在层次中创建的角色的3D游戏对象 将CharacterPosition设置为彼此相邻,以便它们可以创建名称 按此顺序设置材质(蓝色、绿色、红色、黄色),然后重新开始 在屏幕上以3D彩色方式查看给定的输入名称 这就是我到目前为止所做的: 创建每个角色的3D预设并加载到层次结构中 使用InputField和按钮(+脚本)创建GUI 创建一个按钮脚本(Bu
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);
}
}
}
公共类按钮\u scr:monobhavior
{
公共按钮myButton;
公共投入领域;
公共游戏对象go;
无效开始()
{
myButton=GetComponent();
myButton.onClick.AddListener(ProcessText);
}
void ProcessText()
{
Log(“您的名字是:“+un.text”);
getCharacters(联合国文本);
}
无效getCharacters(字符串文本)
{
foreach(文本中的字符c)
{
go=GameObject.Find(“+char.ToUpper(c));
go.SetActive(true);
//SetPosiConnectExt2EachOther
//按此顺序设置彩色材质(蓝色、绿色、红色、黄色)
Debug.Log(“游戏对象名称日志:“+go.name”);
}
}
}
一些考虑:
- 我曾考虑过创建一个游戏对象数组,并用所有 每个字符的引用如下所示:
GameObject[0]=一个3D角色预制体
GameObject[1]=B 3D角色预制
- 然后创建一个for循环,在GameObject数组中查找名称角色,并制作一个副本,以便在游戏中进行设置
- 设置三维角色位置
- 按顺序(蓝色、绿色、红色、黄色)将材质设置为3D角色
我想获得一些指导/建议或一些示例代码,以便检查类似问题的解决。这是一个快速解决方案。没有测试。我没有安装unity。只是为了让你有个想法 对。最好将数组中的所有字母都指定为优化。同时停用Awake()中的所有字母
公共材料[];//按所需顺序指定材质
public void getCharacters(字符串名称)
{
int-materialIndex=0;
对于(int i=0;i
}这是一个快速解决方案。没有测试。我没有安装unity。只是为了让你有个想法 对。最好将数组中的所有字母都指定为优化。同时停用Awake()中的所有字母
公共材料[];//按所需顺序指定材质
public void getCharacters(字符串名称)
{
int-materialIndex=0;
对于(int i=0;i
}我正试图决定你的问题是否太宽泛和/或应该转移到其他地方。除非你有一个具体的问题要解决,否则我会考虑后者。我会尝试决定你的问题是否过于宽泛和/或应该转移到其他地方。我想是后者,除非你有一个具体的问题要解决。
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);
}