CurvedUIInputFieldCaret.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. using UnityEngine.EventSystems;
  5. namespace CurvedUI
  6. {
  7. #if !UNITY_5_1 && !UNITY_5_2
  8. /// <summary>
  9. /// Creates a recttransform caret with image component that can be curved with curvedUI. Hides inputfield's original caret.
  10. /// </summary>
  11. public class CurvedUIInputFieldCaret : MonoBehaviour, ISelectHandler, IDeselectHandler
  12. {
  13. //references
  14. InputField myField;
  15. RectTransform myCaret;
  16. Color origCaretColor;
  17. Color origSelectionColor;
  18. //variables
  19. bool selected = false;
  20. bool selectingText = false;
  21. //settings
  22. int lastCharDist = 2;
  23. void Awake()
  24. {
  25. myField = this.GetComponent<InputField>();
  26. }
  27. void Update()
  28. {
  29. //only update the caret's position when the field is selected.
  30. if (selected)
  31. UpdateCaret();
  32. }
  33. /// <summary>
  34. /// On select, set the caret active and start blinker coroutine.
  35. /// </summary>
  36. /// <param name="eventData"></param>
  37. public void OnSelect(BaseEventData eventData)
  38. {
  39. if (myCaret == null)
  40. CreateCaret();
  41. selected = true;
  42. myCaret.gameObject.SetActive(true);
  43. StartCoroutine(CaretBlinker());
  44. }
  45. /// <summary>
  46. /// Hide the caret on deselect.
  47. /// </summary>
  48. /// <param name="eventData"></param>
  49. public void OnDeselect(BaseEventData eventData)
  50. {
  51. selected = false;
  52. myCaret.gameObject.SetActive(false);
  53. }
  54. /// <summary>
  55. /// Simple blinker. Blinks the caret if inputfield is selected and user is not selecting text.
  56. /// </summary>
  57. /// <returns></returns>
  58. IEnumerator CaretBlinker()
  59. {
  60. while (selected)
  61. {
  62. myCaret.gameObject.SetActive(selectingText ? true : !myCaret.gameObject.activeSelf);
  63. yield return new WaitForSeconds(0.5f / (float)myField.caretBlinkRate);
  64. }
  65. }
  66. void CreateCaret()
  67. {
  68. //lets create a curvedui caret and copy the settings from our input field
  69. GameObject go = new GameObject("CurvedUICaret");
  70. go.AddComponent<RectTransform>();
  71. go.AddComponent<Image>();
  72. go.AddComponent<CurvedUIVertexEffect>();
  73. go.transform.SetParent(this.transform);
  74. go.transform.localScale = Vector3.one;
  75. (go.transform as RectTransform).anchoredPosition3D = Vector3.zero;
  76. //(go.transform as RectTransform).pivot = new Vector2(0, 0.5f);
  77. (go.transform as RectTransform).pivot = new Vector2(0, 1.0f);
  78. go.GetComponent<Image>().color = myField.caretColor;
  79. myCaret = go.transform as RectTransform;
  80. go.transform.SetAsFirstSibling();
  81. //save original color and hide the original caret
  82. myField.customCaretColor = true;
  83. origCaretColor = myField.caretColor;
  84. myField.caretColor = new Color(0, 0, 0, 0);
  85. origSelectionColor = myField.selectionColor;
  86. myField.selectionColor = new Color(0, 0, 0, 0);
  87. go.gameObject.SetActive(false);
  88. }
  89. void UpdateCaret()
  90. {
  91. if (myCaret == null)
  92. CreateCaret();
  93. //Debug.Log("caret:" + myField.caretPosition + " / focus:"+ myField.selectionFocusPosition + " / anchor:" + myField.selectionAnchorPosition + " / charc:" + myField.textComponent.cachedTextGenerator.characterCount + " / charvis:" + myField.textComponent.cachedTextGenerator.characterCountVisible);
  94. Vector2 newCaretPos = GetLocalPositionInText(myField.caretPosition);
  95. //RectTransform originalCaret = (RectTransform)myField.transform.FindChild(myField.name + " Input Caret");
  96. if (myField.selectionFocusPosition != myField.selectionAnchorPosition) // user is selecting text is those two are not equal.
  97. {
  98. selectingText = true;
  99. Vector2 selectionSize = new Vector2(
  100. GetLocalPositionInText(myField.selectionAnchorPosition).x - GetLocalPositionInText(myField.selectionFocusPosition).x,
  101. GetLocalPositionInText(myField.selectionAnchorPosition).y - GetLocalPositionInText(myField.selectionFocusPosition).y
  102. );
  103. newCaretPos = selectionSize.x < 0 ? GetLocalPositionInText(myField.selectionAnchorPosition) : GetLocalPositionInText(myField.selectionFocusPosition);
  104. selectionSize = new Vector2(Mathf.Abs(selectionSize.x), Mathf.Abs(selectionSize.y) + myField.textComponent.fontSize);
  105. myCaret.sizeDelta = new Vector2(selectionSize.x, selectionSize.y);
  106. myCaret.anchoredPosition = newCaretPos;
  107. myCaret.GetComponent<Image>().color = origSelectionColor;
  108. }
  109. else { // user is not selecting text, just update the caret position.
  110. selectingText = false;
  111. //myCaret.sizeDelta = new Vector2(myField.caretWidth, originalCaret == null ? 10 : originalCaret.rect.height);
  112. myCaret.sizeDelta = new Vector2(myField.caretWidth, myField.textComponent.fontSize);
  113. myCaret.anchoredPosition = newCaretPos;
  114. myCaret.GetComponent<Image>().color = origCaretColor;
  115. }
  116. }
  117. /// <summary>
  118. /// Returns the position in inputfield's rectransform local space, based on character position in text. Pretty neat actually.
  119. /// </summary>
  120. /// <param name="charNo"></param>
  121. /// <returns></returns>
  122. Vector2 GetLocalPositionInText(int charNo)
  123. {
  124. if (myField.isFocused)
  125. {
  126. TextGenerator gen = myField.textComponent.cachedTextGenerator;
  127. if (charNo > gen.characterCount - 1) //do not go over the text length.
  128. charNo = gen.characterCount - 1;
  129. if (charNo > 0)
  130. {
  131. UICharInfo charInfo = gen.characters[charNo - 1];
  132. float x = (charInfo.cursorPos.x + charInfo.charWidth) / myField.textComponent.pixelsPerUnit + lastCharDist;
  133. float y = (charInfo.cursorPos.y) / myField.textComponent.pixelsPerUnit;
  134. return new Vector2(x, y);
  135. }
  136. else {
  137. UICharInfo charInfo = gen.characters[charNo];
  138. float x = (charInfo.cursorPos.x) / myField.textComponent.pixelsPerUnit;
  139. float y = (charInfo.cursorPos.y) / myField.textComponent.pixelsPerUnit;
  140. return new Vector2(x, y);
  141. }
  142. }
  143. else return Vector2.zero; // field not focused, return 0,0
  144. }
  145. #region SETTERS AND GETTERS
  146. public Color CaretColor {
  147. get { return origCaretColor; }
  148. set { origCaretColor = value; }
  149. }
  150. public Color SelectionColor {
  151. get { return origSelectionColor; }
  152. set { origSelectionColor = value; }
  153. }
  154. public float CaretBlinkRate {
  155. get { return myField.caretBlinkRate; }
  156. set { myField.caretBlinkRate = value; }
  157. }
  158. #endregion
  159. #else
  160. public class CurvedUIInputFieldCaret : MonoBehaviour
  161. {
  162. //Unity before 5.3 does not contain methods necessary to curve the input field caret without changing original script.
  163. #endif
  164. }
  165. }