- Home /
The popup when clicking an EditorGUI.EnumPopup inside a scaled GUI.matrix, is opened in the wrong location
Is there any solution for this other than rewriting the whole function yourself, with a modified positioning system?
Comment
Answer by helarts · Nov 04, 2015 at 11:07 AM
As I could not find any real solution (and still seems to be an issue in ShaderForge ^^), here is a simple workaround: Replace the EnumPopup by a button to trigger a GenericMenu when you click it, something like that:
using UnityEngine;
using UnityEditor;
using System.Collections;
public class FakeEnumPopup : EditorWindow
{
[MenuItem("Window/FakeEnumPopup")]
static void ShowEditor()
{
FakeEnumPopup win = EditorWindow.GetWindow<FakeEnumPopup>();
win.Show();
}
GenericMenu menuToShow;
GenericMenu myPopup;
string[] myPopupList;
int myPopupSelectedID = 0;
float zoom = 1f;
void InitMenus()
{
myPopupList = new string[]{"item1", "item2", "item3", "item4", "item5"};
myPopup = new GenericMenu();
for (int i =0; i < myPopupList.Length; i++)
{
myPopup.AddItem(new GUIContent(myPopupList[i]), false, MyPopupCallback, i);
}
}
void MyPopupCallback(object obj)
{
myPopupSelectedID = (int)obj;
menuToShow = null;
}
void OnGUI()
{
zoom = GUILayout.HorizontalSlider(zoom, 0.1F, 10.0F);
// Initialize the generic menu(s) if it was not done yet
if(myPopupList == null)
InitMenus();
// Mess with GUI.Matrix
Matrix4x4 previousMatrix = GUI.matrix;
GUIUtility.ScaleAroundPivot(new Vector2(zoom, zoom), new Vector2(Screen.width / 2, 16f));
GUILayout.Label("some random content in the scaled area");
GUILayout.Label("and some more");
// Call myPopup when the button is clicked
if (GUILayout.Button(myPopupList[myPopupSelectedID]))
{
menuToShow = myPopup;
}
// Restore GUI.Matrix
GUI.matrix = previousMatrix;
// Check if a menu has been called and show it
if (menuToShow != null)
{
menuToShow.ShowAsContext();
menuToShow = null;
}
}
}