public void NarrationTapped()
{
HighlightWord(-1);//passing -1 for the word index should clear any highlighting on the string.
Vector2 screenPos = Input.mousePosition; //grab screen coords where you tapped
Camera cam = NGUITools.FindCameraForLayer (gameObject.layer);
Vector3 worldPos = cam.ScreenToWorldPoint (screenPos);//fancy unity convert screen coords to world coords
int charIndex;
try{ //have to wrap this whole thing in a try/catch because NGUI throws exceptions when tapping on space characters...
charIndex = narrationTextLbl.GetCharacterIndexAtPosition(worldPos);//get the index into the string for the character you tapped.
string oldText = narrationTextLbl.text;//copy the page text locally so we can mess with it.
if(charIndex != -1 && charIndex < oldText.Length)
{
if(narrationTextLbl.GetWordAtCharacterIndex(charIndex) != " ")
{
string hexBlue = @"[0000ff]";
string endColorTag = "[-]";
int linkStart = oldText.LastIndexOf(' ', charIndex) + 1; //find the char index for the first character of the selected word
if(linkStart==0) linkStart+= hexBlue.Length;//if you hit the first word, we have to skip the initial bit of BBcode that makes the text black.
int linkEnd = oldText.IndexOf(' ', charIndex);//gets the char index for the end of the selected word.
if (linkEnd == -1) linkEnd = oldText.Length;
if (linkStart != linkEnd)
{
oldText = oldText.Insert(linkStart, hexBlue);//insert the blue BBcode before the word.
oldText = oldText.Insert((linkEnd+hexBlue.Length), "[-]");//and close the BBcode tag
}
narrationTextLbl.text = oldText;//then set the text back to the label
string word = narrationTextLbl.GetWordAtCharacterIndex(charIndex).ToUpper();//get the word now, and uppercase it for the filename.
string fileName = System.Text.RegularExpressions.Regex.Replace(word, "[?!,.;:]", "");//remove punctuations (add to that array if we missed a punctuation type!)
AudioClip snd
= (AudioClip
)Resources
.Load("Words/" + fileName,
typeof(AudioClip
));//load the sound clip for the word narrationAudio.clip = snd;//set it
narrationAudio.Play();//play it!
}
}
}
catch{
Debug.Log ("you probably clicked on a space");
}
}