forked from UnityCommunity/UnityLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetBoxColliderToUI.cs
More file actions
32 lines (24 loc) · 863 Bytes
/
SetBoxColliderToUI.cs
File metadata and controls
32 lines (24 loc) · 863 Bytes
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
// Tries to move BoxCollider(3D)-component to match UI panel/image position, by adjusting collider pivot value
using UnityEngine;
using UnityEditor;
namespace UnityLibrary
{
public class SetBoxColliderToUI : MonoBehaviour
{
[MenuItem("CONTEXT/BoxCollider/Match Position to UI")]
static void FixPosition(MenuCommand command)
{
BoxCollider b = (BoxCollider)command.context;
// record undo
Undo.RecordObject(b.transform, "Set Box Collider To UI");
// fix pos from Pivot
var r = b.gameObject.GetComponent<RectTransform>();
if (r == null) return;
//Debug.Log("pivot "+r.pivot);
var center = b.center;
center.x = 0.5f - r.pivot.x;
center.y = 0.5f - r.pivot.y;
b.center = center;
}
}
}