C# 无法通过游戏菜单在Unity中生成基本人物
老板让我做一个项目,用原始对象和一个独特的对象创建一个基本场景。我创建了对象以及第一人称相机/角色等。唯一错误的是,每次我运行脚本并启动游戏时,每次我单击菜单切换按钮时,所有需要放置的表单都会不断生成,无论玩家在哪里观看。我想要的是玩家切换菜单,点击选项,点击后,对象出现(单个对象),玩家可以将其放置在场景中的任何位置 这是unity的C代码。我认为这是代码的产卵是什么不断循环,但我希望它停止循环C# 无法通过游戏菜单在Unity中生成基本人物,c#,unity3d,unityscript,C#,Unity3d,Unityscript,老板让我做一个项目,用原始对象和一个独特的对象创建一个基本场景。我创建了对象以及第一人称相机/角色等。唯一错误的是,每次我运行脚本并启动游戏时,每次我单击菜单切换按钮时,所有需要放置的表单都会不断生成,无论玩家在哪里观看。我想要的是玩家切换菜单,点击选项,点击后,对象出现(单个对象),玩家可以将其放置在场景中的任何位置 这是unity的C代码。我认为这是代码的产卵是什么不断循环,但我希望它停止循环 public class menuScript : MonoBehaviour { public
public class menuScript : MonoBehaviour {
public GameObject[] items;
private itemplacement itemPlacement;
private bool menuWindowToggle = false;
private Rect menuWindowRect = new Rect(300,100,400,400);
private Dictionary<int,string> inventoryNameDictionary;
// icons
public Texture2D cubeIcon;
public Texture2D columnIcon;
public Texture2D sphereIcon;
public Texture2D robotIcon;
// Use this for initialization
void Start () {
itemPlacement = GetComponent<itemplacement> ();
}
// Update is called once per frame
void Update () {
}
void OnGUI()
{
for (int i = 0; i <items.Length; i ++) {
menuWindowToggle = GUI.Toggle (new Rect (800, 50, 100, 50), menuWindowToggle, "Item Menu");
if (menuWindowToggle) {
menuWindowRect = GUI.Window (0, menuWindowRect, menuWindowMethod, "Item Menu");
itemPlacement.SetItem (items [i]);
}
}
}
void menuWindowMethod (int windowId)
{
Dictionary<int,string> inventoryNameDictionary = new Dictionary<int,string> ()
{
{0, string.Empty},
{1, string.Empty},
{2, string.Empty},
{3, string.Empty}
};
itemClass cube = new itemClass (0, "Cube", cubeIcon, "Top of the line IKEA CUBE, made to break easily");
itemClass column = new itemClass (1, "Column", columnIcon, "Best Persian Column in world, no need paint, it perfect!!!");
itemClass sphere = new itemClass (2, "Sphere", sphereIcon, "ITS SPHERICAL.... SPHERICAL!!!");
itemClass robot = new itemClass (3, "Robot", robotIcon, "Mysterious thing, comes from emails about projects");
//display inventory
inventoryNameDictionary [0] = cube.name;
inventoryNameDictionary [1] = column.name;
inventoryNameDictionary [2] = sphere.name;
inventoryNameDictionary [3] = robot.name;
GUILayout.BeginArea (new Rect (5, 50, 395, 400));
GUILayout.BeginVertical ();
GUILayout.Button (inventoryNameDictionary[0], GUILayout.Height (50));
GUILayout.Button (inventoryNameDictionary[1], GUILayout.Height (50));
GUILayout.Button (inventoryNameDictionary[2], GUILayout.Height (50));
GUILayout.Button (inventoryNameDictionary[3], GUILayout.Height (50));
GUILayout.EndVertical ();
GUILayout.EndArea ();
}
public class itemClass
{
public int item;
public string name;
public Texture2D icon;
public string description;
public itemClass( int ide, string nam, Texture2D ico, string des)
{
item = ide;
name = nam;
icon = ico;
description = des;
}
}
}我实际上并不熟悉旧的GUI内容(Unity显然不再推荐使用它),所以我在这里查找: 我对您发布的代码的理解是GUI.Window()绘制您的窗口,使用menuWindowMethod()绘制内容,但这只绘制窗口。它不会等待用户在它内部做一些事情。因此,在这里编写代码时,将立即继续执行SetItem调用 由于OnGUI在每一帧都会被调用,并且Toggle控件总是返回其当前值,因此在打开它的那一刻,您将运行该循环,并为每一帧的items中的每个项调用一次SetItem
我怀疑您真正想做的是连接这些按钮,以便在单击itemplacement组件时在其上设置新的currentItem,然后等待用户再次单击以调用SetItem。我认为您实际上不希望在OnGUI函数中使用for循环。正确,我希望将其放置在用户单击的每个按钮生成指定游戏对象的位置,并允许他们将其放置在地形上任何想要的位置。如何使用此代码?
public class itemplacement : MonoBehaviour {
private Transform currentItem;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (currentItem = null) {
Vector3 m = Input.mousePosition;
m = new Vector3(m.x,m.y,transform.position.y);
Vector3 p = camera.ScreenToWorldPoint(m);
currentItem.position = new Vector3(p.x,0,p.z);
}
}
public void SetItem(GameObject b){
currentItem = ((GameObject)Instantiate(b)).transform;
}