Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Reflection;
- using UnityEditor;
- using UnityEngine;
- class TestEditor : EditorWindow
- {
- [MenuItem("Test/" + _Title)]
- static void ShowWindow() { GetWindow(typeof(TestEditor), false, _Title); }
- const string _Title = "TextEditor";
- string _txt = "Some text long 1 22 333 4444 55555 666666 7777777 END";
- bool _focusOnTextRequested;
- void OnGUI()
- {
- GUI.SetNextControlName("testText");
- EditorGUILayout.TextField(_txt, EditorStyles.textField);
- float textFieldW = GUILayoutUtility.GetLastRect().width;
- if (_focusOnTextRequested) {
- switch (Event.current.type) {
- case EventType.Layout:
- EditorGUI.FocusTextInControl("testText");
- break;
- case EventType.Repaint:
- _focusOnTextRequested = false;
- TextEditor tEditor = GetCurrTextEditor();
- if (tEditor == null) Debug.LogWarning("Couldn't get TextEditor " + Event.current.type);
- else {
- float txtW = EditorStyles.textField.CalcSize(new GUIContent(_txt)).x;
- tEditor.selectIndex = _txt.Length - 3;
- tEditor.cursorIndex = _txt.Length;
- if (textFieldW < txtW) {
- tEditor.scrollOffset = new Vector2(txtW - textFieldW, 0);
- }
- Repaint();
- }
- break;
- }
- }
- if (GUILayout.Button("Select end")) {
- // EditorGUI.FocusTextInControl("testText"); // THID DOESN'T WORK
- _focusOnTextRequested = true;
- }
- }
- TextEditor GetCurrTextEditor()
- {
- return typeof(EditorGUI)
- .GetField("activeEditor", BindingFlags.Static | BindingFlags.NonPublic)
- .GetValue(null) as TextEditor;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement