单击GUI按钮时禁用GameObject创建

时间:2016-06-16 19:03:15

标签: c# unity3d

我有一大堆类似的代码:

GameObject prefab; // Gets set when GUI button is clicked

void Update(){
    if(Input.GetMouseButtonUp(0) && prefab){
        Instantiate(prefab, /* At mouse position on ground */);
        prefab = null;
    }
}

单击按钮时的代码如下:

public void SetBuilding(GameObject building){
    prefab = building;
    Destroy(ghostBuildingObject);
    ghostBuildingObject = Instantiate(building);

    if (ghostBuildingObject) {
        // Set the alpha to half
        Color c = ghostBuildingObject.GetComponent<MeshRenderer>().material.color;
        c.a = 0.5f;
        ghostBuildingObject.GetComponent<MeshRenderer>().material.color = c;

        // Disable Components
        ghostBuildingObject.GetComponent<Collider>().isTrigger = true;
        Destroy(ghostBuildingObject.GetComponent<NavMeshObstacle>());
        Destroy(ghostBuildingObject.GetComponent<Building>());
        ghostBuilding = ghostBuildingObject.AddComponent<GhostBuilding>();
        ghostBuildingObject.layer = LayerMask.NameToLayer("Ghost");
        ghostBuildingObject.name = "Ghost Building";
    }
}

它的作用基本上是创建一个对象,我点击地面对象。当我点击一个按钮时,它仍会创建游戏对象。当我点击GUI按钮时,如何阻止它创建游戏对象?

4 个答案:

答案 0 :(得分:1)

在您提出自己的答案之前,正在键入此内容。我仍然决定回答,因为你在答案中所做的是错误和不必要的。您在每个帧中分配内存多次次,在您的答案中的代码中仅检测到点击...

  

我正在使用Button(UnityEngine.GUI.Button)

     

我正在使用unity5 GUI元素

你不应该开始使用它。有些人建议你应该使用光线投射,但光线通过UI的问题仍将存在。

您需要的是来自Button而不是UnityEngine.UI.Button的{​​{1}}。您必须更改整个程序才能使用UnityEngine.GUI.Button中的UI

要创建按钮,请转到 GameObject - &gt; 用户界面 - &gt; 按钮。这应该创建Canvas和使Button工作所需的其他组件。这是Unity UI tutorials

  

它的作用基本上是创建一个对象,我点击它   地面物体。

您可以通过实施UnityEngine.UI并覆盖IPointerClickHandler功能来检测按钮点击。

将下面的脚本附加到您要检测点击的地面。

OnPointerClick

对于您的Button,您只需使用using UnityEngine; using System.Collections; using UnityEngine.UI; using UnityEngine.EventSystems; public class Click : MonoBehaviour, IPointerClickHandler { void Start() { Camera.main.gameObject.AddComponent<PhysicsRaycaster>(); } public void OnPointerClick(PointerEventData eventData) { Debug.Log("Clicked!"); } } 订阅按钮即可。以下是检测3个按钮点击的完整代码。这样做的好处是只能检测到一次点击。点击不会通过其他游戏对象,你每帧只分配内存只是为了检测按钮点击。

Button.onClick.AddListener(() => functionName());

答案 1 :(得分:0)

我不知道我是否清楚地了解你是否在按钮上实例化并点击了:

ghostBuildingObject = Instantiate(building);

Instantiate(prefab, /* At mouse position on ground */);

因此创建2次

是正常的

答案 2 :(得分:0)

您是否尝试过:

if(Input.GetMouseButtonUp(0) && prefab != null){
    Instantiate(prefab, /* At mouse position on ground */);
    prefab = null;
}

答案 3 :(得分:-1)

使用指针事件似乎解决了这个问题:

void Update(){
    // Get pointer events
    var pointer = new PointerEventData(EventSystem.current);
    pointer.position = Input.mousePosition;

    // Test the raycast
    List<RaycastResult> raycastResults = new List<RaycastResult>();
    EventSystem.current.RaycastAll(pointer, raycastResults);

    // Check if anything was cast
    if(Input.GetMouseButtonUp(0) && raycastResults.Count == 0 && prefab){
        Instantiate(prefab, /* At mouse position on ground */);
        prefab = null;
    }
}
相关问题