Skip to content

Commit 0f9252e

Browse files
committed
- To listen for pointer events, NonDrawingGraphic is now used instead of an Image (results in slightly improved graphics performance)
- Fixed #3
1 parent e81a2f3 commit 0f9252e

File tree

11 files changed

+87
-35
lines changed

11 files changed

+87
-35
lines changed

DynamicPanels.unitypackage

-80.7 KB
Binary file not shown.

Plugins/DynamicPanels/Scripts/Anchoring/AnchorZoneBase.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public abstract class AnchorZoneBase : MonoBehaviour, IPointerEnterHandler, IPoi
1212

1313
public RectTransform RectTransform { get; private set; }
1414

15-
private Image raycastZone;
15+
private Graphic raycastZone;
1616

1717
private int hoveredPointerId = PanelManager.NON_EXISTING_TOUCH;
1818

@@ -21,9 +21,7 @@ public abstract class AnchorZoneBase : MonoBehaviour, IPointerEnterHandler, IPoi
2121
protected void Awake()
2222
{
2323
RectTransform = (RectTransform) transform;
24-
25-
raycastZone = gameObject.AddComponent<Image>();
26-
raycastZone.color = Color.clear;
24+
raycastZone = gameObject.AddComponent<NonDrawingGraphic>();
2725
}
2826

2927
protected void OnEnable()
@@ -37,7 +35,6 @@ protected void OnEnable()
3735
public void Initialize( Panel panel )
3836
{
3937
m_panel = panel;
40-
raycastZone.sprite = panel.Internal.BackgroundSprite;
4138
}
4239

4340
public void SetActive( bool value )

Plugins/DynamicPanels/Scripts/DynamicPanelsCanvas.cs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ public InternalSettings( DynamicPanelsCanvas canvas )
1818
this.canvas = canvas;
1919

2020
#if UNITY_EDITOR
21-
if( canvas.UnityCanvas == null ) // is null while inspecting this component in edit mode
21+
if( !canvas.UnityCanvas ) // is null while inspecting this component in edit mode
2222
return;
2323
#endif
2424

2525
if( canvas.UnityCanvas.renderMode == RenderMode.ScreenSpaceOverlay ||
26-
( canvas.UnityCanvas.renderMode == RenderMode.ScreenSpaceCamera && canvas.UnityCanvas.worldCamera == null ) )
26+
( canvas.UnityCanvas.renderMode == RenderMode.ScreenSpaceCamera && !canvas.UnityCanvas.worldCamera ) )
2727
worldCamera = null;
2828
else
29-
worldCamera = canvas.UnityCanvas.worldCamera ?? Camera.main;
29+
worldCamera = canvas.UnityCanvas.worldCamera ? canvas.UnityCanvas.worldCamera : Camera.main;
3030
}
3131

3232
public Panel DummyPanel { get { return canvas.dummyPanel; } }
@@ -140,7 +140,7 @@ public string ID
140140
public Vector2 Size { get; private set; }
141141

142142
private Panel dummyPanel;
143-
private Image background;
143+
private Graphic background;
144144

145145
private RectTransform anchorZonesParent;
146146
private readonly CanvasAnchorZone[] anchorZones = new CanvasAnchorZone[4]; // one for each side
@@ -208,22 +208,20 @@ private void Awake()
208208
#endif
209209

210210
UnanchoredPanelGroup = new UnanchoredPanelGroup( this );
211-
RectTransform.pivot = new Vector2( 0.5f, 0.5f );
211+
RectTransform.ChangePivotWithoutAffectingPosition( new Vector2( 0.5f, 0.5f ) );
212212

213-
if( GetComponent<RectMask2D>() == null )
213+
if( !GetComponent<RectMask2D>() )
214214
gameObject.AddComponent<RectMask2D>();
215215

216216
Size = RectTransform.rect.size;
217217

218218
InitializeRootGroup();
219219
InitializeAnchorZones();
220220

221-
background = GetComponent<Image>();
222-
if( background == null )
221+
background = GetComponent<Graphic>();
222+
if( !background )
223223
{
224-
background = gameObject.AddComponent<Image>();
225-
background.sprite = dummyPanel.Internal.BackgroundSprite;
226-
background.color = Color.clear;
224+
background = gameObject.AddComponent<NonDrawingGraphic>();
227225
background.raycastTarget = false;
228226
}
229227

@@ -236,7 +234,7 @@ private void Start()
236234

