Unity 擴充套件彈出選單視窗

2021-10-11 01:25:14 字數 4595 閱讀 4845

unity 編輯器下提供了系統級的選單彈窗,會阻塞訊息,卡住渲染,以及不能進行搜尋等功能,而它自己有非阻塞版本的彈窗,但不開放給使用者使用,比如這個介面:

/// genericmenu的unity風格版本

///

public

class

genericpopupmenu

:popupwindowcontent

private

class

menuitem

public

menuitem

(guicontent _content,

bool _separator,

bool _on,

genericmenu.menufunction2 _func,

object _userdata)

public

menuitem

(guicontent _content,

bool _separator,

bool _on,

bool _enabled)

}private

static

styles s_styles;

private

static

string s_textfieldname =

"genericpopupmenutextfield"

;private

readonly

arraylist m_menuitems =

newarraylist()

;private

readonly

arraylist m_menufiltereditems =

newarraylist()

;private

bool m_enablefiltered =

true

;private

string m_filteredtext =

string

.empty;

private

int m_selectedindex;

private

bool m_focustext =

true

;private

double m_focustexttime;

private

vector2 m_scrollpos;

private

bool m_ignorecase;

public

genericpopupmenu

(bool ignorecase =

false

)public

void

additem

(guicontent content,

bool on,

genericmenu.menufunction func)

public

void

additem

(guicontent content,

bool on,

genericmenu.menufunction2 func,

object userdata)

public

void

adddisableditem

(guicontent content)

public

void

addseparator

(string path)

public

intgetitemcount()

public

void

showascontext()

dropdown

(new

rect

(event.current.mouseposition.x, event.current.mouseposition.y,0f,

0f));

}public

void

dropdown

(rect position)

public

override

void

onopen()

public

override

vector2

getwindowsize()

float w =

230f;if

(maxlen >25)

return

newvector2

(w,320f);

}public

override

void

ongui

(rect rect)

drawcustomtextfield

(rect)

;drawlist

(rect);if

(event.current.type == eventtype.mousemove)

}private

void

drawcustomtextfield

(rect rect)

event current = event.current;

if(current.type == eventtype.keydown)

}string text = m_filteredtext;

rect rect2 =

newrect

(rect.x +

5f, rect.y +

5f, rect.width -

10f-

14f,

16f)

; gui.

setnextcontrolname

(s_textfieldname);if

(m_focustext)

else

}string text2 = editorgui.

textfield

(rect2, text, s_styles.customtextfield)

;rect position = rect2;

position.x +

= rect2.width;

position.width =

14f;

if(gui.

button

(position, guicontent.none, text2 !=

string

.empty ? s_styles.customtextfieldcancelbutton : s_styles.customtextfieldcancelbuttonempty))if

(text != text2)

if(contenttext.

contains

(m_filteredtext))}

}}private

void

drawlist

(rect rect)

guilayout.

endscrollview()

; guilayout.

endarea()

;}private

void

drawlistelement

(rect rect,

int index,

menuitem menuitem)

else

if(menuitem.func !=

null

) editorwindow.

close()

; guiutility.

exitgui()

;}}break

;case eventtype.mousemove:

if(rect.

contains

(current.mouseposition)

)break

;case eventtype.repaint:

}break;}

}private

void

changeselectedindex

(int change)

int index = filteredcount <=0?

0:(m_selectedindex + change + filteredcount)

% filteredcount;

setselectedindex

(index);}

private

void

setselectedindex

(int index)

}效果類似如下:

用彈出選單模擬視窗的選單

在加了skin的不規則區域視窗,視窗選單很難固定在原來的位置,無法用原來的方式實現。這裡提供了一種用彈出選單模擬視窗選單的方法。首先在視窗的任意乙個區域畫出選單的效果,這個區域我們稱為選單區,我們把每個選單項在視窗中的位置儲存下來 crect m rcmenu 3 程式主要要處理三個訊息 wm lb...

Unity視窗彈出順序管理

遊戲互動時可能同時觸發多個事件,於是同時彈出多個彈窗,堆在一起不好看而且可能影響判斷。利用佇列的結構解決視窗彈出問題。所有視窗掛在同乙個父物體下 總控制器 public listwindows 在視窗把需要控制的視窗放入 int numofwindows 控制的視窗數量 string queue 視...

Unity3D的編輯器擴充套件 右鍵彈出選單欄

最近用u3d的編輯器擴充套件api來寫乙個動畫幀事件編輯器,其中有不少實用的東西,在這裡分享下 現在要是在乙個介面上繪製乙個小盒子,然後對著滑鼠右鍵點選盒子,可以彈出選擇框,大概如下 public void draw rect rect 效果應該是醬紫的 看起來還不錯 event.current這個...