作者yabibear (yabi)
看板GameDesign
標題[請益] unity中gameobject改parent後顯示問題
時間Mon Jul 25 15:24:27 2016
(已解決)
目前再作背包欄跟裝備欄的部分,
想要如果再裝備欄A已經裝備了物品a,
那再拖曳其他物品到裝備欄A時,物品a會被丟回背包欄。
一開始在ItemStroage_Grid(背包)產生三個itemSlot(物品)
http://imgur.com/a/WiFWo
拖曳itemSlot0 (物品a)到裝備欄Eq01_Grid(裝備欄A)。
http://imgur.com/a/8FiJg
再拖曳itemSlot1 (物品b)到裝備欄 Eq01_Grid (裝備欄A),想要把前一個裝備
(itemSlot0 物品a)丟回ItemStroage_Grid (背包)
http://imgur.com/a/KFAxY
在Hierarchy可以看到itemSlot0已經變成ItemStroage_Grid的child.
右邊也顯示他的位置在ItemStroage_Grid裡面,可是圖顯示不出來,
也無法在對itemSlot點擊拖曳等動作。
變換parent我是這樣寫的
NGUITools.FindInParents<UIGrid(gameObject).transform.GetChild(0).
SetParent(GameObject.Find("ItemStroage_Grid").transform);
此時的gameObject="itemSlot1",我是先找他的parent "Eq01_Grid",
再找Eq01_Grid的第一個child "itemSlot0",再改變itemSlot0的parent。
請問這樣可能是哪邊出了問題呢?
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.116.164.175
※ 文章網址: https://webptt.com/m.aspx?n=bbs/GameDesign/M.1469431469.A.29B.html
1F:推 wix3000: itemSlot0.localScale = Vector3. one 07/25 18:36
2F:推 chowleft: UIWidget.CheckParent() 07/25 18:46
3F:→ yabibear: wix大試過了沒有效 07/25 20:44
4F:→ yabibear: chow大 我找不到checkparent的用法 07/25 20:45
5F:→ yabibear: 不過我有print(itemSlot0.parent) 來確定parent已經改了 07/25 20:46
6F:推 chowleft: NGUI 物件在執行階段改 parent 記得是沒用的 07/25 20:56
7F:→ chowleft: 你可直接從hierarchy拖物件到不同Panel試試 07/25 20:58
8F:→ chowleft: 要讓 Panel 更新物件必須自行通知才行 07/25 20:58
9F:→ yabibear: @@ 我的備用方案是destory這個itemSlot 07/25 21:02
10F:→ yabibear: 在背包在生一個出來 希望這樣不會太花資源 07/25 21:03
11F:→ yabibear: 通知是指抓到那個panel 執行refresh嗎 07/25 21:05
12F:→ chowleft: itemSlot.GetComponet<UIWidget>().CheckParent(); 07/25 21:07
13F:→ chowleft: 你試試看這樣有沒有編譯錯誤...現在沒 NGUI Orz 07/25 21:08
14F:→ yabibear: 我的UIWidget方法只有checklayer跟 parent等等 07/25 21:13
15F:→ yabibear: <UIWidget>.parent顯示是Panel(UIPanel) 07/25 21:14
16F:→ chowleft: 呃....那有 ParentHasChanged() 嗎? 07/25 21:15
17F:→ yabibear: 有耶 07/25 21:17
18F:→ chowleft: 恩...大大可以在拖曳完後,呼叫一下該函式看看嗎? 07/25 21:18
20F:→ yabibear: 框框有出來了!!! 07/25 21:20
21F:→ yabibear: 我在移動那個框框 貓咪圖案也會出來 07/25 21:21
22F:→ yabibear: 我抓itemslot底下的UITEXTURE然後在ParentHasChanges() 07/25 21:24
23F:→ yabibear: 完全好了 感動QQ 卡了兩天 感謝以上大大的幫忙 07/25 21:25
※ 編輯: yabibear (140.116.164.175), 07/25/2016 21:29:14