-
Notifications
You must be signed in to change notification settings - Fork 450
/
Copy pathCreateOutlineForPanelEditor.cs
59 lines (50 loc) · 1.92 KB
/
CreateOutlineForPanelEditor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Custom Contect menu for UI Image component (in WorldSpace UI canvas)
// used for creating outline for panel (image) using LineRenderer
using UnityEngine;
using UnityEditor;
using UnityEngine.UI;
namespace UnityLibrary
{
public class CreateOutlineForPanelEditor : MonoBehaviour
{
[MenuItem("CONTEXT/Image/Create Outline For Panel")]
static void DoubleMass(MenuCommand command)
{
// get reference
Image comp = (Image)command.context;
if (comp == null)
{
Debug.LogError("No Image component found..");
return;
}
// TODO check that its worlspace canvas
// get worldspace borders, FIXME get root canvas, instead of parent
var canvasRect = comp.transform.parent.GetComponent<Canvas>().GetComponent<RectTransform>();
Vector3[] corners = new Vector3[4];
canvasRect.GetWorldCorners(corners);
var line = comp.transform.GetComponent<LineRenderer>();
if (line == null)
{
Debug.LogError("Missing LineRenderer component");
return;
}
if (line.useWorldSpace == true)
{
Debug.LogWarning("LineRenderer has worlspace enabled, disabling it");
line.useWorldSpace = false;
}
// set line points
line.positionCount = corners.Length + 1;
line.SetPositions(corners);
// connect last and first
line.SetPosition(line.positionCount - 1, corners[0]);
// convert worldspace to localspace
for (int i = 0, len = line.positionCount; i < len; i++)
{
var worldPos = line.GetPosition(i);
var localPos = canvasRect.transform.InverseTransformPoint(worldPos);
line.SetPosition(i, localPos);
}
}
}
}