Added new Extension method for discovering a parent canvas for a non-graphicpull/413/head
parent
11309d2d82
commit
ec165a57b9
|
@ -12,7 +12,7 @@ namespace UnityEngine.UI.Extensions
|
|||
{
|
||||
UILineRenderer curveRenderer = target as UILineRenderer;
|
||||
|
||||
if (!curveRenderer || curveRenderer.Points == null || curveRenderer.Points.Length < 2)
|
||||
if (!curveRenderer || curveRenderer.drivenExternally || curveRenderer.Points == null || curveRenderer.Points.Length < 2)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -64,6 +64,9 @@ namespace UnityEngine.UI.Extensions
|
|||
public BezierType BezierMode = BezierType.None;
|
||||
public int BezierSegmentsPerCurve = 10;
|
||||
|
||||
[HideInInspector]
|
||||
public bool drivenExternally = false;
|
||||
|
||||
/// <summary>
|
||||
/// UV rectangle used by the texture.
|
||||
/// </summary>
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
/// Credit Simon (darkside) Jackson
|
||||
/// Sourced from - My head
|
||||
namespace UnityEngine.UI.Extensions
|
||||
{
|
||||
public static class UIExtensionMethods
|
||||
{
|
||||
public static Canvas GetParentCanvas(this RectTransform rt)
|
||||
{
|
||||
RectTransform parent = rt;
|
||||
Canvas parentCanvas = rt.GetComponent<Canvas>();
|
||||
|
||||
int SearchIndex = 0;
|
||||
while (parentCanvas == null || SearchIndex > 50)
|
||||
{
|
||||
parentCanvas = rt.GetComponentInParent<Canvas>();
|
||||
if (parentCanvas == null)
|
||||
{
|
||||
parent = parent.parent.GetComponent<RectTransform>();
|
||||
SearchIndex++;
|
||||
}
|
||||
}
|
||||
return parentCanvas;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 08fd87e2db0639a4caf7a053f227a470
|
||||
timeCreated: 1498086098
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,65 @@
|
|||
/// Credit Alastair Aitchison
|
||||
/// Sourced from - https://bitbucket.org/UnityUIExtensions/unity-ui-extensions/issues/123/uilinerenderer-issues-with-specifying
|
||||
|
||||
namespace UnityEngine.UI.Extensions
|
||||
{
|
||||
[RequireComponent(typeof(UILineRenderer))]
|
||||
[ExecuteInEditMode]
|
||||
public class UILineConnector : MonoBehaviour
|
||||
{
|
||||
|
||||
// The elements between which line segments should be drawn
|
||||
public RectTransform[] transforms;
|
||||
private RectTransform canvas;
|
||||
private RectTransform rt;
|
||||
private UILineRenderer lr;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
canvas = GetComponentInParent<RectTransform>().GetParentCanvas().GetComponent<RectTransform>();
|
||||
rt = GetComponent<RectTransform>();
|
||||
lr = GetComponent<UILineRenderer>();
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
if (transforms == null || transforms.Length < 1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the pivot points
|
||||
Vector2 thisPivot = rt.pivot;
|
||||
Vector2 canvasPivot = canvas.pivot;
|
||||
|
||||
// Set up some arrays of coordinates in various reference systems
|
||||
Vector3[] worldSpaces = new Vector3[transforms.Length];
|
||||
Vector3[] canvasSpaces = new Vector3[transforms.Length];
|
||||
Vector2[] points = new Vector2[transforms.Length];
|
||||
|
||||
// First, convert the pivot to worldspace
|
||||
for (int i = 0; i < transforms.Length; i++)
|
||||
{
|
||||
worldSpaces[i] = transforms[i].TransformPoint(thisPivot);
|
||||
}
|
||||
|
||||
// Then, convert to canvas space
|
||||
for (int i = 0; i < transforms.Length; i++)
|
||||
{
|
||||
canvasSpaces[i] = canvas.InverseTransformPoint(worldSpaces[i]);
|
||||
}
|
||||
|
||||
// Calculate delta from the canvas pivot point
|
||||
for (int i = 0; i < transforms.Length; i++)
|
||||
{
|
||||
points[i] = new Vector2(canvasSpaces[i].x, canvasSpaces[i].y);
|
||||
}
|
||||
|
||||
// And assign the converted points to the line renderer
|
||||
lr.Points = points;
|
||||
lr.relativeSize = false;
|
||||
lr.drivenExternally = true;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 8b52ee3a4491f3742bb35fa3eec798d3
|
||||
timeCreated: 1498085244
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Loading…
Reference in New Issue