Skip to content

Commit

Permalink
- To listen for pointer events, NonDrawingGraphic is now used instead…
Browse files Browse the repository at this point in the history
… of an Image (results in slightly improved graphics performance)

- Fixed #3
  • Loading branch information
yasirkula committed Nov 2, 2019
1 parent e81a2f3 commit 0f9252e
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 35 deletions.
Binary file removed DynamicPanels.unitypackage
Binary file not shown.
7 changes: 2 additions & 5 deletions Plugins/DynamicPanels/Scripts/Anchoring/AnchorZoneBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public abstract class AnchorZoneBase : MonoBehaviour, IPointerEnterHandler, IPoi

public RectTransform RectTransform { get; private set; }

private Image raycastZone;
private Graphic raycastZone;

private int hoveredPointerId = PanelManager.NON_EXISTING_TOUCH;

Expand All @@ -21,9 +21,7 @@ public abstract class AnchorZoneBase : MonoBehaviour, IPointerEnterHandler, IPoi
protected void Awake()
{
RectTransform = (RectTransform) transform;

raycastZone = gameObject.AddComponent<Image>();
raycastZone.color = Color.clear;
raycastZone = gameObject.AddComponent<NonDrawingGraphic>();
}

protected void OnEnable()
Expand All @@ -37,7 +35,6 @@ protected void OnEnable()
public void Initialize( Panel panel )
{
m_panel = panel;
raycastZone.sprite = panel.Internal.BackgroundSprite;
}

public void SetActive( bool value )
Expand Down
24 changes: 11 additions & 13 deletions Plugins/DynamicPanels/Scripts/DynamicPanelsCanvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ public InternalSettings( DynamicPanelsCanvas canvas )
this.canvas = canvas;

#if UNITY_EDITOR
if( canvas.UnityCanvas == null ) // is null while inspecting this component in edit mode
if( !canvas.UnityCanvas ) // is null while inspecting this component in edit mode
return;
#endif

if( canvas.UnityCanvas.renderMode == RenderMode.ScreenSpaceOverlay ||
( canvas.UnityCanvas.renderMode == RenderMode.ScreenSpaceCamera && canvas.UnityCanvas.worldCamera == null ) )
( canvas.UnityCanvas.renderMode == RenderMode.ScreenSpaceCamera && !canvas.UnityCanvas.worldCamera ) )
worldCamera = null;
else
worldCamera = canvas.UnityCanvas.worldCamera ?? Camera.main;
worldCamera = canvas.UnityCanvas.worldCamera ? canvas.UnityCanvas.worldCamera : Camera.main;
}

public Panel DummyPanel { get { return canvas.dummyPanel; } }
Expand Down Expand Up @@ -140,7 +140,7 @@ public string ID
public Vector2 Size { get; private set; }

private Panel dummyPanel;
private Image background;
private Graphic background;

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

UnanchoredPanelGroup = new UnanchoredPanelGroup( this );
RectTransform.pivot = new Vector2( 0.5f, 0.5f );
RectTransform.ChangePivotWithoutAffectingPosition( new Vector2( 0.5f, 0.5f ) );

if( GetComponent<RectMask2D>() == null )
if( !GetComponent<RectMask2D>() )
gameObject.AddComponent<RectMask2D>();

Size = RectTransform.rect.size;

InitializeRootGroup();
InitializeAnchorZones();

background = GetComponent<Image>();
if( background == null )
background = GetComponent<Graphic>();
if( !background )
{
background = gameObject.AddComponent<Image>();
background.sprite = dummyPanel.Internal.BackgroundSprite;
background.color = Color.clear;
background = gameObject.AddComponent<NonDrawingGraphic>();
background.raycastTarget = false;
}

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

