Author Topic: SpriteSelector not selecting sprites  (Read 2484 times)

Vesuvian

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 32
    • View Profile
SpriteSelector not selecting sprites
« on: June 10, 2014, 04:21:31 AM »
Hi all

I'm trying to bash together a SpriteData PropertyAttribute with a custom property drawer:



It's not a perfect copy of the atlas and sprite fields that NGUI draws in the inspector, but it's at a stage where it's working pretty well.

The only problem I have is that I can't select sprites. I'm using the following code to build the "Sprite" button:

  1.         if (GUI.Button(new Rect(position.x, position.y,
  2.                                 PREFIX_BUTTON_WIDTH, position.height), "Sprite"))
  3.         {
  4.                 NGUISettings.atlas = atlas.objectReferenceValue as UIAtlas;
  5.                 NGUISettings.selectedSprite = sp.stringValue;
  6.                 SpriteSelector.Show((string spriteName) => SelectSprite(serializedObject,
  7.                                                                                         prop.FindPropertyRelative("m_SpriteName"),
  8.                                                                                        sp.stringValue));
  9.         }
  10.  
  11.         /// <summary>
  12.         ///     Sprite selection callback function.
  13.         /// </summary>
  14.         private void SelectSprite(SerializedObject serializedObject, SerializedProperty serializedProperty,
  15.                                   string spriteName)
  16.         {
  17.                 Debug.Log(spriteName);
  18.  
  19.                 serializedObject.Update();
  20.                 serializedProperty.stringValue = spriteName;
  21.                 serializedObject.ApplyModifiedProperties();
  22.                 NGUISettings.selectedSprite = spriteName;
  23.         }

When I press the "Sprite" button I get the SpriteSelector window, but when I click on sprites I get a debugged sprite name of "":



Furthermore, I can see that the typical green rect is not drawn on the sprites in the editor window, indicating to me that I don't seem to be making any meaningful selection.

Opening sprite selector from UISprite:



Opening sprite selector from custom property drawer:



I'm attaching the property and the drawer in case anyone wants to take a look. It's pretty short but the drawer gets messy.

If anyone has any ideas on how I can get the SpriteSelector to work, I'd love to hear them.

Thanks,
Ves


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: SpriteSelector not selecting sprites
« Reply #1 on: June 11, 2014, 04:42:59 AM »
There is no such property called "m_SpriteName". Remove the underscore. It should be "mSpriteName".

Vesuvian

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 32
    • View Profile
Re: SpriteSelector not selecting sprites
« Reply #2 on: June 11, 2014, 07:01:48 AM »
Sorry Aren, I know my variable names are inconsistent with those in UISprite. The names in my PropertyDrawer represent those found in my PropertyAttribute.

Reading SpriteSelector it seems to be completely agnostic to any property paths, so I still don't think the naming is a problem here.

I just spent some time re-reading my code a bunch of times to make sure this is true, and I found this stupid mistake:

Old -
  1. SpriteSelector.Show((string spriteName) => SelectSprite(serializedObject, sp, sp.stringValue));

New -
  1. SpriteSelector.Show((string spriteName) => SelectSprite(serializedObject, sp, spriteName));

Sorry for wasting your time!