The game I am working on right now is a point and click adventure, and I have a hint system which is supposed to tell the player what to do depending on how far they have completed the level’s task.
The hint system is simply a canvas UI image. It’s purpose is to take on the position and rect size of whatever other ui object I tell it to mimic.
So far this has worked well for buttons, but I reach a point where I have a dropdown list. I am stumped at how to get the item gameobjects created by the DropDown UI component for my hint object to mimic. I have thought of trying to get the gameobject name although the created items are all the same name. Ideally I can get the gameobject from the DropDown component itself. All solutions are welcome.
PS:
I am not looking for alternatives of how implement this particular part of my game which uses dropdown lists since I am not in a position to influence the design of the game, and incidentally the reason why I have not explained what the dropdown list functions as in the game.
I was able to use this script to get the a List of all the RectTransforms of a dropdown
DropDown
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class Hint : MonoBehaviour {
public Dropdown dd;
void Update()
{
List<RectTransform> ddObjects = GetDropDownList();
if (ddObjects.Count > 0)
Debug.Log("Got a Valid List");
}
List<RectTransform> GetDropDownList()
{
List<RectTransform> ddList = new List<RectTransform>();
Transform dropdown = dd.transform.Find("Dropdown List");
if (dropdown != null)
{
Transform viewport = dropdown.Find("Viewport");
if (viewport != null)
{
Transform content = viewport.Find("Content");
if (content != null)
{
// skip first child since its a Inactive Template
for (int i = 1; i < content.childCount; ++i)
ddList.Add(content.GetChild(i).GetComponent<RectTransform>());
}
}
}
return ddList;
}
}
Unfortunately I could not figure out a way to get any kind of event when the drop down was actived. The only events it sends out seem to be when you actually select an object… Not when you press the ^ to bring up the list. So I had to check each Frame to see if they clicked it, which is suboptimal at best. If you can figure out how to get an event when the dropdown is pressed you can use the above method to get the RectTransforms.
Thanks. I’ll probably try it out and adapt it using GetChild(index) since I’m tracking what’s to be selected in the dropdown list for my hint to go to via integers. I actually did the detection of clicking on the DropDown gameObject by adding a script with the OnPointerClick event from EventSystems, and having OnPointerClick call a function in my dropdown managing script.
Would still like to see if there are other solutions, but will certainly be trying this out in the mean time.
Alright Ive got my hint system to get the dropdown list gameObject, and the individual item gameObjects in the dropdown list(Thanks a ton @takatok your solution works great). However, now I’m unable to detect when the player has “deselected” the dropdown gameObject.
So when the player is supposed to open a dropdown list, the hint appears above the dropdown list. when the dropdown list is open, the hint also goes to the item gameObject which it is supposed to go to. However, if the player clicks on anything other than the highlighted object, the hint stays in the space it previously occupied.
I am hoping to know when the player has clicked away from the dropdown list, and when I register that happening i can reposition my hint object back onto the dropdown list gameObject.
Sorry if I am sounding kinda needy, but my mind is just spent right now.
EDIT:
I was able to detect this gameObject named “Blocker” which was created upon the opening of the dropdown list. It is essentially a button to detect a click anywhere other than stuff in the dropdown menu. After detecthing this gameObject, I can add a listener to it’s Button component and detect when the player has simply deselected from the DropDown menu.