I am looking to add a button to a menu screen, it does not need to be anything complicated, in fact, the easier the better!
The project is a Cocos2d-x project, and here is the existing start button code;
fileName = (CCString*)sc->imageDict->objectForKey("GUI_StartButton-image");
CCSprite *startGameSprite = CCSprite::createWithSpriteFrameName( fileName->m_sString.c_str() );
CCSprite *startGameSpriteClick = CCSprite::createWithSpriteFrameName( fileName->m_sString.c_str() );
startGameSpriteClick->setColor( ccc3(128, 128, 128) );
CCMenuItemSprite *startGameItem = CCMenuItemSprite::create(startGameSprite, startGameSpriteClick, NULL, this, menu_selector(MainMenu::menuStartGameCallback));
startGameItem->setPosition( GameController::sharedGameController()->getPositionOfUIItem( "GUI_StartButton-image" ) );
pMenu = CCMenu::create(startGameItem, NULL);
All I want to do is add another button and call it 'Information'
I can create the image.png for it no problem, but if anyone could assist in helping me with what I need to type for the button to appear, for the moment, let's use hard coded values, say for example, to put it in the top left hand corner of the screen if possible?
I guess it needs an action to tell it what to do when pressed, I have code that I can put inside that, but I am not sure what I need to put to call the action, ordinarily it would be a void or IBAction but this is Cocos so I am unfamiliar with it.
Would appreciate any assistance! Thanks :)
Chris
Have you added the menu as a child to your scale/current layer?
Until you add it as as a child node it will not be rendered.
I would create a menu as follows:
// create the sprites for button up and button down
CCSprite* startGameSprite = CCSprite::createWithSpriteFrameName("start_button_up.png");
CCSprite* startGameSpriteClick = CCSprite::createWithSpriteFrameName("start_button_down.png");
// create a menu item (button) with the up/down sprites
CCMenuItemSprite* startGameItem = CCMenuItemSprite::create(startGameSprite, startGameSpriteClick, this, menu_selector(MainMenu::menuStartGameCallback));
// create a menu to hold the buttons (remembering to NULL terminate the list)
CCMenu *menu = CCMenu::create(startGameItem, NULL);
// position the entire menu
menu->setPosition(0,0);
// add it as a child (so it appears
this->addChild(menu);
Notes:
menu_selector(MainMenu::menuStartGameCallback)
This tells the CCMenuItemSprite which function to call when a touch is detected, and should be a function in the class MainMenu of the following structure:
void MainMenu::menuStartGameCallback(CCObject* sender)
{
CCLOG("Hello!");
}
Remember to declare this function in MainMenu.h
Now, to add another button to this menu you just need to make an addition to the above code:
// NEW /////////
CCSprite* informationSprite = CCSprite::createWithSpriteFrameName("info_button_up.png");
CCSprite* informationSpriteClick = CCSprite::createWithSpriteFrameName("info_button_down.png");
CCMenuItemSprite* infoGameItem = CCMenuItemSprite::create(informationSprite, informationSpriteClick, this, menu_selector(MainMenu::menuInfoCallback));
// END NEW //////
CCSprite* startGameSprite = CCSprite::createWithSpriteFrameName("start_button_up.png");
CCSprite* startGameSpriteClick = CCSprite::createWithSpriteFrameName("start_button_down.png");
// create a menu item (button) with the up/down sprites
CCMenuItemSprite* startGameItem = CCMenuItemSprite::create(startGameSprite, startGameSpriteClick, this, menu_selector(MainMenu::menuStartGameCallback));
// create a menu to hold the buttons (remembering to NULL terminate the list)
// NEW - we include the new info item
CCMenu *menu = CCMenu::create(startGameItem, infoGameItem, NULL);
// position the entire menu
menu->setPosition(0,0);
// add it as a child (so it appears
this->addChild(menu);
Remembering to create the callback function:
void MainMenu::menuInfoCallback(CCObject* sender)
{
CCLOG("Information!");
}
Related
I'm trying to create a scroll grid view in which every cell object is tapable.
When a cell object is tapped I want to scale and traslate it to the center of the screen and render it above other cells.
I was able to make it tapable and scale it in its position. Now I want to move the cell object to the center of the screen and render it above other cells.
I've tried many solutions but none of them works.
This is my hierarchy:
This is the grid in normal state:
This is the grid when a cell was tapped:
I'm populating the grid from a C# script dynamically.
void Populate()
{
GameObject cardContainerInstance, cardInstance;
foreach (var c in cardsCollection.GetAll())
{
if (c.IsOwned)
{
cardContainerInstance = Instantiate(cardContainer, transform);
cardInstance = cardContainerInstance.transform.Find("Card").gameObject;
var cardManager = cardInstance.GetComponent<CardManager>();
cardManager.card = c;
cardManager.AddListener(this);
}
else
{
Instantiate(cardSlot, transform);
}
}
}
public void OnCardClick(GameObject cardObject, Card card)
{
Debug.Log("OnCardClick " + card.name);
if (openedCard != null) {
if (openedCard.Number == card.Number)
{
CloseCard(openedCardObject);
}
else
{
CloseCard(openedCardObject);
OpenCard(cardObject, card);
}
}
else
{
OpenCard(cardObject, card);
}
}
void OpenCard(GameObject cardObject, Card card)
{
//cardObject.GetComponent<Canvas>().sortingOrder = 1;
var animator = cardObject.GetComponent<Animator>();
animator.SetTrigger("Open");
openedCard = card;
openedCardObject = cardObject;
}
void CloseCard(GameObject cardObject)
{
var animator = cardObject.GetComponent<Animator>();
animator.SetTrigger("Close");
openedCard = null;
openedCardObject = null;
}
I can't figure out how to move the cell to the center and render it above others.
Note that all is animated using an animator attached to the object itself.
Could anyone help me please? Thank you very much!
EDIT: more details
All cell object have the following hierarchy:
where:
CardContainer is an empty object added to use animator on Card child object
Card is the object itself that has a script, a canvas renderer and an animator
StatsImage is the object that slide out when the card is tapped
Image is a calssic UIImage with Image script, Shadow script and canvas renderer
Other component are simple texts.
EDIT: fix in progress
Trying to apply this suggestions I was able to manage the rendering order (as you see on the image below) but it seems that prevent touch events to be detected on the game object.
I've added a GraphicsRaycaster too and now the bottom horizontal scroll view scrolls again but only if I click and drag a card.
Moreover, with the GraphicsRaycaster, the main grid card still are not clickable and it's possible to open the card only if it is behind the bottom panel (if I click on the red spot in the image below the card behind the panel receives che click)
This is the CardContainer at runtime(note that I'm attaching new Canvas and GraphicsRaycaster on the CardContainer, which is the "root" element):
You didn't clarify whether you are using a sprite renderer or some other method but here is an answer for each.
Sprite renderer:
this the simple one. In each sprite renderer, there is a variable called "sortingOrder" in script and "Order in layer" in the inspector. sprite renderer with sorting Orders that are higher is rendered first. All you would need to do is call:
cardObject.GetComponent<SpriteRenderer>().sortingOrder = 1;
when you click the card, and
cardObject.GetComponent<SpriteRenderer>().sortingOrder = 0;
when you unclick it. I hope that makes sense!
Other Method:
this one is a bit harder and I would suggest that you switch to sprite renderers and it will be much easier and more stable down the road, but I can understand if you have already written a lot of scripts and don't want to go back and change them.
Anyway, all you will need to do Is create two layers: cardLower and cardUpper. then create a new camera and call it topCamera. now under the top camera object in the inspector, change the culling mask (it's near the top) and make sure cardUpper is selected. then change the Clear flags (first one) to "Don't Clear" finally change the depth to 0 (if that doesn't work change it to -2). Now objects in the cardUpper will always be rendered above everything else. You can change the layer through script with
cardObject.layer = "cardUpper"
or
cardObject.layer = "cardLower"
I hope that helps!
Ok, so its pretty simple. So you are going to want to add another canvas component to the game object, and check the override sorting to true. Then use
cardObject.GetComponent<Canvas>().sortingOrder = 1;
to place it in the front and
cardObject.GetComponent<Canvas>().sortingOrder = 0;
to put it in the back.
you are also going to need to put a GraphicsRaycaster on to each of the cardObjects
Ignore my other answer about sprite renderers, they are not needed here
I want to add a background to the prefab when it clicked as it should look as its selected I have added the click listener which is working but I'm not able to add a background
for (int i = 0; i < list.Count; i++) {
GameObject tempobj = Instantiate (amtPrefab);
listGameObject.Add(tempobj);
tempobj.GetComponent<Button>().onClick.AddListener(() =>
OnButtonClick(tempobj)
);
}
I'm using this code and I want to change the color in the OnButtonClick method that i'm passing the clicked object to
Thanks in advance for the help
Use Ui Button how you are doing currently and in your Background image of your prefab add your script to change the color.
Subscribe to the click event:
tempobj.GetComponent<Button> ().onClick.AddListener(() => OnButtonClick(tempobj));
And only need to call your delegate event OnButtonClick()
void OnButtonClick()
{
//change backgroung color code here
}
I'm trying to use the new UI objects in Unity 4.6 to create a ranking screen where I download information about the users and show them. Among those informations I have an URL pointing to their picture.
Before the UI tools I was doing it with GUITexture and using the www.LoadImageIntoTexture method, but there doesn't seem to be a similar solution for UI Images. Using a GUITexture with a Rect Transform doesn't seem to work.
Should I just manually download the image and load it into the image or is there a similiar solution as with using WWW class?
The new Image component takes requires a sprite to render.
What should work is download a PNG texture using the WWW class, then creating a sprite and finally assigning it to the component.
Here's some sample code.
Create a new Monobehaviour, paste below code into it (I called my script "LoadImageToButton")
Create a new UIButton
Attach the Monobehaviour created in Step 1 to the Button
In the Button's "OnClick" event, Drag the button created in Step 2 (this same button) into the GameObject field
Select the "LoadImage" function as the function to call for the OnClick event. (In my case it was "LoadImageToButton.LoadImage")
Hit Play, click the button. You should see the button's image change (It looks terrible, but it works)
See the pictures attached for actual results.
//This is the method you call if you use the "OnClick" event from a button click.
//We cannot call the Coroutine function below, because it breaks the rule for
//the event system. i.e. Must be a return type of void.
public void LoadImage () {
//Call the actual loading method
StartCoroutine(RealLoadImage());
}
//This is where the actual code is executed
private IEnumerator RealLoadImage () {
//A URL where the image is stored
string url = "http://www.axsu3d.com/DLs/AxSWP8Plugin/TileLogo.png";
//Call the WWW class constructor
WWW imageURLWWW = new WWW(url);
//Wait for the download
yield return imageURLWWW;
//Simple check to see if there's indeed a texture available
if(imageURLWWW.texture != null) {
//Construct a new Sprite
Sprite sprite = new Sprite();
//Create a new sprite using the Texture2D from the url.
//Note that the 400 parameter is the width and height.
//Adjust accordingly
sprite = Sprite.Create(imageURLWWW.texture, new Rect(0, 0, 400, 400), Vector2.zero);
//Assign the sprite to the Image Component
GetComponent<UnityEngine.UI.Image>().sprite = sprite;
}
yield return null;
}
How to know the distance of the Drag, unused PositionUP or onInputUp to differentiate a drag from a long click?
http://i.stack.imgur.com/FvVsN.pnghttp://www.html5gamedevs.com/uploads/monthly_07_2014/post-9642-0-48696600-1405440289.png
it is a some code:
var fnd = game.add.group();
var o = fnd.create(game.world.randomX, game.world.randomY, 'fon');
o.events.onDragStart.add(this.DragActivo, this);
DragActive = function (image) {
//When something has been moved is drag but when something has been pressed for long time is clicked
};
You can use the property sprite.input.dragDistanceThreshold = 3; in order to start dragging only if the pointer moves a minimum amount of pixels when after onInputDown.
Here's the doc
I solved my problem that you have to do something like this.
I used the function mouseup and inside this one I placed the mousemove to recognize when the mouse moves it is a drag, if it does not do it is a very long click
because this framework does not have
i have one window panel and i want to set image in it.so i do,
Window window = new Window();
Panel panel = new Panel();
AbsolutePanel absolutePanel = new AbsolutePanel();
Image image = new Image("img/heat_map.jpg");
absolutePanel.add(image);
Image ap1Image = new Image("img/end.PNG");
ap1Image.getElement().getStyle().setMargin(1, Unit.PX);
absolutePanel.add(ap1Image);
panel.add(absolutePanel);
window.add(panel);
but i stuck in code as i can't overlap another small icon image on main image(heat_map).
i want onclick event on that icon image.but i can't overlap images in window panel.please help me out.
It seems that you using something like GXT not pure GWT. But anyway - AbsolutePanel should implement something like add(Widget, int left, int top) method so you need use it instead of simple add(widget)
First thing is in your code is you can not instantiate GWT Window Class since the constructor Window() is not visible.
Second thing is there is no add method in window class.
And finally to overlap your images one on another you need to apply Some CSS
(Z-index..positions )
CSS Divs overlapping, how do I force one above the other?
And finally
you can simply add a click handler to image.
imageIcon.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
// Do something....
}
});
Good luck.