Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: SoullessDreamer on July 22, 2014, 01:24:23 PM

Title: A few questions
Post by: SoullessDreamer on July 22, 2014, 01:24:23 PM
Hello everyone,
A little about my company and myself before I begin:

The name of our company is FireCreation. We are a small group of 18 people. We are obviously using Unity to build our first game for retail use.

I am 32 years old. I am the lead code designer as well as our company's CEO. I have years of experience with C++, LUA, HTML1, and HEX. I started modifying an old game called Marathon, Marathon 2, and Marathon Infinity(in HEX). Followed by Unreal(the first Unreal) and Unreal Tournament(A forum of C++). Then I moved on to Arma, Arma 2, Arma 2 Operation Arrowhead, DayZ the mod, and the mod Domination 2(Arma is C++ oriented). I then turned my sights onto modding Rust(LUA via Oxide). Now I am working with C# in Unity. Even though I might know a great deal of C# I still do not know everything.

I have a few questions, some might have already been asked. Some I might find by the time someone answers them. I want to say sorry in advance for any duplicate questions that someone has already asked. I also want to say sorry for questions that might be obvious to a seasoned coder in C#. I am still learning some of it's advance capabilities. I am also watching all the tutorial videos that these fine people here have made for us. Sadly not only do I have to take care of my company, but I have a wife and an 11th month old that demands quite a bit of my time. So I watch those amazing videos when when I have time.

Now for my questions:

I am using a State Machine for our game. I also created a preliminary UI system before I bought NGUI (I am attaching it to this thread, anyone can use it if they need although with NGUI it has become moot). What I am trying to do is get a UI to come up when the user looks at the item in question. I had it working with my little UI script. I have RayCast always on(another script) and I also got it to read when nothing is being 'hit'. So what I am asking is how would I get NGUI to have text come up on the user's screen when RayCast hits a specific item?

I am also using a State Machine to have persistent terrain, items, etc. So how would I add the main menu to what I already have? As of right now I have every item(gameobjects, scripts, the State Machine itself, etc) attach itself to an empty gameobject. I tried to attach the 'Control - Background' to this gameobject but I get an error. The UI Root does not child itself to the gameobject but the 'Control - Background' does. I know I am going to need the UI Root for the entire game. How would I go about Getting the UI Root to be persistent through the loading of each scene with my State Machine(child it to the empty gameobject like the Control - Background)? I have thought of making the UI Root replace the empty gameobject and attach everything to the UI Root. Yet before I rip that apart I was hoping to get a few ideas, if any.

There will be more questions to come as I watch more of the tutorials. I will use this same thread to ask them when they arise. I want to thank everyone in advance for even taking the time to read this. I also want to thank the Developers of NGUI. You guys are awesome! Your application will cut my development time in half. The one thing I have noticed with new indie game developers now a days, is they might be pushing out games with amazing gameplay but they forget to have something as basic as a comprehensive UI. To me the UI is just as important as gameplay. It is the first thing you see when the user loads into the game.

So again thank you for your hard work, you have gained an avid supporter!
Title: Re: A few questions
Post by: ArenMook on July 22, 2014, 09:31:09 PM
NGUI performs its own raycasts underneath mouse and touch events, provided the camera that sees the object you're interacting with has a UICamera script attached. NGUI won't perform raycasts into the screen that's not under the mouse or touch event. You can create a custom input method for NGUI that would do that, however (set UICamera.onCustomInput).

How to get the text come up on the screen? Enable the game object that has the UI element you want. Not sure I understand the question. Do you want it to be in the world space, part of your 3D world? If so, create a game object, attach UIPanel to it, and add widgets underneath it. You'll want to scale the panel object so that it's small first though (0.01, 0.01, 0.01 for example). You don't need the UIRoot for in-game UIs.
Title: Re: A few questions
Post by: SoullessDreamer on July 23, 2014, 03:36:31 PM
NGUI performs its own raycasts underneath mouse and touch events, provided the camera that sees the object you're interacting with has a UICamera script attached. NGUI won't perform raycasts into the screen that's not under the mouse or touch event. You can create a custom input method for NGUI that would do that, however (set UICamera.onCustomInput).
Thank you. Is this how I get game data information to show on the UI for the HUD? I will search for it as well. I have not had time to dive into NGUI as I have been watching the tutorials. Just thought I would ask here in case I can not find it.