237235
HashSet<Transform> createdTabs = new HashSet<Transform>(); // A set to prevent duplicate tabs or to prevent making canvas itself a panel
238236
Transform tr = transform;
239-
while( tr != null )
237+
while( tr )
240238
{
241239
createdTabs.Add( tr );
242240
tr = tr.parent;
@@ -384,7 +382,7 @@ private Panel CreateInitialPanel( PanelProperties properties, Panel anchor, Dire
384382
for( int i = 0; i < properties.tabs.Count; i++ )
385383
{
386384
PanelTabProperties panelProps = properties.tabs[i];
387-
if( panelProps.content != null && !panelProps.content.Equals( null ) )
385+
if( panelProps.content )
388386
{
389387
if( createdTabs.Contains( panelProps.content ) )
390388
continue;

Plugins/DynamicPanels/Scripts/Grouping/UnanchoredPanelGroup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ protected override void UpdateLayout()
4545
protected override void EnsureMinimumSizeOf( IPanelGroupElement element )
4646
{
4747
Panel panel = element as Panel;
48-
if( panel == null || panel.Equals( null ) )
48+
if( !panel )
4949
return;
5050

5151
Vector2 position = panel.Position;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using UnityEngine.UI;
2+
3+
namespace DynamicPanels
4+
{
5+
// Credit: http://answers.unity.com/answers/1157876/view.html
6+
public class NonDrawingGraphic : Graphic
7+
{
8+
public override void SetMaterialDirty() { return; }
9+
public override void SetVerticesDirty() { return; }
10+
11+
protected override void OnPopulateMesh( VertexHelper vh )
12+
{
13+
vh.Clear();
14+
return;
15+
}
16+
}
17+
}

Plugins/DynamicPanels/Scripts/Helpers/NonDrawingGraphic.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Plugins/DynamicPanels/Scripts/Helpers/PanelTab.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public Panel Detach()
151151

152152
private void SetActive( bool activeState )
153153
{
154-
if( Content == null || Content.Equals( null ) )
154+
if( !Content )
155155
m_panel.Internal.RemoveTab( m_panel.GetTabIndex( this ), true );
156156
else
157157
{
@@ -166,7 +166,7 @@ private void SetActive( bool activeState )
166166

167167
void IPointerClickHandler.OnPointerClick( PointerEventData eventData )
168168
{
169-
if( Content == null || Content.Equals( null ) )
169+
if( !Content )
170170
m_panel.Internal.RemoveTab( m_panel.GetTabIndex( this ), true );
171171
else
172172
m_panel.ActiveTab = m_panel.GetTabIndex( this );

Plugins/DynamicPanels/Scripts/Helpers/PanelUtils.cs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public static Panel CreatePanel( RectTransform content, DynamicPanelsCanvas canv
1717
if( canvas == null )
1818
{
1919
canvas = Object.FindObjectOfType<DynamicPanelsCanvas>();
20-
if( canvas == null || canvas.Equals( null ) )
20+
if( !canvas )
2121
{
2222
Debug.LogError( "Panels require a DynamicPanelsCanvas!" );
2323
return null;
@@ -59,7 +59,7 @@ public static Panel CreatePanel( RectTransform content, DynamicPanelsCanvas canv
5959

6060
public static Panel CreatePanelFor( RectTransform content, DynamicPanelsCanvas canvas )
6161
{
62-
if( content == null || content.Equals( null ) )
62+
if( !content )
6363
{
6464
Debug.LogError( "Content is null!" );
6565
return null;
@@ -70,7 +70,7 @@ public static Panel CreatePanelFor( RectTransform content, DynamicPanelsCanvas c
7070

7171
public static PanelTab GetAssociatedTab( RectTransform content )
7272
{
73-
if( content == null || content.Equals( null ) )
73+
if( !content )
7474
{
7575
Debug.LogError( "Content is null!" );
7676
return null;
@@ -95,5 +95,36 @@ public static bool IsNull( this IPanelGroupElement element )
9595
{
9696
return element == null || element.Equals( null );
9797
}
98+
99+
// Credit: https://github.com/Unity-Technologies/UnityCsReference/blob/4fc5eb0fb2c7f5fb09f990fc99f162c8d06d9570/Editor/Mono/Inspector/RectTransformEditor.cs#L1259
100+
public static void ChangePivotWithoutAffectingPosition( this RectTransform rectTransform, Vector2 pivot )
101+
{
102+
if( rectTransform.pivot == pivot )
103+
return;
104+
105+
Vector3 cornerBefore;
106+
Vector3[] s_Corners = new Vector3[4];
107+
rectTransform.GetWorldCorners( s_Corners );
108+
if( rectTransform.parent )
109+
cornerBefore = rectTransform.parent.InverseTransformPoint( s_Corners[0] );
110+
else
111+
cornerBefore = s_Corners[0];
112+
113+
rectTransform.pivot = pivot;
114+
115+
Vector3 cornerAfter;
116+
rectTransform.GetWorldCorners( s_Corners );
117+
if( rectTransform.parent )
118+
cornerAfter = rectTransform.parent.InverseTransformPoint( s_Corners[0] );
119+
else
120+
cornerAfter = s_Corners[0];
121+
122+
Vector3 cornerOffset = cornerAfter - cornerBefore;
123+
rectTransform.anchoredPosition -= (Vector2) cornerOffset;
124+
125+
Vector3 pos = rectTransform.transform.position;
126+
pos.z -= cornerOffset.z;
127+
rectTransform.transform.position = pos;
128+
}
98129
}
99130
}

Plugins/DynamicPanels/Scripts/Panel.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void ValidateTabs()
7676
{
7777
for( int i = panel.tabs.Count - 1; i >= 0; i-- )
7878
{
79-
if( panel.tabs[i].Content == null || panel.tabs[i].Content.Equals( null ) || panel.tabs[i].Content.parent != panel.contentParent )
79+
if( !panel.tabs[i].Content || panel.tabs[i].Content.parent != panel.contentParent )
8080
RemoveTab( i, true );
8181
}
8282
}
@@ -289,7 +289,7 @@ public PanelTab this[int tabIndex]
289289
}
290290
set
291291
{
292-
if( value != null && !value.Equals( null ) )
292+
if( value )
293293
AddTab( value.Content, tabIndex );
294294
}
295295
}
@@ -382,7 +382,7 @@ private void OnApplicationQuit()
382382

383383
public PanelTab AddTab( RectTransform tabContent, int tabIndex = -1 )
384384
{
385-
if( tabContent == null || tabContent.Equals( null ) )
385+
if( !tabContent )
386386
return null;
387387

388388
// Reset active tab because otherwise, it acts buggy in rare cases
@@ -398,7 +398,7 @@ public PanelTab AddTab( RectTransform tabContent, int tabIndex = -1 )
398398
if( thisTabIndex == -1 )
399399
{
400400
PanelTab tab = PanelUtils.GetAssociatedTab( tabContent );
401-
if( tab == null )
401+
if( !tab )
402402
{
403403
tab = (PanelTab) Instantiate( Resources.Load<PanelTab>( "DynamicPanelTab" ), tabsParent, false );
404404
tabs.Insert( tabIndex, tab );
@@ -445,7 +445,7 @@ public PanelTab AddTab( RectTransform tabContent, int tabIndex = -1 )
445445

446446
public PanelTab AddTab( PanelTab tab, int tabIndex = -1 )
447447
{
448-
if( tab == null || tab.Equals( null ) )
448+
if( !tab )
449449
return null;
450450

451451
return AddTab( tab.Content, tabIndex );
@@ -686,10 +686,7 @@ private PanelResizeHelper GetResizeZone( Direction direction )
686686

687687
resizeZone = new GameObject( "ResizeZone" + direction, typeof( RectTransform ) ).AddComponent<PanelResizeHelper>();
688688
resizeZone.RectTransform.SetParent( resizeZonesParent, false );
689-
690-
Image background = resizeZone.gameObject.AddComponent<Image>();
691-
background.color = Color.clear;
692-
background.sprite = Internal.BackgroundSprite;
689+
resizeZone.gameObject.AddComponent<NonDrawingGraphic>();
693690

694691
resizeZones[(int) direction] = resizeZone;
695692

Plugins/DynamicPanels/Scripts/PanelManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ public void OnPointerEnteredCanvas( DynamicPanelsCanvas canvas, PointerEventData
354354
{
355355
previewPanelCanvas = canvas;
356356

357-
if( hoveredAnchorZone != null && !hoveredAnchorZone.Equals( null ) && hoveredAnchorZone.Panel.Canvas != canvas )
357+
if( hoveredAnchorZone && hoveredAnchorZone.Panel.Canvas != canvas )
358358
hoveredAnchorZone.OnPointerExit( pointer );
359359

360360
previewPanel.SetParent( canvas.RectTransform, false );

0 commit comments

Comments
 (0)