Объявление
public static bool DisplayDialog(string title, string message, string ok, string cancel = "");Параметры
title | Заголовок окна сообщения. |
message | Текст сообщения. |
ok | Метка, отображаемая на кнопке диалога OK. |
cancel | Метка, отображаемая на кнопке диалогового окна «Отмена». |
Возвращает
bool Возвращает true
, если пользователь нажимает кнопку OK. В противном случае возвращает false
.
Описание
Этот метод отображает модальное диалоговое окно.
Используйте его для отображения окон сообщений в редакторе.
ok
и cancel
— это метки, которые будут отображаться на кнопках диалогового окна. Если cancel
пуст (по умолчанию), отображается только одна кнопка. DisplayDialog возвращает true
, если нажата кнопка ok
.
Для диалоговых окон, которые могут отображаться повторно, рассмотрите возможность использования перегрузки этого метода, которая принимает DialogOptOutDecisionType, как описано в приведенном ниже примере кода.
Смотрите так же: DisplayDialogComplex function.
// Размещает выбранные объекты на поверхности местности.
using UnityEngine;
using UnityEditor;
public class PlaceSelectionOnSurface : ScriptableObject
{
[MenuItem("Example/Place Selection On Surface")]
static void CreateWizard()
{
Transform[] transforms = Selection.GetTransforms(SelectionMode.Deep |
SelectionMode.ExcludePrefab | SelectionMode.Editable);
if (transforms.Length > 0 &&
EditorUtility.DisplayDialog("Place Selection On Surface?",
"Are you sure you want to place " + transforms.Length
+ " on the surface?", "Place", "Do Not Place"))
{
Undo.RecordObjects(transforms, "Place Selection On Surface");
foreach (Transform transform in transforms)
{
RaycastHit hit;
if (Physics.Raycast(transform.position, -Vector3.up, out hit))
{
transform.position = hit.point;
Vector3 randomized = Random.onUnitSphere;
randomized = new Vector3(randomized.x, 0F, randomized.z);
transform.rotation = Quaternion.LookRotation(randomized, hit.normal);
}
}
}
}
}
Объявление
public static bool DisplayDialog(string title, string message, string ok, DialogOptOutDecisionType dialogOptOutDecisionType, string dialogOptOutDecisionStorageKey);Объявление
public static bool DisplayDialog(string title, string message, string ok, string cancel = "", DialogOptOutDecisionType dialogOptOutDecisionType, string dialogOptOutDecisionStorageKey);Параметры
title | The title of the message box. |
message | The text of the message. |
ok | Label displayed on the OK dialog button. |
cancel | Label displayed on the Cancel dialog button. |
dialogOptOutDecisionType | The type of opt-out decision a user can make. |
dialogOptOutDecisionStorageKey | The unique key setting to store the decision under. |
Возвращает
booltrue
if the user clicks the ok
button, or previously opted out. Returns false
if the user cancels or closes the dialog without making a decision.
Описание
This method displays a modal dialog that lets the user opt-out of being shown the current dialog box again.
Use this method to display dialog boxes in the Editor that might be shown repeatedly. Choose which DialogOptOutDecisionType to use based on how often you think users encounter this message and how often you want to remind them of it.
If the user opts-out of seeing the dialog box associated with the provided dialogOptOutDecisionStorageKey
, Unity doesn't show the dialog box and this method immediately returns true
.
ok
and cancel
are labels displayed on the dialog buttons. If cancel
is empty, the button displays as "Cancel". This is the default setting. DisplayDialog returns true
if the user presses the ok
button.
If the user opts-out of the dialog box, Unity stores this decision. If dialogOptOutDecisionType
is set to DialogOptOutDecisionType.ForThisMachine Unity stores the decision via EditorPrefs.SetBool. If dialogOptOutDecisionType
is set to DialogOptOutDecisionType.ForThisSession Unity stores the decision via SessionState.SetBool. In both cases Unity stores the decision under the key provided as dialogOptOutDecisionStorageKey
.
If you want to the let the user change the decision that is stored in EditorPrefs, you can add this to the Editor Preferences with a SettingsProvider.
Use DialogOptOutDecisionType.ForThisSession to show a dialog before a user performs a destructive action that might lose some of their work. If you think the user might see this dialog too often, you can add an option to the Editor Preferences with a SettingsProvider by using EditorPrefs and query that setting before showing the dialog.
Смотрите так же: DisplayDialogComplex function.
// Places the selected Objects on the surface of a terrain.
using UnityEngine;
using UnityEditor;
using UnityEngine.UIElements;
public class PlaceSelectionOnSurface : ScriptableObject
{
const string placeOnSurfaceDialogDecisionKey = "Example.PlaceOnSurfaceDecision";
[MenuItem("Example/Place Selection On Surface")]
static void CreateWizard()
{
Transform[] transforms = Selection.GetTransforms(SelectionMode.Deep |
SelectionMode.ExcludePrefab | SelectionMode.Editable);
if (transforms.Length > 0 &&
EditorUtility.DisplayDialog("Place Selection On Surface?",
"Are you sure you want to place " + transforms.Length
+ " on the surface?", "Place", "Do Not Place", DialogOptOutDecisionType.ForThisMachine, placeOnSurfaceDialogDecisionKey))
{
// Register and Undo event so that this action is not only not desctrutive but also easy to revert.
// Without Undo, DialogOptOutDecisionType.ForThisSession would be a better fiting decision type.
Undo.RecordObjects(transforms, "Place Selection On Surface");
foreach (Transform transform in transforms)
{
RaycastHit hit;
if (Physics.Raycast(transform.position, -Vector3.up, out hit))
{
transform.position = hit.point;
Vector3 randomized = Random.onUnitSphere;
randomized = new Vector3(randomized.x, 0F, randomized.z);
transform.rotation = Quaternion.LookRotation(randomized, hit.normal);
}
}
}
}
}