How to get the text come up on the screen? Enable the game object that has the UI element you want. Not sure I understand the question. Do you want it to be in the world space, part of your 3D world? If so, create a game object, attach UIPanel to it, and add widgets underneath it. You'll want to scale the panel object so that it's small first though (0.01, 0.01, 0.01 for example). You don't need the UIRoot for in-game UIs.
I was meaning a HUD. So if you look at a specific rock it says "Use bla to do bla" on the players view.

I should also mention that my game is a first person shooter. Thank you again for your time.
Title: Re: A few questions
Post by: ArenMook on July 23, 2014, 09:08:09 PM
Nothing to it. When you add your first widget to the scene, NGUI will make a new camera for you that will be used to draw the UI. This camera will have a UICamera script attached, meaning it will be capable of sending out events to widgets. If you attach UICamera to your main camera, your main camera will also be sending out NGUI events and your in-game colliders will be receiving events as well. Leave the 2D (HUD) camera stationary. When you want to display something over something else, use transform.OverlayPosition.
Title: Re: A few questions
Post by: SoullessDreamer on July 26, 2014, 01:05:05 AM
Awesome that's exactly what I want. Thank you so much! Also I was able to start my State Machine from a button by setting it up like you(or one of your employees?) did on the walkthroughs here (https://www.youtube.com/watch?v=jDxWG81sNPc&feature=youtu.be#). I made an invisible sprite and gave it a script to start it. I am sure I will have more questions soon. I hope you do not mind me adding them to this thread.

You guys are amazing. Thank you again!
Title: Re: A few questions
Post by: SoullessDreamer on July 26, 2014, 01:12:01 PM
Ok another question:
I have made my buttons as I have said before. That was super easy and I thank you and your crew for this. I am however having an issue with the button placement. I have used your anchor system to get it to resize to the screen so no matter what resolution the game is set at the button will resize accordingly. My problem is that the button stretches to an inch of the side of the screen. I have made some progress with adding an invisible sprite and anchoring it to this. Sadly it doesn't make for a good look on lower resolutions. Is there a setting to set the offset of the anchor? I will post some pictures here if you don't mind taking a look as I think that my words don't do the issue justice.

What I am trying to do:
Make a button that resizes when the screen resolution changes.
Sizes we support:
1920 x 1080
      |
      |
     \ /
640 x 480
Everything in between those. I just need the button not to be almost the size of the screen when using 640 x 480. Thank you in advance.

I will have to post twice to get all the pictures as it's 1.3MB  ::)
Title: Re: A few questions
Post by: SoullessDreamer on July 26, 2014, 01:12:47 PM
Ok here is the other file. Sorry I had to double post.
Title: Re: A few questions
Post by: ArenMook on July 27, 2014, 03:15:19 AM
Use the layout system to your advantage ("Anchors" section under any widget). Consult UIRect documentation page and watch the video tutorials that cover the layout system. You can make any widget scale itself based on some other rectangle, such as that of the screen size.

Also note that the absolute easiest way to handle screen resolution changes is to change your UIRoot to "Fixed Size" type. Consult UIRoot's documentation page for an explanation.
Title: Re: A few questions
Post by: SoullessDreamer on August 05, 2014, 04:38:40 PM
Well you have been such a big help! However I have to ask another stupid question. I think you have explained it but I am confused as my situation is a bit different. After the Title screen and the options menu I have a FPS cam prefab that is called into game via code so that it's not there until you are out of the menu. What I need is to build a HUD on the FPS cam that is called into the game. How would I do that? I figure I would have to add it to the prefab itself. Sadly I don't know how to go about doing this. If you can direct me to a tutorial or if it's as easy as making a main menu.

Thank you in advance.
Title: Re: A few questions
Post by: ArenMook on August 06, 2014, 08:41:51 PM
I wouldn't add a separate UI just for that. I would just create your HUD UI elements alongside your existing ones, just underneath its own UIPanel. This way you can order it to the front or back easily. When you enable the FPS camera, just turn on the HUD panel as well. Keep its game object disabled otherwise.
Title: Re: A few questions
Post by: SoullessDreamer on September 05, 2014, 05:02:09 PM
Ok so things have been long going. I stopped the UI system to work on other areas I needed to fix. I am back on the UI system again. I have built a UI system with NGUI for both my inventory and HUD. The problem I am getting is calling the UI for the inventory with the 'I' key and have it overlay the main camera. I know the answer is staring me in the face. I have made the UI panel for my inventory. I just don't know how to have to come up on my Main Camera.

