簡體   English   中英

如何創建一個對話框並在單擊3d中的對象時顯示它?

[英]How to create a dialogue box and display it when an object in clicked in unity 3d?

我對腳本和團結相對較新。 如何創建一個帶有“是”和“否”按鈕的對話框,並在單擊3d中的特定對象時顯示它?

將Dialogue Text UI對象和Button UI對象保留為空Rect / UI對象的子對象。

在此輸入圖像描述

在將要單擊的對象中使用如下所示的代碼:

void OnMouseDown()
{
 dialogueBoxUIObject.SetActive(true);
}

保持dialogueBoxUIObject公共GameObject ,這樣就可以在檢查分配給它。 保持ColliderRigidbody組件在沒有重力的情況下與要單擊的對象保持一致。

有一個很好的Unity教程,如何在這里實現一個模態框:

https://unity3d.com/learn/tutorials/modules/intermediate/live-training-archive/modal-window

它實現了您需要做的事情,並且還添加了可能在將來幫助您的其他組件。 您可以看到用於實現視頻下方對話框的所有代碼。

您可以使用Canvas Group在其中分組所需的對話框以及Yes / No按鈕並將其Alpha設置為0並將Interactable設置為false,因此它最初不會顯示。

然后附上一個腳本來要單擊並使用對象OnMouseUp和里面改變CanvasGroup阿爾法1和可交互為True。

如果您正在尋找腳本方法,請使用OnMouseDown!

boolean isOpen = false;

void OnGUI() //I think this must be used on the camera so you may have to reference a gui controller on the camera
{
  if(isOpen) //Is it Open?
  {
    if(GUI.Button(new Rect(10, 10, 100, 50), "Yes")) //Display and use the Yes button
    {
      Debug.Log("Yes");
      isOpen = false;
    }
    if(GUI.Button(new Rect(110, 10, 100, 50), "No")) //Display and use the No button
    {
      Debug.Log("No");
      isOpen = false;
    }
  }
}

void OnMouseDown() //Get the mouse click
{
  isOpen = true;   //Set the buttons to appear
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM