using UnityEngine;
public class TextSelect : MonoBehaviour
{
public int curKeyboardControl;
private GUIContent myText1 = new GUIContent("Test Field");
void OnGUI()
{
int TextField1ID = GUIUtility.GetControlID(FocusType.Keyboard);
DoTextField(new Rect(20, 20, 200, 20), TextField1ID, myText1, true, 200, "TextField");
}
//функция для создания текстареа
public static void DoTextField(Rect position, int id, GUIContent content, bool multiline, int maxLength, GUIStyle style)
{
if ((maxLength >= 0) && (content.text.Length > maxLength))
{
content.text = content.text.Substring(0, maxLength);
}
TextEditor stateObject = (TextEditor) GUIUtility.GetStateObject(typeof(TextEditor), id);
stateObject.content.text = content.text;
stateObject.SaveBackup();
stateObject.position = position;
stateObject.style = style;
stateObject.multiline = multiline;
stateObject.controlID = id;
stateObject.ClampPos();
Event current = Event.current;
bool flag = false;
switch (current.type)
{
case EventType.mouseDown:
if (position.Contains(current.mousePosition))
{
GUIUtility.hotControl = id;
if (GUIUtility.keyboardControl != id)
{
GUIUtility.keyboardControl = id;
}
stateObject.MoveCursorToPosition(Event.current.mousePosition);
//когда 1 раз кликаем - выделяем весь текст
if (Event.current.clickCount == 1)
{
stateObject.SelectAll();
}
if ((Event.current.clickCount == 2) && GUI.skin.settings.doubleClickSelectsWord)
{
stateObject.SelectCurrentWord();
stateObject.DblClickSnap(TextEditor.DblClickSnapping.WORDS);
stateObject.MouseDragSelectsWholeWords(true);
}
if ((Event.current.clickCount == 3) && GUI.skin.settings.tripleClickSelectsLine)
{
stateObject.SelectCurrentParagraph();
stateObject.MouseDragSelectsWholeWords(true);
stateObject.DblClickSnap(TextEditor.DblClickSnapping.PARAGRAPHS);
}
current.Use();
}
goto Label_02CF;
case EventType.mouseUp:
if (GUIUtility.hotControl == id)
{
stateObject.MouseDragSelectsWholeWords(false);
GUIUtility.hotControl = 0;
current.Use();
}
goto Label_02CF;
case EventType.mouseDrag:
if (GUIUtility.hotControl != id)
{
goto Label_02CF;
}
if (!current.shift)
{
stateObject.SelectToPosition(Event.current.mousePosition);
break;
}
stateObject.MoveCursorToPosition(Event.current.mousePosition);
break;
case EventType.keyDown:
if (GUIUtility.keyboardControl == id)
{
if (stateObject.HandleKeyEvent(current))
{
current.Use();
flag = true;
content.text = stateObject.content.text;
}
else
{
if ((current.keyCode == KeyCode.Tab) || (current.character == '\t'))
{
return;
}
char character = current.character;
if (((character == '\n') && !multiline) && !current.alt)
{
return;
}
Font font = style.font;
if (font == null)
{
font = GUI.skin.font;
}
if (font.HasCharacter(character) || (character == '\n'))
{
stateObject.Insert(character);
flag = true;
}
else if (character == '\0')
{
current.Use();
}
}
goto Label_02CF;
}
return;
case EventType.repaint:
if (GUIUtility.keyboardControl == id)
{
stateObject.DrawCursor(content.text);
}
else
{
style.Draw(position, content, id, false);
}
goto Label_02CF;
default:
goto Label_02CF;
}
current.Use();
Label_02CF:
if (flag)
{
GUI.changed = true;
content.text = stateObject.content.text;
if ((maxLength >= 0) && (content.text.Length > maxLength))
{
content.text = content.text.Substring(0, maxLength);
}
current.Use();
}
}
}