- Home /
Word or string position under mouse position
This is a long shot, but I have a scrollview with a single label in it. The label shows a Rich Text string. Is there any way to capture the position in the string (or the word) that the mouse is over?
Many thanks
Comment
Answer by ScottYann · Feb 24, 2014 at 01:49 AM
//if you are using ngui it is pretty easy actually:
using UnityEngine; using System.Collections;
public class textTest : MonoBehaviour {
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
void OnClick ()
{
Vector2 screenPos = Input.mousePosition;
Camera cam = NGUITools.FindCameraForLayer (gameObject.layer);
Vector3 worldPos = cam.ScreenToWorldPoint (screenPos);
int testCharIndex = GetComponent<UILabel> ().GetCharacterIndexAtPosition (worldPos);
string theWord = GetComponent<UILabel> ().GetWordAtPosition (worldPos);
print (testCharIndex);
print (theWord);
}
}