Author Topic: NGUI Element - Transform position  (Read 6154 times)

Briksins

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 5
    • View Profile
NGUI Element - Transform position
« on: May 11, 2014, 07:10:52 AM »
Hello everyone, im having some difficulties with NGUI (3.5.8)

We are working on applications and GUI where GUI elements should be dynamically moving here and there, new items appears, move in, move out and so on... It would be kind of hard to define all GUI in editor, as GUI have to change dynamically depending on situation. So I have to create NGUI elements from code, however i faced one problem with transform position - it is set to zero always, and im not sure how to fix it

here is my small example code:

  1. public class GUIManager : MonoBehaviour {
  2.  
  3.  
  4.     private GameObject inputField;
  5.  
  6.     public void LoginButtonClick()
  7.     {
  8.         Debug.Log("Login Button Clicked");
  9.         inputField = CreateInputField("Please Enter some text", new Vector3());
  10.         CreateButton("Get Input Value", new Vector3(0.0f, -45f, 0.0f), PrintInputFieldValue);
  11.  
  12.     }
  13.  
  14.     public GameObject CreateInputField(string baseText, Vector3 pos)
  15.     {
  16.         GameObject inputPrefab = Resources.Load<GameObject>("Prefabs/GUI/InputField");
  17.         GameObject input = Instantiate(inputPrefab, new Vector3(), transform.rotation) as GameObject;
  18.         input.transform.position = pos;
  19.         SetTextToChiledLable(input, baseText);
  20.         return input;
  21.     }
  22.  
  23.     public void CreateButton(string btnName, Vector3 pos, EventDelegate.Callback method)
  24.     {
  25.         GameObject buttonPrefab = Resources.Load<GameObject>("Prefabs/GUI/Button");
  26.         GameObject btn = Instantiate(buttonPrefab, pos, transform.rotation) as GameObject;
  27.         SetTextToChiledLable(btn, btnName);
  28.         btn.GetComponent<UIButton>().onClick.Add(new EventDelegate(method));
  29.     }
  30.  
  31.     public void SetTextToChiledLable(GameObject parent, string text)
  32.     {
  33.         foreach(Transform child in parent.transform)
  34.         {
  35.             if (child.gameObject.name.Equals("Label"))
  36.             {
  37.                 child.gameObject.GetComponent<UILabel>().text = text;
  38.                 break;
  39.             }
  40.         }
  41.     }
  42.  
  43.     public string GetTextFromChiledLable(GameObject parent)
  44.     {
  45.         foreach (Transform child in parent.transform)
  46.         {
  47.             if (child.gameObject.name.Equals("Label"))
  48.             {
  49.                 return child.gameObject.GetComponent<UILabel>().text;
  50.             }
  51.         }
  52.         return "";
  53.     }
  54.  
  55.     public void PrintInputFieldValue()
  56.     {
  57.         Debug.Log("Input field value was: " + inputField.GetComponent<UIInput>().value);
  58.     }
  59.  
  60. }
  61.  

In 2 words:
I have static predefined button created in Editor with attached call back method: "LoginButtonClick", and predefined prefabs (button, input field).
By clicking that static button it would call: "LoginButtonClick"
which create one input field and one button where input field will be at zero coordinates and button should be a bit bellow (-45 on Y axes)

When i debug the process of creation of the button, prefab instantiated at the right position, however when item appear in the scene it already some how resets to zero, so it appears on top of input field and overlap it.
moreover setting "myButton.transform.position" to newly created button after it was placed in the scene doesn't have any effect.

So im wondering how to set position of GUI element in the script?
am also very surprised that there are so little information and tutorials on manipulations with NGUI trough script, I cant believe that most of the developers enough just in-editor gui definition.
I even have specious that im missing some concept of using NGUI and doing something wrong. or is there already some predefined helper code like "NGUI.CreateButton(btnName, callback)"

Here is logic my current test case:
Depending on application logic, i need to display NGUI elements for Login or Register actione where amount and position of elements could be different.
I know i can set all of that in editor with use of "tabs" where GUI for each tab (Login/Register) is predefined, however what if i don't want any tabs? If i need the same single screen where i would need 2 input fields(login/pass) and 2 buttons (login/Register) for Login screen and once user press "Register" add more (with nice animation: move in/move out) 3 input fields (email, name, etc..) without separate screens or tabs?
I believe that is achievable only trough script? am i right? Or im seriously missing some NGUI concept?   



ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUI Element - Transform position
« Reply #1 on: May 11, 2014, 08:32:39 AM »
Never use Instantiate. Use NGUITools.AddChild.

Instead of iterating through children like that, write a script that would hold references to the relevant fields. For example like this:
  1. using UnityEngine;
  2.  
  3. public class MyScript : MonoBehaviour
  4. {
  5.     public UILabel nameLabel;
  6.     public UILabel textLabel;
  7. }
Attach this script to your prefab and set the referenced labels. Then all you have to do to modify their text is GetComponent<MyScript>() and work with the referenced values.

Now onto your problem.... "myButton.transform.position"? That's world position, and considering that UIRoot is scaled to 1/Screen.height, it's definitely not what you should be looking at.

Learn the difference between transform.position and transform.localPosition. It's a core concept of Unity, and you need to know this before doing anything even remotely non-trivial. As you learn it, your issue will become obvious.

Briksins

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 5
    • View Profile
Re: NGUI Element - Transform position
« Reply #2 on: May 11, 2014, 10:01:13 AM »
Thank you very much for your replay
And thank you for a tip to avoid the loop and iterating trough childs, will defiantly include script to prefab to store references.

Regarding transform.position - that was very silly mistake :) got it sorted!
Regarding "NGUITools.AddChild" - I believe parent have to be "UI Root"? is there a quck way to get root? like:

NGUITools.AddChild(NGUITools.getRoot(), myPrefab); cause i didnt find
Or i have to make reference as you recommend with labels?

P.S. is that you who developing new GUI system for Unity 3.6? Got beta build of it, looks very nice and promising, but.... NGUI is way more advanced at the moment, so we rolled back to 4.3 and NGUI :)

P.P.S. I would defiantly recommend include more tutorials and documentation regarding how to work with NGUI or New Unity GUI from the code, rather than in Editor drag and drop.
What is more - would be nice to have pre-scripted methods to add NGUI element like
NGUITools.CreateButton(*params*)
NGUITools.CreateInputFied(*params*)
and so on...

But anyway, thank you very much and you know how great you are with your tool     

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUI Element - Transform position
« Reply #3 on: May 11, 2014, 02:06:21 PM »
I left Unity at the end of November last year. Other people are doing the upcoming GUI.

UIRoot.list gives you a list of all available UI roots for you to use. So if you only have one, you can use NGUITools.AddChild(UIroot.list[0].transform, prefab);