public void OnEnable() { // Creation and assembly of the window. var root = rootVisualElement; // Construct toolbar. // Currently not handled by uxml (2019.1.1f1). var toolbar = new Toolbar(); root.Add(toolbar); var tbButtonLoad = new ToolbarButton { text = "Load" }; toolbar.Add(tbButtonLoad); var tbButtonSave = new ToolbarButton { text = "Save" }; toolbar.Add(tbButtonSave); var tbButtonSaveMenu = new ToolbarButton { text = "Save & Create Shortcut" }; toolbar.Add(tbButtonSaveMenu); var tbSpacer = new ToolbarSpacer(); toolbar.Add(tbSpacer); var tbButtonRun = new ToolbarButton { text = "Execute" }; toolbar.Add(tbButtonRun); var tbSpacer2 = new ToolbarSpacer(); toolbar.Add(tbSpacer2); var tbButtonClearCode = new ToolbarButton { text = "Clear Code" }; toolbar.Add(tbButtonClearCode); var tbButtonClearOutput = new ToolbarButton { text = "Clear Output" }; toolbar.Add(tbButtonClearOutput); var tbButtonClearAll = new ToolbarButton { text = "Clear All" }; toolbar.Add(tbButtonClearAll); // Assemble and construct visual tree. var visualTree = AssetDatabase.LoadAssetAtPath <VisualTreeAsset>("Packages/com.unity.scripting.python/Styles/pythonconsole_uxml.uxml"); visualTree.CloneTree(root); root.styleSheets.Add(AssetDatabase.LoadAssetAtPath <StyleSheet>("Packages/com.unity.scripting.python/Styles/pythonconsole_uss.uss")); // Fetch references to UXML objects (UQuery). m_textFieldCode = root.Query <TextField>("textAreaCode").First(); m_textFieldOutput = root.Query <TextField>("textAreaOutput").First(); m_holderofOutputTextField = root.Query <ScrollView>("holderOutput").First(); m_scrollerOutput = root.Query <Scroller>(className: "unity-scroller--vertical").First(); m_scrollerCode = root.Query <Scroller>(className: "unity-scroller--vertical").Last(); // Set up the Undo handling // Use the event to add to the undo stack and collapse the stack to // give a feeling closer to a real text editor undo m_textFieldCode.RegisterCallback <ChangeEvent <string> >(OnCodeInput); // Implement event handlers. m_textFieldCode.Q(TextField.textInputUssName).RegisterCallback <KeyDownEvent>(OnExecute); tbButtonLoad.RegisterCallback <MouseUpEvent>(OnLoad); tbButtonSave.RegisterCallback <MouseUpEvent>(OnSave); tbButtonSaveMenu.RegisterCallback <MouseUpEvent>(OnSaveShortcut); tbButtonRun.RegisterCallback <MouseUpEvent>(OnExecute); tbButtonClearCode.RegisterCallback <MouseUpEvent>(OnClearCode); tbButtonClearOutput.RegisterCallback <MouseUpEvent>(OnClearOutput); tbButtonClearAll.RegisterCallback <MouseUpEvent>(OnClearAll); // Handle reserialization. m_textFieldCode.value = m_code; m_textFieldOutput.value = m_outputContents; // Also assign to the static variable here. On some occasions, // like domain reloads, the value is lost and if the window is // already showing, ShowWindow() won't be called, and code output // will never go to the console output. s_window = this; }