How to make a dropdown in Unity Editor with list from another Monobehaviour Script
I'm trying to simplify our game by making great Custom Editor object in Unity
In our game we have a list of UI interface that we set in Unity Editor like that :
When we want to show a specifique page we tell in the editor by the name of the page, like that :
Everything works well, but sometimes we forget to add the page in our UIManager and we loose time.
I was thinking to display a dropdown in Editor of all our pageName set in the UiManager
so I have created a Page class with a pageName inside, I saw that we need to make it derive from Monobehaviour or ScriptableObject to be shown in the onClick of a button And then I created a Custom Editor c# file to show all pages.
For the moment I got that
UIManager.cs
[System.Serializable]
public class Page : ScriptableObject
{
[SerializeField] public string pageName;
}
public class UIManager : MonoBehaviour {
[SerializeField]
public PageController[] pages;
public void ShowPage(string name)
{
foreach (PageController tmp in pages)
{
if (tmp.name == name)
{
tmp.Show();
}
}
}
public void ShowPage(Page page)
{
ShowPage(page.pageName);
}
}
PageEditor.cs
[CustomEditor(typeof(Page))]
public class PageEditor : Editor
{
PageController[] pages;
string[] pageName;
int index = 0;
void OnEnable()
{
pages = Link.to.uiManager.pages;
pageName = new string[pages.Length];
for (int i = 0; i < pages.Length; i++)
{
pageName[i] = pages[i].name;
}
}
public override void OnInspectorGUI()
{
DrawDefaultInspector();
index = EditorGUILayout.Popup(index, pageName);
EditorUtility.SetDirty(target);
}
}
Link.cs
public class Link : MonoBehaviour
{
public static Link to;
[Header("Managers")]
public GameManager gameManager;
public UIManager uiManager;
void Awake()
{
to = this;
}
}
But nothing is shown. On the OnClick it show None (Page)
like I have to drag and drop a Page object or something like that
My Static Link is not set yet (because it's on Awake) so I also tried to get the pages like that
UIManager uiManager = GameObject.Find("UIManager").GetComponent<UIManager>();
pages = uiManager.pages;
But same result... It there a way to do what I want ?