Извиняюсь сразу за глупый вопрос, но не могу сам справиться )
Есть стандартный код popup:
Синтаксис:
Используется csharp
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;
}
}
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;
}
}
Синтаксис:
Используется javascript
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("Foo");
list[1] = new GUIContent("Bar");
list[2] = new GUIContent("Thing1");
list[3] = new GUIContent("Thing2");
list[4] = new GUIContent("Thing3");
// 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);
var colors = new Color[4];
for (color in colors) color = Color.white;
tex.SetPixels(colors);
tex.Apply();
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("Click me!"), list, listStyle)) {
picked = true;
}
if (picked) {
GUI.Label (Rect(50, 70, 400, 20), "You picked " + list[listEntry].text + "!");
}
}
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("Foo");
list[1] = new GUIContent("Bar");
list[2] = new GUIContent("Thing1");
list[3] = new GUIContent("Thing2");
list[4] = new GUIContent("Thing3");
// 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);
var colors = new Color[4];
for (color in colors) color = Color.white;
tex.SetPixels(colors);
tex.Apply();
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("Click me!"), list, listStyle)) {
picked = true;
}
if (picked) {
GUI.Label (Rect(50, 70, 400, 20), "You picked " + list[listEntry].text + "!");
}
}
Все работает, появляется кнопка Click me, при ее нажатии выпадает меню.
Как сделать, чтобы выпадающее меню появлялось не при клике на кнопке "click me", а при нажатии на объект к которому прикреплен скрипт (кнопка click me не нужна)?
Спасибо, кто подскажет ))