I can make unity switch from the UI panel's camera I have made for the inventory then back to the main camera with no problem. What I was hoping is to get an overly of the inventory window over my FPS view. So when I hit 'I' it shows on the screen and you can still see the FPS view in the background. I guess what is stumping me is getting NGUI attached to my FPSCamera prefab. So for the sake of argument, lets say I am using the Controller that comes with unity.(The prefab has the name of "First Person Controller" under the "Standard Assets/Character Controllers/" if you import the "Character Controllers" Asset.)

So with that Prefab how would I go about adding a HUD and an Inventory system. Like I said I already have it so the user can switch between a UI Panel and the main camera. That however does not put the Inventory UI over the main camera, it's taking the user to a 'new' camera. That's not what I want. I want the inventory UI to overlay the main camera. I also want to do something similar to the HUD, it will only come up when the player is hit(I already have the code for that) and fade after a few seconds.

I am just confused on how to attach it to the main camera I have already made. That way no matter what way you are looking with the mouse(In First Person View) the inventory will still come up on that camera. I have even watched most all the walkthrough vids. So if it was there in your walkthroughs, please post where and I will go over it again. Or if there is another thread on this I would be more than happy to read it over. I would ask if you could possibly give me some step by step on how you would attach it that would be amazing!

Thank you.
Title: Re: A few questions
Post by: ArenMook on September 06, 2014, 02:01:55 AM
Which camera the UI is drawn by is determined by the game object's layer, same as any other renderer.
Title: Re: A few questions
Post by: SoullessDreamer on September 06, 2014, 01:05:23 PM
So sticking each camera on the same layer will have one render over another if 'I' is pushed? Like I said I have one cam turn off and another on to get an 'inventory'. The problem is, I want what is on another panel I made in NGUI to overlay the one on my current FPS camera(Not made by NGUI). Or at least add NGUI properties to my current FPS camera. I am really at a loss for words on how this is done. Now I am not saying you are wrong, it's just because I don't know what you mean. How will the game's layers help in this situation?

I hate saying it but I might need a bit of a detailed walkthrough on this, if you would not mind.
Thank you
Title: Re: A few questions
Post by: ArenMook on September 07, 2014, 08:32:43 AM
UI camera, when it's created by NGUI, will automatically disable the layer it's drawing. This won't happen if you copy/paste the UI from somewhere.

Only ONE camera needs to be able to see the UI layer. For example if you have a "UI" layer, and your UI is on that layer, then you must make sure that the Main Camera does not draw the UI layer.

If you have two different UIs (for example 3D and 2D), then you must ensure that each one is on a different layer, and the UICamera that draws that UI only sees that layer.
Title: Re: A few questions
Post by: SoullessDreamer on September 11, 2014, 05:39:34 PM
Ok this helped me a lot as I have not worked to much with layers through unity. Before I was using all layers though code only. In fact most everything I do is with code vs unity's panels. My old UI system I built though code used one layer and keys toggled on and off the UI(HUD and Inventory). So I thank you for being patient with me. Sorry it took so long to understand what you were trying to convey.

Thank you again, I will be back with more questions if necessary but for now I can move forward.
Title: Re: A few questions
Post by: SoullessDreamer on September 12, 2014, 06:18:44 PM
Ok another question, with this one I hope to be less of a thorn in your side.

I have made my inventory, looks amazing! It works amazing, thank you! I however come up with another problem. How do I make another UI root and camera that goes with it? Here is what I am trying to do:

I have a camera on my inventory UI, that works just fine(via NGUI). I was wondering how I am able to make a inventory and then a HUD  this way. Every time I create a new panel, UI, etc, through NGUI it always attaches itself to the original UI root. So what I am asking is if there is a way to make another UI root so I can stick another camera on it and make another viewport for the HUD. I know how to toggle the prefabs through "SetActive" and it works. That way both the HUD and the Inventory are on the same UI root. Sadly everything under the one UI root has to be(and reverts back no matter what) to the layer the UI root is. I was just hoping to have 2 'viewports' as it were so things are not all in one space. If not possible I understand, honestly I was just trying to make it look clean.

