Author Topic: Create a pop-up dialog box via script?  (Read 22631 times)

Excal

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 21
    • View Profile
Create a pop-up dialog box via script?
« on: May 04, 2014, 05:57:55 PM »
I've gone through most of the video tutorials and they are great for creating things within the editor, but I'm wondering how to create pop-up dialog boxes via script. I did a few google searches and couldn't find any script examples. If anyone has one, please share :)

LightSky

  • Jr. Member
  • **
  • Thank You
  • -Given: 3
  • -Receive: 2
  • Posts: 56
    • View Profile
Re: Create a pop-up dialog box via script?
« Reply #1 on: May 04, 2014, 06:08:16 PM »
You could just create a popup dialog prefab and instantiate it whenever you need it.
Or you could have a reference to a popup window and use that one for all of your popups and modify is when you want to call it.
It mostly depends on what you want to do with the popup and how you want to use it.

Excal

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 21
    • View Profile
Re: Create a pop-up dialog box via script?
« Reply #2 on: May 04, 2014, 06:14:06 PM »
I'm still new to NGUI, but that seems like a great idea. I think the issue is that my dialog boxes will be different sizes. I'm currently creating them using Unity's default GUI system that uses OnGUI and specifies actual pixel sizes and positions. I heard NGUI is resolution independent and that's why I want to switch to it. I've been having trouble figuring out how to dynamically create dialogs.

Would the solution be to create a dialog box prefab for each dialog I want, and then just instantiate it at the right time, or is there a better way?

LightSky

  • Jr. Member
  • **
  • Thank You
  • -Given: 3
  • -Receive: 2
  • Posts: 56
    • View Profile
Re: Create a pop-up dialog box via script?
« Reply #3 on: May 04, 2014, 06:18:15 PM »
You could easily create a dynamic dialog box by having a script attached that will set the text for it and then use UIStretch on the background to match the text/sprite size for the popup.
So you will set the text, the text width/dimensions will be defined by you in the UILabel of the prefab and then UIStretch will be called on the background so it matches nicely.

Excal

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 21
    • View Profile
Re: Create a pop-up dialog box via script?
« Reply #4 on: May 04, 2014, 06:36:35 PM »
Alright, bear with me here, I'm still wrapping my head around this. I have an image in photoshop of what I want my button to look like:

Wooden Atlas has a set number of sprites and I'm not quite sure how to get mine to be an option. Do you know how I might be able to set this as my button background? Should I ignore the button prefab and just drag this into the scene and attach a button script to it?

LightSky

  • Jr. Member
  • **
  • Thank You
  • -Given: 3
  • -Receive: 2
  • Posts: 56
    • View Profile
Re: Create a pop-up dialog box via script?
« Reply #5 on: May 04, 2014, 06:44:52 PM »
You should put the textures for your UI inside an Atlas. (Watch the NGUI video on this if you need more info)

Your Prefab should look something like this:

-DialogWindow    <--- Prefab GameObject with a script on it that has a reference to the sprite and label
  - Sprite   <--Button Background
  - Label    <--Text for the button

You would instantiate the DialogWindow and call the script attached to set the popup.   Also you should use anchoring to set the default positioning of your text on the sprite.

Excal

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 21
    • View Profile
Re: Create a pop-up dialog box via script?
« Reply #6 on: May 04, 2014, 07:02:45 PM »
Which NGUI video? The 3.0.7 video doesn't cover creating an atlas.

LightSky

  • Jr. Member
  • **
  • Thank You
  • -Given: 3
  • -Receive: 2
  • Posts: 56
    • View Profile
Re: Create a pop-up dialog box via script?
« Reply #7 on: May 04, 2014, 07:05:57 PM »
Go to NGUI in the toolbar.
NGUI -> Open -> Atlas Maker

Pretty much follow the instructions that it gives you to create a new Atlas. 

Excal

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 21
    • View Profile
Re: Create a pop-up dialog box via script?
« Reply #8 on: May 05, 2014, 12:01:32 AM »
Is using a texture background is just as fine as using a sprite background?

Also, is there any particular script I should attach to the gameObject that contains my NGUI dialog box?


LightSky

  • Jr. Member
  • **
  • Thank You
  • -Given: 3
  • -Receive: 2
  • Posts: 56
    • View Profile
Re: Create a pop-up dialog box via script?
« Reply #9 on: May 05, 2014, 12:46:19 AM »
Using a texture background would result in an extra drawcall, meanwhile using a sprite from your Atlas will save drawcalls.
You will need to make a custom script that has a reference to the Label and Sprite and then configure those two how you would want it. 
I would just recommend having everything that you would want to set inside of a constructor, very similarly to Message Boxes in VB/C#.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Create a pop-up dialog box via script?
« Reply #10 on: May 05, 2014, 11:14:28 PM »
Thanks, LightSky. But as a note, don't use UIStretch. The new layout system handles it better.

Also, one of NGUI's tutorials shows how you can easily auto-resize a widget based on the label's content. So you could easily have resizeable dialog boxes using the layout system.

Excal

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 21
    • View Profile
Re: Create a pop-up dialog box via script?
« Reply #11 on: May 05, 2014, 11:48:10 PM »
Which one? Every time someone mentions NGUI tutorial there's like 20 of them.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Create a pop-up dialog box via script?
« Reply #12 on: May 06, 2014, 12:01:50 AM »
The ones that come with NGUI. There are 7 of them I think? I re-did all tutorial scenes a couple of weeks ago.