Popup.cs bombing-out on me.

Hi,

I just tried to use the Popup.cs (put into standard assets/scripts folder) along with the PopupListUsageExample.js but it kept crashing unity.

I made a scene from scratch with only an empty game object and the example script attached. It’s still just crashing unity when I try to click the button/menu (no dropdown).

anyone know what’s wrong?

(I have no other code in the scene)

Can you post your code. It will us help you. :smiley:

I’m using the code straight from the wiki. I changed GUIContent text and added an Application.OpenURL.

using UnityEngine;


public class Popup {
    static int popupListHash = "PopupList".GetHashCode();
   
    public static bool List (Rect position, ref bool showList, ref int listEntry, GUIContent buttonContent, GUIContent[] listContent,
                             GUIStyle listStyle) {
        return List(position, ref showList, ref listEntry, buttonContent, listContent, "button", "box", listStyle);
    }
   
    public static bool List (Rect position, ref bool showList, ref int listEntry, GUIContent buttonContent, GUIContent[] listContent,
                             GUIStyle buttonStyle, GUIStyle boxStyle, GUIStyle listStyle) {
        int controlID = GUIUtility.GetControlID(popupListHash, FocusType.Passive);
        bool done = false;
        switch (Event.current.GetTypeForControl(controlID)) {
            case EventType.mouseDown:
                if (position.Contains(Event.current.mousePosition)) {
                    GUIUtility.hotControl = controlID;
                    showList = true;
                }
                break;
            case EventType.mouseUp:
                if (showList) {
                    done = true;
                }
                break;
        }
       
        GUI.Label(position, buttonContent, buttonStyle);
        if (showList) {
            Rect listRect = new Rect(position.x, position.y, position.width, listStyle.CalcHeight(listContent[0], 1.0f)*listContent.Length);
            GUI.Box(listRect, "", boxStyle);
            listEntry = GUI.SelectionGrid(listRect, listEntry, listContent, 1, listStyle);
        }
        if (done) {
            showList = false;
        }
        return done;
    }
}

and the example code

private var showList = false;

private var listEntry = 0;
private var list : GUIContent[];
private var listStyle : GUIStyle;
private var picked = false;

function Start () {
    // Make some content for the popup list
    list = new GUIContent[5];
    list[0] = new GUIContent("ROTATE: LMB");
    list[1] = new GUIContent("ZOOM: SHIFT + LMB");
    list[2] = new GUIContent("SELECT:  DBL-CLICK");
    list[3] = new GUIContent("RESET: DBL-CLICK");

    // Make a GUIStyle that has a solid white hover/onHover background to indicate highlighted items
    listStyle = new GUIStyle();
    listStyle.normal.textColor = Color.white;
    var tex = new Texture2D(2, 2);
    listStyle.hover.background = tex;
    listStyle.onHover.background = tex;
    listStyle.padding.left = listStyle.padding.right = listStyle.padding.top = listStyle.padding.bottom = 4;
	

}

function OnGUI () {
    if (Popup.List (Rect(50, 100, 100, 20), showList, listEntry, GUIContent("Navigation"), list, listStyle)) {
        picked = true;
    }
    if (picked) {
        GUI.Label (Rect(50, 70, 400, 20), "You picked " + list[listEntry].text + "!");
    }
	
	if (GUI.Button(Rect(800,40,20,20),"I"))
	{
	//Application.OpenURL ();
	}
	
}

Ugh. I think it’s because I deleted one of the menu options, so the 5th element of the declared list array isn’t defined. Though Unity crashes instead of giving an error.

What happens if you put back the deleted line? Does it still crash?

No. It’s working fine now. Thanks.