Author Topic: bug in UILabel?  (Read 3965 times)

niall

  • Newbie
  • *
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 13
    • View Profile
bug in UILabel?
« on: April 04, 2014, 07:41:10 AM »
I was trying to make individual words in a label tappable, for the purposes of highlighting them as they are tapped, and playing the audio for that word.  Colour me amazed to find NGUI already had a function for this!  However, it seems to throw exceptions when you tap on spaces in the label.  I was able to get around it with a dirty try/catch, but perhaps it needs fixing, or I am just missing something?  I'll paste my function for reference, if you're interested! (heavily commented for some new-to-Unity devs joining me soon.)  I'm not sure I can post any code from UILabel.cs where I believe the problem is, so I'll leave that out.  :)


  1.         public void NarrationTapped()
  2.         {
  3.                 HighlightWord(-1);//passing -1 for the word index should clear any highlighting on the string.
  4.                 Vector2 screenPos = Input.mousePosition; //grab screen coords where you tapped
  5.                 Camera cam = NGUITools.FindCameraForLayer (gameObject.layer);
  6.                 Vector3 worldPos = cam.ScreenToWorldPoint (screenPos);//fancy unity convert screen coords to world coords
  7.                 int charIndex;
  8.                 try{ //have to wrap this whole thing in a try/catch because NGUI throws exceptions when tapping on space characters...
  9.                         charIndex = narrationTextLbl.GetCharacterIndexAtPosition(worldPos);//get the index into the string for the character you tapped.
  10.                         string oldText = narrationTextLbl.text;//copy the page text locally so we can mess with it.
  11.                         if(charIndex != -1 && charIndex < oldText.Length)
  12.                         {
  13.                                 if(narrationTextLbl.GetWordAtCharacterIndex(charIndex) != " ")
  14.                                 {
  15.                                         string hexBlue = @"[0000ff]";
  16.                                         string endColorTag = "[-]";
  17.                                         int linkStart = oldText.LastIndexOf(' ', charIndex) + 1; //find the char index for the first character of the selected word
  18.                                         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.
  19.                                         int linkEnd = oldText.IndexOf(' ', charIndex);//gets the char index for the end of the selected word.
  20.                                         if (linkEnd == -1) linkEnd = oldText.Length;
  21.                                        
  22.                                         if (linkStart != linkEnd)
  23.                                         {
  24.                                                 oldText = oldText.Insert(linkStart, hexBlue);//insert the blue BBcode before the word.
  25.                                                 oldText = oldText.Insert((linkEnd+hexBlue.Length), "[-]");//and close the BBcode tag
  26.                                         }
  27.                                         narrationTextLbl.text = oldText;//then set the text back to the label
  28.                                         string word = narrationTextLbl.GetWordAtCharacterIndex(charIndex).ToUpper();//get the word now, and uppercase it for the filename.
  29.                                         string fileName = System.Text.RegularExpressions.Regex.Replace(word, "[?!,.;:]", "");//remove punctuations (add to that array if we missed a punctuation type!)
  30.                                         AudioClip snd = (AudioClip)Resources.Load("Words/" + fileName, typeof(AudioClip));//load the sound clip for the word
  31.                                         narrationAudio.clip = snd;//set it
  32.                                         narrationAudio.Play();//play it!
  33.                                 }
  34.                         }
  35.                 }
  36.                 catch{
  37.                         Debug.Log ("you probably clicked on a space");
  38.                 }
  39.         }


edit: here is the exception thrown as well:

ArgumentOutOfRangeException: Cannot be negative.
Parameter name: length
System.String.Substring (Int32 startIndex, Int32 length) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System/String.cs:348)
UILabel.GetWordAtCharacterIndex (Int32 characterIndex) (at Assets/NGUI/Scripts/UI/UILabel.cs:1286)
NarrationLoader.NarrationTapped () (at Assets/Scripts/NarrationLoader.cs:115)
« Last Edit: April 04, 2014, 07:57:38 AM by niall »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: bug in UILabel?
« Reply #1 on: April 04, 2014, 01:29:16 PM »
Try changing UILabel.GetWordAtCharacterIndex to:
  1.         public string GetWordAtCharacterIndex (int characterIndex)
  2.         {
  3.                 if (characterIndex != -1 && characterIndex < mText.Length)
  4.                 {
  5.                         int linkStart = mText.LastIndexOf(' ', characterIndex) + 1;
  6.                         int linkEnd = mText.IndexOf(' ', characterIndex);
  7.                         if (linkEnd == -1) linkEnd = mText.Length;
  8.  
  9.                         if (linkStart != linkEnd)
  10.                         {
  11.                                 int len = linkEnd - linkStart;
  12.  
  13.                                 if (len > 0)
  14.                                 {
  15.                                         string word = mText.Substring(linkStart, len);
  16.                                         return NGUIText.StripSymbols(word);
  17.                                 }
  18.                         }
  19.                 }
  20.                 return null;
  21.         }

niall

  • Newbie
  • *
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 13
    • View Profile
Re: bug in UILabel?
« Reply #2 on: April 08, 2014, 08:33:23 AM »
Yep, that works of course!  Will you be including it in the next update?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: bug in UILabel?
« Reply #3 on: April 09, 2014, 02:49:00 AM »
Judging by the posting date, it should be already a part of 3.5.6.

niall

  • Newbie
  • *
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 13
    • View Profile
Re: bug in UILabel?
« Reply #4 on: April 09, 2014, 02:56:08 PM »
Hah, look at that!  I don't always check for my plugin updates, but when I do, they overwrite changes I made to them!  /DosEquis