Author Topic: Update UI Data on widget class  (Read 2991 times)

lzt120

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 68
    • View Profile
Update UI Data on widget class
« on: June 14, 2014, 10:33:15 AM »
I wonder if there are one method in widget class to update text property in UILabel and update the spriteName property in UISprite, would be easier for updating the data for UI. As in game development, label's Text property and sprite's spriteName is the most often  property need update once the data changed.Would you like to add this in next update ?

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Update UI Data on widget class
« Reply #1 on: June 14, 2014, 07:24:32 PM »
Right now it's left more "raw", in that .text and .spriteName is exposed for you to change. The thing is, you have to handle hooking up the widgets to data anyway, so you might as well handle the update right there.

What did you exactly have in mind for a single updater? Just a wrapper method? or something that hooks up directly to the data and interprets what it needs to do based on that? Generally, it seems to me that this is much more game specific, which means it's outside the scope of a widget class.


lzt120

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 68
    • View Profile
Re: Update UI Data on widget class
« Reply #2 on: June 14, 2014, 10:39:27 PM »
Please see example as below:

public class WidgetData : MonoBehaviour
{
   // Udpate the text of lable and sprite name of sprite
   public virtual void UpdateText(string text){}
}

public abstract class UIRect : WidgetData

In UISprite:

public override void UpdateText(string text)
   {
      spriteName = text;
   }

In UILabel:
public override void UpdateText(string text)
   {
      Debug.Log("text");
      this.text = text;
   }

In Test:
public class Test : MonoBehaviour {

   public string[] myData;

   public WidgetData[] myWidget;

   void Start()
   {
      Debug.Log("Start");
      for(int i = 0;i < myWidget.Length;i++)
      {
         myWidget.UpdateText(myData);
      }
   }
}

By using this method, I can let my artist to drag and drop the widget in inspector and my UI logic does not bother with NGUI or other UI System.I can separate UI and Logic for easy update the UI looks if need and the logic remain the same as it was.

lzt120

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 68
    • View Profile
Re: Update UI Data on widget class
« Reply #3 on: June 14, 2014, 10:48:56 PM »
But one problem is, if this method does not support by official, I need modify the files every time for new version of NGUI.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Update UI Data on widget class
« Reply #4 on: June 15, 2014, 06:05:36 AM »
Sprites and labels are very different, and having a common function to set both would be weird, to say the least.

Sprites are drawn using a combination of an atlas, and a sprite name referencing a specific part of that atlas.

Labels are drawn using a combination of a font and a text representing which characters to draw from that font.

They're very different things.

Furthermore, expanding the outlook there is also the 2D sprite class which references 2D sprites directly, without any name... and a UITexture class that references textures directly -- again without any names. Same thing with UIPanel, for that matter. Even the base UIWidget (invisible widget) doesn't have any text to update. All 4 derive from UIRect. None of them have any point in "UpdateText" as they doesn't have any text to update.

See my point?

lzt120

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 68
    • View Profile
Re: Update UI Data on widget class
« Reply #5 on: June 15, 2014, 08:03:03 AM »
1. I see your point. your point stands on drawing but mine stands on change it on run-time.
If I need change the sprite (not 2D sprite) on run-time, I can change it by spriteName property,right ? If I need change the label's text,I can modify its text property directly.right? But how can I change both of them in the same function?I need declare one label variable and one sprite variable  to change the text property for label and sprite name for sprite. But if there one function in widget class and then overwrite it in label and sprite, I just need declare in my code two widgets and I call the widget's function and then lable will change its text property and sprite will change its sprite name. I do not even need to know if it is one label or it is one sprite. both of them just the widget only in programmer's view.

1.If using 2D sprite, how can I can this sprite on run-time ? I need use one the Unity 2D sprite to replace it ? It does's use Atlas ?
 

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Update UI Data on widget class
« Reply #6 on: June 15, 2014, 10:24:00 AM »
This would do the same thing for your Test class without any need to change anything in NGUI.

  1. public class Test : MonoBehaviour {
  2.  
  3.    public string[] myData;
  4.    public UIWidget[] myWidgets;
  5.  
  6.    void Start()
  7.    {
  8.       Debug.Log("Start");
  9.       for(int i = 0;i < myWidgets.Length;i++)
  10.       {
  11.                 var myWidget = myWidgets[i];
  12.                 if (myWidget.GetType() == typeof(UISprite))
  13.                 {
  14.                         ((UISprite)myWidget).spriteName = myData[i];
  15.                 }
  16.                 else if (myWidget.GetType() == typeof(UILabel))
  17.                 {
  18.                         ((UILabel)myWidget).text = myData[i];
  19.                 }        
  20.       }
  21.    }
  22. }

lzt120

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 68
    • View Profile
Re: Update UI Data on widget class
« Reply #7 on: June 15, 2014, 08:48:03 PM »
Thanks. But still, I think that if can offer some common function in widget class and then overwrite it in sub class would be easier change it on run-time.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Update UI Data on widget class
« Reply #8 on: June 16, 2014, 08:16:51 AM »
Sorry, but this change is something I won't make.

Base class should never contain functionality that can be left unused by derived classes. Doing so is horrible programming architecture.