[Unity] Loaded event issues
Posted: 09 Jul 2015, 20:35
Hi,
I've been pulling my hair out trying to get this to work, but I can't seem to...
The idea:
- A scrollviewer contains a stackpanel
- The stackpanel contains a bunch of items in it.
- I want to know where each item is, relative to the scrollviewer.
- A control contains buttons, with one button per item in the scrollviewer.
- Clicking the buttons will scroll the scrollviewer to the position of the item
So I created an attached property to be attached to any scrollviewer (including a custom one we've built)
I've created a gist with all the attempts I've tried to get this working: https://gist.github.com/MrHayato/64402a3d1ca1139fcd6e
The attached property can be seen in this gist as TrackerA and looks pretty straightforward. However, the debug line only writes out (0,0) as the position for each of the children (even though they're all visible, no bindings).
I then added child.UpdateLayout() to each of the children, thinking perhaps it needs to be measured and arranged. You can see this at TrackerB.cs (line 29). This was a little better, as I was getting non-zero values. However, it seems the event fires twice, and then throws an exception:
So maybe loaded is getting fired too often? Not sure why, since it should only fire once. So, I'll make sure it is only fired once (TrackerC line 15). However, we are back to the first result, where everything reports 0,0.
What gives? Why is Loaded firing more than once if I call UpdateLayout on its children? And why aren't the correct sizes being reported back without it? I thought the loaded event would fire after it's loaded.
I've been pulling my hair out trying to get this to work, but I can't seem to...
The idea:
- A scrollviewer contains a stackpanel
- The stackpanel contains a bunch of items in it.
- I want to know where each item is, relative to the scrollviewer.
- A control contains buttons, with one button per item in the scrollviewer.
- Clicking the buttons will scroll the scrollviewer to the position of the item
So I created an attached property to be attached to any scrollviewer (including a custom one we've built)
I've created a gist with all the attempts I've tried to get this working: https://gist.github.com/MrHayato/64402a3d1ca1139fcd6e
The attached property can be seen in this gist as TrackerA and looks pretty straightforward. However, the debug line only writes out (0,0) as the position for each of the children (even though they're all visible, no bindings).
I then added child.UpdateLayout() to each of the children, thinking perhaps it needs to be measured and arranged. You can see this at TrackerB.cs (line 29). This was a little better, as I was getting non-zero values. However, it seems the event fires twice, and then throws an exception:
Code: Select all
Exception: System.ApplicationException: System.ApplicationException: System.ApplicationException: System.ApplicationException: System.ApplicationException: System.ApplicationException: System.ApplicationException: System.ApplicationException: System.ApplicationException: System.ApplicationException: System.ApplicationException: System.ApplicationException: System.ApplicationException: System.ApplicationException: Element is already loaded
at Noesis.UIElement.UpdateLayout () [0x00015] in D:\Dev\iQmetrix\XQ.Shelf\Un
Noesis.Error.Check () (at Assets/Plugins/NoesisGUI/Scripts/Core/NoesisError.cs:21)
Noesis.UIRenderer.Noesis_UpdateRenderer (Int32 rendererId, Double timeInSeconds) (at Assets/Plugins/NoesisGUI/Scripts/Core/NoesisUIRendererImports.cs:145)
Noesis.UIRenderer.Update (Double timeInSeconds, AntialiasingMode aaMode, TessellationMode tessMode, TessellationQuality tessQuality, RendererFlags flags, Boolean enableMouse, Boolean enableTouch, Boolean enablePostProcess, Boolean flipVertically) (at Assets/Plugins/NoesisGUI/Scripts/Core/NoesisUIRenderer.cs:129)
NoesisGUIPanel.LateUpdate () (at Assets/Plugins/NoesisGUI/Scripts/NoesisGUIPanel.cs:203)
What gives? Why is Loaded firing more than once if I call UpdateLayout on its children? And why aren't the correct sizes being reported back without it? I thought the loaded event would fire after it's loaded.