Skip to content

Commit 936e19d

Browse files
authored
add linerenderer context menu
convert linerenderer points between local to worldspace
1 parent e08b7a9 commit 936e19d

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// converts line renderer points from world space to local space
2+
3+
using UnityEngine;
4+
using UnityEditor;
5+
using UnityEditor.SceneManagement;
6+
7+
namespace UnityLibrary.ContextMenu
8+
{
9+
public static class LineRendererToLocalSpace
10+
{
11+
private const string MenuPath = "CONTEXT/LineRenderer/Convert Points To Local Space";
12+
13+
[MenuItem(MenuPath, true)]
14+
private static bool Validate(MenuCommand command)
15+
{
16+
return command != null && command.context is LineRenderer;
17+
}
18+
19+
[MenuItem(MenuPath)]
20+
private static void Convert(MenuCommand command)
21+
{
22+
var lr = (LineRenderer)command.context;
23+
if (lr == null) return;
24+
25+
int count = lr.positionCount;
26+
if (count == 0) return;
27+
28+
Transform t = lr.transform;
29+
30+
Undo.RecordObject(lr, "Convert LineRenderer To Local Space");
31+
32+
// Get current positions in world space no matter what mode it's in.
33+
Vector3[] world = new Vector3[count];
34+
if (lr.useWorldSpace)
35+
{
36+
lr.GetPositions(world);
37+
}
38+
else
39+
{
40+
Vector3[] local = new Vector3[count];
41+
lr.GetPositions(local);
42+
for (int i = 0; i < count; i++)
43+
world[i] = t.TransformPoint(local[i]);
44+
}
45+
46+
// Convert world -> local, switch mode, write back.
47+
Vector3[] newLocal = new Vector3[count];
48+
for (int i = 0; i < count; i++)
49+
newLocal[i] = t.InverseTransformPoint(world[i]);
50+
51+
lr.useWorldSpace = false;
52+
lr.SetPositions(newLocal);
53+
54+
EditorUtility.SetDirty(lr);
55+
56+
if (!Application.isPlaying)
57+
EditorSceneManager.MarkSceneDirty(lr.gameObject.scene);
58+
}
59+
}
60+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// converts LineRenderer points from local space to world space via context menu in Unity Editor
2+
3+
using UnityEngine;
4+
using UnityEditor;
5+
using UnityEditor.SceneManagement;
6+
7+
namespace UnityLibrary.ContextMenu
8+
{
9+
public static class LineRendererToWorldSpace
10+
{
11+
private const string MenuPath = "CONTEXT/LineRenderer/Convert Points To World Space";
12+
13+
[MenuItem(MenuPath, true)]
14+
private static bool Validate(MenuCommand command)
15+
{
16+
return command != null && command.context is LineRenderer;
17+
}
18+
19+
[MenuItem(MenuPath)]
20+
private static void Convert(MenuCommand command)
21+
{
22+
var lr = (LineRenderer)command.context;
23+
if (lr == null) return;
24+
25+
if (lr.useWorldSpace)
26+
{
27+
Debug.Log("LineRenderer is already using World Space.");
28+
return;
29+
}
30+
31+
int count = lr.positionCount;
32+
if (count == 0) return;
33+
34+
Transform t = lr.transform;
35+
36+
Undo.RecordObject(lr, "Convert LineRenderer To World Space");
37+
38+
Vector3[] local = new Vector3[count];
39+
lr.GetPositions(local);
40+
41+
Vector3[] world = new Vector3[count];
42+
for (int i = 0; i < count; i++)
43+
world[i] = t.TransformPoint(local[i]);
44+
45+
lr.useWorldSpace = true;
46+
lr.SetPositions(world);
47+
48+
EditorUtility.SetDirty(lr);
49+
50+
if (!Application.isPlaying)
51+
EditorSceneManager.MarkSceneDirty(lr.gameObject.scene);
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)