Showing posts with label UI. Show all posts
Showing posts with label UI. Show all posts
Thursday, February 6, 2014
Unity Part V: Return of the NGUI
I wanted to put a scroll pane in my game. Following the tutorials on the NGUI site and manually making empty GameObjects and adding the appropriate scripts didn't work at all! I had no idea what I was doing wrong. I even used the example scroll pane scene and just copied it into my project and it didn't work either! The items in the scroll pane wouldn't render. I couldn't figure it out! I copied all of the settings exactly. Finally, I used the NGUI create panel and it just magically worked. I have no idea what I was doing wrong originally, but I'll make it a point not to add scroll panes without using the menu regardless.
My next step? The trade menu. I'm going to be using Facebook to connect players. More on this in my next blog.
Wednesday, February 5, 2014
Unity 2D IV: The Reckoning of the UIs
I tentatively finished up (is that an oxymoron?) my UIs last night. I need to create assets for the other the Trade and Build aspects of the game, so I'm moving on to that today. I created an abstract class for all of my UI menus to extend from that adds some tween scaling on a specified 2D axis.
using UnityEngine;
using System.Collections;
public abstract class AbstractUi : MonoBehaviour {
private TweenScale SubMenuTween;
private GameObject LastMenu;
private Vector3 OriginalScale;
public enum Axis { X, Y, Z };
private float TweenDuration = 0.2f;
protected void Start()
{
}
protected void openSubMenu(GameObject subMenu, Axis scaleAxis)
{
subMenu.SetActive(true);
Vector2 finalScale = new Vector3(subMenu.transform.localScale.x, subMenu.transform.localScale.y, 1);
switch (scaleAxis)
{
case Axis.X:
subMenu.transform.localScale = new Vector3(0, subMenu.transform.localScale.y, subMenu.transform.localScale.z);
break;
case Axis.Y:
subMenu.transform.localScale = new Vector3(subMenu.transform.localScale.x, 0, subMenu.transform.localScale.z);
break;
default:
subMenu.transform.localScale = new Vector3(subMenu.transform.localScale.x, 0, subMenu.transform.localScale.z);
break;
}
SubMenuTween = TweenScale.Begin(subMenu, TweenDuration, new Vector3(finalScale.x, finalScale.y));
SubMenuTween.method = UITweener.Method.BounceIn;
}
protected void closeMenu(GameObject subMenu, float scaleX, float scaleY)
{
if (LastMenu == null) // prevents multiple calls to close window while one is still animating.
{
OriginalScale = new Vector3(subMenu.transform.localScale.x, subMenu.transform.localScale.y, 1);
TweenScale.Begin(subMenu, TweenDuration, new Vector3(scaleX, scaleY));
SubMenuTween.eventReceiver = gameObject;
SubMenuTween.method = UITweener.Method.EaseIn;
SubMenuTween.callWhenFinished = "onSubMenuTweenClose";
LastMenu = subMenu;
}
}
public void onSubMenuTweenClose()
{
LastMenu.SetActive(false);
LastMenu.transform.localScale = OriginalScale;
LastMenu = null;
}
}
using UnityEngine;
using System.Collections;
public class HeadsUpUi : AbstractUi {
public GameObject MenuUi;
private TweenScale MainMenuTweenScale;
// Use this for initialization
void Start () {
MenuUi.SetActive(false);
}
// Update is called once per frame
void Update () {
}
public void OnMenuButtonClick()
{
if (!MenuUi.activeSelf)
openSubMenu(MenuUi, Axis.Y);
else
closeMenu(MenuUi, MenuUi.transform.localScale.x, 0);
}
}
using UnityEngine;
using System.Collections;
public class MenuUi : AbstractUi {
// Use this for initialization
public GameObject TravelUi;
public GameObject BuildUi;
public GameObject TradeUi;
void Start ()
{
HideSubMenus();
}
private void HideSubMenus()
{
TravelUi.SetActive(false);
//BuildUi.SetActive(false);
//TradeUi.SetActive(false);
}
// Update is called once per frame
void Update ()
{
}
public void OnTravelClick()
{
HideSubMenus();
TravelUi.SetActive(true);
openSubMenu(TravelUi, Axis.X);
}
public void OnBuildClick()
{
HideSubMenus();
//openSubMenu(BuildUi, Axis.X);
}
public void OnTradeClick()
{
HideSubMenus();
//openSubMenu(TradeUi, Axis.X);
}
public void OnSubMenuItemChosen()
{
HideSubMenus();
gameObject.SetActive(false);
}
}
Ngui has click events built into buttons, so in my editor, all I had to do was assign things like OnTradeClick to the Trade button, OnBuildClick to the Build button, and so on. The UI needs some aesthetic refinement, but for now it's functional and I need to move on to asset creation to do the rest. Good bye UI programming... I shall return!!!