HashSet<Transform> createdTabs = new HashSet<Transform>(); // A set to prevent duplicate tabs or to prevent making canvas itself a panel
Transform tr = transform;
while( tr != null )
while( tr )
{
createdTabs.Add( tr );
tr = tr.parent;
Expand Down Expand Up @@ -384,7 +382,7 @@ private Panel CreateInitialPanel( PanelProperties properties, Panel anchor, Dire
for( int i = 0; i < properties.tabs.Count; i++ )
{
PanelTabProperties panelProps = properties.tabs[i];
if( panelProps.content != null && !panelProps.content.Equals( null ) )
if( panelProps.content )
{
if( createdTabs.Contains( panelProps.content ) )
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected override void UpdateLayout()
protected override void EnsureMinimumSizeOf( IPanelGroupElement element )
{
Panel panel = element as Panel;
if( panel == null || panel.Equals( null ) )
if( !panel )
return;

Vector2 position = panel.Position;
Expand Down
17 changes: 17 additions & 0 deletions Plugins/DynamicPanels/Scripts/Helpers/NonDrawingGraphic.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using UnityEngine.UI;

namespace DynamicPanels
{
// Credit: http://answers.unity.com/answers/1157876/view.html
public class NonDrawingGraphic : Graphic
{
public override void SetMaterialDirty() { return; }
public override void SetVerticesDirty() { return; }

protected override void OnPopulateMesh( VertexHelper vh )
{
vh.Clear();
return;
}
}
}
12 changes: 12 additions & 0 deletions Plugins/DynamicPanels/Scripts/Helpers/NonDrawingGraphic.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Plugins/DynamicPanels/Scripts/Helpers/PanelTab.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public Panel Detach()

private void SetActive( bool activeState )
{
if( Content == null || Content.Equals( null ) )
if( !Content )
m_panel.Internal.RemoveTab( m_panel.GetTabIndex( this ), true );
else
{
Expand All @@ -166,7 +166,7 @@ private void SetActive( bool activeState )

void IPointerClickHandler.OnPointerClick( PointerEventData eventData )
{
if( Content == null || Content.Equals( null ) )
if( !Content )
m_panel.Internal.RemoveTab( m_panel.GetTabIndex( this ), true );
else
m_panel.ActiveTab = m_panel.GetTabIndex( this );
Expand Down
37 changes: 34 additions & 3 deletions Plugins/DynamicPanels/Scripts/Helpers/PanelUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static Panel CreatePanel( RectTransform content, DynamicPanelsCanvas canv
if( canvas == null )
{
canvas = Object.FindObjectOfType<DynamicPanelsCanvas>();
if( canvas == null || canvas.Equals( null ) )
if( !canvas )
{
Debug.LogError( "Panels require a DynamicPanelsCanvas!" );
return null;
Expand Down Expand Up @@ -59,7 +59,7 @@ public static Panel CreatePanel( RectTransform content, DynamicPanelsCanvas canv

public static Panel CreatePanelFor( RectTransform content, DynamicPanelsCanvas canvas )
{
if( content == null || content.Equals( null ) )
if( !content )
{
Debug.LogError( "Content is null!" );
return null;
Expand All @@ -70,7 +70,7 @@ public static Panel CreatePanelFor( RectTransform content, DynamicPanelsCanvas c

public static PanelTab GetAssociatedTab( RectTransform content )
{
if( content == null || content.Equals( null ) )
if( !content )
{
Debug.LogError( "Content is null!" );
return null;
Expand All @@ -95,5 +95,36 @@ public static bool IsNull( this IPanelGroupElement element )
{
return element == null || element.Equals( null );
}

// Credit: https://github.com/Unity-Technologies/UnityCsReference/blob/4fc5eb0fb2c7f5fb09f990fc99f162c8d06d9570/Editor/Mono/Inspector/RectTransformEditor.cs#L1259
public static void ChangePivotWithoutAffectingPosition( this RectTransform rectTransform, Vector2 pivot )
{
if( rectTransform.pivot == pivot )
return;

Vector3 cornerBefore;
Vector3[] s_Corners = new Vector3[4];
rectTransform.GetWorldCorners( s_Corners );
if( rectTransform.parent )
cornerBefore = rectTransform.parent.InverseTransformPoint( s_Corners[0] );
else
cornerBefore = s_Corners[0];

rectTransform.pivot = pivot;

Vector3 cornerAfter;
rectTransform.GetWorldCorners( s_Corners );
if( rectTransform.parent )
cornerAfter = rectTransform.parent.InverseTransformPoint( s_Corners[0] );
else
cornerAfter = s_Corners[0];

Vector3 cornerOffset = cornerAfter - cornerBefore;
rectTransform.anchoredPosition -= (Vector2) cornerOffset;

Vector3 pos = rectTransform.transform.position;
pos.z -= cornerOffset.z;
rectTransform.transform.position = pos;
}
}
}
15 changes: 6 additions & 9 deletions Plugins/DynamicPanels/Scripts/Panel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void ValidateTabs()
{
for( int i = panel.tabs.Count - 1; i >= 0; i-- )
{
if( panel.tabs[i].Content == null || panel.tabs[i].Content.Equals( null ) || panel.tabs[i].Content.parent != panel.contentParent )
if( !panel.tabs[i].Content || panel.tabs[i].Content.parent != panel.contentParent )
RemoveTab( i, true );
}
}
Expand Down Expand Up @@ -289,7 +289,7 @@ public PanelTab this[int tabIndex]
}
set
{
if( value != null && !value.Equals( null ) )
if( value )
AddTab( value.Content, tabIndex );
}
}
Expand Down Expand Up @@ -382,7 +382,7 @@ private void OnApplicationQuit()

public PanelTab AddTab( RectTransform tabContent, int tabIndex = -1 )
{
if( tabContent == null || tabContent.Equals( null ) )
if( !tabContent )
return null;

// Reset active tab because otherwise, it acts buggy in rare cases
Expand All @@ -398,7 +398,7 @@ public PanelTab AddTab( RectTransform tabContent, int tabIndex = -1 )
if( thisTabIndex == -1 )
{
PanelTab tab = PanelUtils.GetAssociatedTab( tabContent );
if( tab == null )
if( !tab )
{
tab = (PanelTab) Instantiate( Resources.Load<PanelTab>( "DynamicPanelTab" ), tabsParent, false );
tabs.Insert( tabIndex, tab );
Expand Down Expand Up @@ -445,7 +445,7 @@ public PanelTab AddTab( RectTransform tabContent, int tabIndex = -1 )

public PanelTab AddTab( PanelTab tab, int tabIndex = -1 )
{
if( tab == null || tab.Equals( null ) )
if( !tab )
return null;

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

resizeZone = new GameObject( "ResizeZone" + direction, typeof( RectTransform ) ).AddComponent<PanelResizeHelper>();
resizeZone.RectTransform.SetParent( resizeZonesParent, false );

Image background = resizeZone.gameObject.AddComponent<Image>();
background.color = Color.clear;
background.sprite = Internal.BackgroundSprite;
resizeZone.gameObject.AddComponent<NonDrawingGraphic>();

resizeZones[(int) direction] = resizeZone;

Expand Down
2 changes: 1 addition & 1 deletion Plugins/DynamicPanels/Scripts/PanelManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ public void OnPointerEnteredCanvas( DynamicPanelsCanvas canvas, PointerEventData
{
previewPanelCanvas = canvas;

if( hoveredAnchorZone != null && !hoveredAnchorZone.Equals( null ) && hoveredAnchorZone.Panel.Canvas != canvas )
if( hoveredAnchorZone && hoveredAnchorZone.Panel.Canvas != canvas )
hoveredAnchorZone.OnPointerExit( pointer );

previewPanel.SetParent( canvas.RectTransform, false );
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ This asset helps you create dynamic panels using Unity's UI system. These panels

## HOW TO

First, import **DynamicPanels.unitypackage** to your project. Afterwards, add **Dynamic Panels Canvas** component to the *RectTransform* that you want to move your panels inside. This RectTransform doesn't have to be the Canvas object. It can be any child of it and can be of any custom size.
First, import [DynamicPanels.unitypackage](https://github.com/yasirkula/UnityDynamicPanels/releases) to your project. Afterwards, add **Dynamic Panels Canvas** component to the *RectTransform* that you want to move your panels inside. This RectTransform doesn't have to be the Canvas object. It can be any child of it and can be of any custom size.

There are two ways to create panels: by using the GUI of Dynamic Panels Canvas or via Scripting API. There are also two types of panels: *free panels* that can be moved around and resized freely and *docked panels* that are moved by the layout system, depending on where it is docked to. A panel can have multiple tabs.

Expand Down

0 comments on commit 0f9252e

Please sign in to comment.