如何在unity 3d中单击对象时创建对话框并显示它?
How to create a dialogue box and display it when an object in clicked in unity 3d?
我对脚本和 Unity 比较陌生。我如何创建一个带有 "Yes" 和 "No" 按钮的对话框并在单击 unity 3d 中的特定对象时显示它?
将对话文本 UI 对象和按钮 UI 对象保留为空 Rect/UI 对象的子对象。
在将被点击的对象中使用如下代码:
void OnMouseDown()
{
dialogueBoxUIObject.SetActive(true);
}
将 dialogueBoxUIObject
保留为 public GameObject
以便您可以在检查器中分配它。将 Collider
和 Rigidbody
组件与要单击的对象保持无重力。
这里有一个关于如何实现模态框的很好的 Unity 教程:
https://unity3d.com/learn/tutorials/modules/intermediate/live-training-archive/modal-window
它实现了您需要做的事情,还添加了将来可能对您有帮助的其他组件。您可以在视频下方看到用于实现对话框的所有代码。
您可以使用 Canvas Group 将您想要的对话框和两个 Yes/No 按钮组合在其中,并将其 Alpha 设置为 0 并将 Interactable 设置为 false,因此它最初不会显示。
然后将脚本附加到您要单击的对象并使用 OnMouseUp 并在其中将 CanvasGroup Alpha 更改为 1 并将 Interactable 更改为 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
}
我对脚本和 Unity 比较陌生。我如何创建一个带有 "Yes" 和 "No" 按钮的对话框并在单击 unity 3d 中的特定对象时显示它?
将对话文本 UI 对象和按钮 UI 对象保留为空 Rect/UI 对象的子对象。
在将被点击的对象中使用如下代码:
void OnMouseDown()
{
dialogueBoxUIObject.SetActive(true);
}
将 dialogueBoxUIObject
保留为 public GameObject
以便您可以在检查器中分配它。将 Collider
和 Rigidbody
组件与要单击的对象保持无重力。
这里有一个关于如何实现模态框的很好的 Unity 教程:
https://unity3d.com/learn/tutorials/modules/intermediate/live-training-archive/modal-window
它实现了您需要做的事情,还添加了将来可能对您有帮助的其他组件。您可以在视频下方看到用于实现对话框的所有代码。
您可以使用 Canvas Group 将您想要的对话框和两个 Yes/No 按钮组合在其中,并将其 Alpha 设置为 0 并将 Interactable 设置为 false,因此它最初不会显示。
然后将脚本附加到您要单击的对象并使用 OnMouseUp 并在其中将 CanvasGroup Alpha 更改为 1 并将 Interactable 更改为 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
}