Legacy Documentation: Version 5.6 (Go to current version)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

EditorGUIUtility.CommandEvent

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public static function CommandEvent(commandName: string): Event;
public static Event CommandEvent(string commandName);

Parameters

commandName The command to be sent.

Description

Creates an event that can be sent to another window.

#pragma strict

class CommandEventExample extends EditorWindow {

var eventCount : int = 0;

@MenuItem("Examples/CommandEvent example") static function Init() { var window = GetWindowWithRect.<CommandEventExample>(Rect(0, 0, 150, 40)); window.Show(); }

function OnGUI() { if (GUILayout.Button("Paste")) { var e : Event = EditorGUIUtility.CommandEvent("Paste"); var ew : EditorWindow = EditorWindow.GetWindow.<CommandEventTest>(true); ew.SendEvent(e); } }

}
// Sends a paste event to an EditorWindow, as if Paste
// was selected from the Edit menu.

using UnityEngine; using UnityEditor;

public class CommandEventExample : EditorWindow { [MenuItem("Examples/CommandEvent example")] static void commandEventExample() { CommandEventExample window = EditorWindow.GetWindowWithRect<CommandEventExample>(new Rect(0, 0, 150, 40)); window.Show(); }

void OnGUI() { if (GUILayout.Button("Paste")) { Event e = EditorGUIUtility.CommandEvent("Paste"); EditorWindow ew = EditorWindow.GetWindow<Test>(true); ew.SendEvent(e); } } }

The script below receives the Paste message sent from CommandEventExample above.

#pragma strict

class CommandEventTest extends EditorWindow {

var eventCount : int = 0;

@MenuItem("Examples/Example that receives a paste event") static function Init() { var window = GetWindow(CommandEventTest); window.Show(); }

function OnGUI() { EditorGUI.LabelField(new Rect(10, 10, 90, 30), "Paste: " + eventCount.ToString());

if (Event.current.type == EventType.ExecuteCommand) { var e : Event = Event.current; if (e.commandName == "Paste") { eventCount = eventCount + 1; } } } }
using UnityEngine;
using UnityEditor;

public class CommandEventTest : EditorWindow { private int eventCount = 0;

[MenuItem("Examples/Example that receives a paste event")] static void test() { CommandEventTest window = EditorWindow.GetWindowWithRect<CommandEventTest>(new Rect(0, 0, 100, 40)); window.Show(); }

void OnGUI() { EditorGUI.LabelField(new Rect(10, 10, 90, 30), "Paste: " + eventCount.ToString());

if (Event.current.type == EventType.ExecuteCommand) { Event e = Event.current; if (e.commandName == "Paste") { eventCount = eventCount + 1; } } } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961