Monday, February 3, 2014
Unity Part III: The Anchoring
I played around with NGUI some more and I think I'm getting my head wrapped around anchoring. I got my HeadsUp menu looking fly by putting 0 in on the left and right anchor points to the UIRoot and put a higher res image whose width matched my phone's resolution's landscape width. This got it to stretch across the screen without looking lame. The next part was the dropdown menu from the HeadsUp. I did not want that one to scale, I wanted it to align on the right side of a button labeled "Menu" that is on the right side of my HeadsUp menu, as well as a fixed distance down from it. To achieve this, I created a Unified anchor on the MenuUi, set the target of the MenuUi to be the Menu-Button, then set the left and right anchor to the "Target's Right" because I always want MenuUi to be the same distance from the right edge of Menu-Button no mater how it moves or shifts around in its own anchoring system (although the Menu-Button is also a fixed distance from the right edge). The bottom and top portion of the anchor should be inconsequential since I have heights on my menu fixed, but nevertheless, I positioned them relative to the bottom.

At this point, if I adjust the Left number higher or lower it'll scale the MenuUi narrower and wider respectively because its points get closer or further from the right side. If I adjust the Right number higher or lower, it'll scale the MenuUi wider and narrower because it's getting further or closer to the right side. Maybe that's a lousy way to describe it... They're really just points that you're moving, and if you are having difficulty wrapping your head around it, take a look at the yellow lines Unity adds in the image below.

Does that help? I hope so. The blue dots show where the anchors are, the yellow dots are the target's edges. One thing to keep in mind is that changing one anchor without changing its opposite anchor will cause scaling, so I've been adjusting both together by the same amount when I don't want things to scale. To reset to my sprite's dimensions so it's not scaled, I remove the anchor and use the Snap button on the widget's dimensions drop down.

At this point, if I adjust the Left number higher or lower it'll scale the MenuUi narrower and wider respectively because its points get closer or further from the right side. If I adjust the Right number higher or lower, it'll scale the MenuUi wider and narrower because it's getting further or closer to the right side. Maybe that's a lousy way to describe it... They're really just points that you're moving, and if you are having difficulty wrapping your head around it, take a look at the yellow lines Unity adds in the image below.

Does that help? I hope so. The blue dots show where the anchors are, the yellow dots are the target's edges. One thing to keep in mind is that changing one anchor without changing its opposite anchor will cause scaling, so I've been adjusting both together by the same amount when I don't want things to scale. To reset to my sprite's dimensions so it's not scaled, I remove the anchor and use the Snap button on the widget's dimensions drop down.
Saturday, February 1, 2014
Unity 2D Part 2
Figured out a solution to making a camera fixed to the edge. Basically, I had my UI root set up wrong. Tasharen, the makers of NGUI, has their website set up differently than I remember, or they changed the site while I was reading the documentation. I noticed more in-depth documentation seemed to be on the forums and I can't seem to find the docs from their site that I was using before. After reading the section on UIRoot, I discovered that UIRoot determines a lot about scaling and resolution adjustment in a game. I set the project's resolution to 1024x600 then set UIRoot's ScalingStyle property to "FixedSizeOnMobiles" with a Maximum and Manual height of 600. After rebuilding and testing on my obnoxiously large Android phone, I got the desired effect. I'm still not sure at this point if this is the correct approach, and I'm reading more on the NGUI documents. I also am not a fan of the stretch look either, but I suspect this is the aspect of android that people warned me about; namely, producers release builds for several resolutions. I can always tell a crappy game in the Android store when graphics are all stretched to hell. I don't want my games to look crappy, so mastering clean UIs will be added to my to-do list. I suspect I'll still have to use stretch, but export multiple resolutions of my source graphics to get the best of both worlds.
While reading the doc, I also found that UIAnchor is deprecated. The original video tutorial on NGUI I watched said to use the UIAnchor class to anchor. Now, NGUI has an anchor option on all of its GameObjects (widgets?).
While reading the doc, I also found that UIAnchor is deprecated. The original video tutorial on NGUI I watched said to use the UIAnchor class to anchor. Now, NGUI has an anchor option on all of its GameObjects (widgets?).
Subscribe to:
Posts (Atom)