Make several buttons out of a single image (basic question)
Hi guys!
I have one full menu image that in addition to the background and all decoration already includes all of the menu items text. I want to be able to use this one image to make every single one of the menu items, rather than cutting it up in Photoshop and making each menu item separately.
I thought I could accomplish this by creating a GameObject child for each of the menu items with a Box Collider 2D + Toggle, positioning the colliders on top of their corresponding menu item. This did not work, independently of whether "Is Trigger" was checked.
What am I doing wrong? I'm sure this must be possible. I also plan to do this for an inventory grid rather than having each slot be separate.
Thanks a lot!
EDIT: I also tried removing the Button component and replacing it with a script with OnMouseDown but the mouse click never registers. There does not seem to be anything in the way given that I turned everything else off and the click still didn't register.
I realized my BoxCollider2D was set to 1x1. For some reason I assumed it took the shape of the RectTransform it was attached to. However, this did not change anything. So far the only thing that has worked was attaching this to my object, but I think it's horribly inelegant.
public GameObject toggledObject; //so that it doesn't activate and deactivate rapidly float timePassed = 0; float minTime = 1f; void Update() { if (Input.GetMouseButton(0)) { if (GetComponent<BoxCollider2D>().bounds.Contains(Input.mousePosition) && timePassed >= minTime) { toggledObject.SetActive(!toggledObject.activeSelf); timePassed = 0; } } timePassed += Time.fixedDeltaTime; }
EDIT2: for some reason all new line characters are deleted when I post this so the code is unformatted and awful to read
Your answer
Follow this Question
Related Questions
How to make button image larger than the button's interactable area? 1 Answer
Glowing 2D items 0 Answers
How to get Access of image as sprit in scripts 0 Answers
Why can't spam click on UI Image ? 0 Answers
How to import an image at runtime? 1 Answer