Another way to ask my question would be, how would you set this up to be 'clean' looking? Thank you for your time again!
Title: Re: A few questions
Post by: Nicki on September 12, 2014, 06:45:26 PM
Generally you wouldn't do it as two different roots, as you would then run into trouble when positions and scales don't match anymore. Keep everything under the same root and handle multiple sections under there.

If you want to subdivide the screen into multiple parts, you can set a Clipping region on uipanels (or use a parent widget) which you can anchor the child widgets to. You can even tween the parent widgets or panels if you need things to animate in (just remember to check Offset on the panels, if you want it to move).
Title: Re: A few questions
Post by: SoullessDreamer on September 18, 2014, 06:14:20 PM
Thank you Nicki. I am just going to have all the UI elements on one panel and turn each item off with "SetActive" that way when you hit "I" It fades the HUD for the Inventory. Thank you for the help though.

I do have two other short questions:
1) With a UILabel, how do you send a number to it? I have tried this
  1. UILabel lbl = GetComponent<UILabel>();
  2. lbl.text = "Hello world!";
but the 'lbl.text' does not work with numbers(obviously). I have looked around this site and could not find anything. I even dove into UILabel and UISlider.

2) Also if there is a way to make the UISlider "Value" to be more than 0-1? Something like 0-1000? If not I can multiply the value before it gets to the UILabel. I just need to know how to send numbers to a UILabel before that can happen.

Thank you in advance.
Title: Re: A few questions
Post by: SoullessDreamer on September 19, 2014, 04:24:07 PM
Ok so I figured it out. I filtered the UISlider script to send it's value to a script I wrote:
  1. using UnityEngine;
  2. using System.Collections;
  3. using Assets.Code.Interfaces;
  4. using Assets.Code.States;
  5. using System;
  6.  
  7. public class MakeLableWholeNumber : MonoBehaviour
  8. {
  9.         public GameObject label1;
  10.  
  11.                 // Use this for initialization
  12.                 void Start ()
  13.                 {
  14.                         label1 = GameObject.Find ("/GameManager/UI Root/Control - Colored Progress Bar/Thumb/Label1");
  15.                 }
  16.        
  17.                 // Update is called once per frame
  18.                 public void Update ()
  19.                 {
  20.                        
  21.                 }
  22.  
  23.                 public void MakeItemAmount()
  24.                 {
  25.                         UILabel label = label1.GetComponent<UILabel> ();
  26.                         label.text = Mathf.RoundToInt(UIProgressBar.current.value * 1000f) + "";
  27.                 }
  28. }
This gave me not only the amount I needed(1000 items max) but will allow me to do more with everything later. Now I don't need it right at this moment, but I see a problem with it going past 1000. So my original question about setting the Value in UISlider, being 0-1, is still valid. Is there a way to up it? That way I can do above 1000?

Thank you again for your time.

Edit: Ok I found the value from the '1-0 Value slider' in UIProgressBarEditor. Sadly if changed from 0-1 to 0-100 it automatically sets the value to 1(In inspector) and you can't change it. If someone could point me in the right direction on where to edit to get the value slider to do more than 0-1 that would be amazing.

Another issue I am having is how to get the UISlider's Value to start at 0.001 vs 0.01(default). If someone could point me to the script and line where to change this, I would be most appreciative. Thank you.
Title: Re: A few questions
Post by: Nicki on September 19, 2014, 06:29:20 PM
Any value can stringified. For any given number just do

  1. label.text = 12.ToString();
  2.  

As for UISlider, no, the entire idea of a progressbar is that it's always 0-1. That said, you shouldn't use the UI to hold actual data - it should just represent your underlying dataset. So, say if you want to show 768 out of 1000 you would fill it in from your data like so:

  1. mySlider.value = 768f / 1000f;
  2.  

And if you want to make it a little nicer:

  1. public int MyMaxSliderValue = 1000;
  2. public int MyCurrentSliderValue = 500;
  3.  
  4. public void SetSlider()
  5. {
  6.   mySlider.value = MyCurrenSliderValue / MyMaxSliderValue;
  7. }
  8.  

Where you would set the max and current from wherever, and then call the SetSlider. Of course there are cleverer ways of doing it (like use properties that automatically update the slider, or a single method that sets everything and updates) but there you go.
Title: Re: A few questions
Post by: SoullessDreamer on September 19, 2014, 09:06:43 PM
No, the data is from my GameManager from a few scripts I made and attached to it. I have an idea on how to go about this. I do thank you for your help. I will post back if I have anymore questions.

Thank you again.