【问题标题】:Cannot Spawn basic figures in Unity via in game menu无法通过游戏菜单在 Unity 中生成基本人物
【发布时间】:2015-04-21 14:50:38
【问题描述】:

我的老板给我的任务是创建一个包含原始对象和一个独特对象的基本场景。我创建了对象以及第一人称相机/角色等。唯一的错误是每次运行脚本并启动游戏时,每次单击菜单切换按钮时,所有需要的表单要放置,无论玩家在哪里看,都会无限生成。我想要的是让玩家切换菜单,点击选项,点击后,对象出现(单个对象),玩家可以将其放置在场景中的任何位置。

这是来自 unity 的 C# 代码。我认为是 spawn 的代码一直在循环,但我希望它停止循环。

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

}

另外一个负责放置的代码是:

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

}

【问题讨论】:

    标签: c# unity3d unityscript


    【解决方案1】:

    我实际上并不熟悉旧的 GUI 东西(显然 Unity 不建议再使用它了),所以我在这里查了一下:http://docs.unity3d.com/Manual/gui-Basics.html

    我对您发布的代码的理解是 GUI.Window() 绘制您的窗口,使用 menuWindowMethod() 绘制内容,但这只是 绘制窗口。它不会等待用户在其中做某事。因此,当您在此处编写代码时,将立即继续执行 SetItem 调用。

    由于每帧都会调用 OnGUI,并且 Toggle 控件始终返回其当前值,因此在您打开它的那一刻,您将运行该循环并为每一帧的项目中的每个项目调用一次 SetItem。

    我怀疑您真正想要做的是将这些按钮连接起来,以便在单击它们时在您的 itemplacement 组件上设置一个新的 currentItem,然后等到用户再次单击以调用 SetItem。我认为您实际上并不希望在 OnGUI 函数中使用该 for 循环。

    【讨论】:

    • 正确,我希望将它放在用户单击的每个按钮生成指定游戏对象的位置,并允许他们将其放置在地形上任何他们想要的位置。我该如何使用这段代码?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-16
    • 2019-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多