Skip to content

Commit 925a7d6

Browse files
author
setchi
committed
Added support for all types of RenderMode
1 parent 2765ce2 commit 925a7d6

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

Assets/Hypertext/Scripts/HypertextBase.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ public abstract class HypertextBase : Text, IPointerClickHandler
1010
const int CharVertsNum = 6;
1111
readonly List<ClickableEntry> _entries = new List<ClickableEntry>();
1212
static readonly ObjectPool<List<UIVertex>> _verticesPool = new ObjectPool<List<UIVertex>>(null, l => l.Clear());
13+
Canvas _rootCanvas;
14+
Canvas RootCanvas { get { return _rootCanvas ?? (_rootCanvas = GetComponentInParent<Canvas>()); } }
1315

1416
struct ClickableEntry
1517
{
@@ -198,16 +200,26 @@ Rect MergeRects(List<Rect> rects)
198200
return new Rect { min = min, max = max };
199201
}
200202

201-
// TODO: Canvas の Render Mode によって localPosition の算出方法を変える
202-
Vector3 ToLocalPosition(Vector3 position)
203+
Vector3 ToLocalPosition(Vector3 position, Camera camera)
203204
{
204-
var localPosition = transform.InverseTransformPoint(position);
205+
if (!RootCanvas)
206+
{
207+
return Vector3.zero;
208+
}
209+
210+
if (RootCanvas.renderMode == RenderMode.ScreenSpaceOverlay)
211+
{
212+
return transform.InverseTransformPoint(position);
213+
}
214+
215+
var localPosition = Vector2.zero;
216+
RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, position, camera, out localPosition);
205217
return localPosition;
206218
}
207219

208220
void IPointerClickHandler.OnPointerClick(PointerEventData eventData)
209221
{
210-
var localPosition = ToLocalPosition(eventData.position);
222+
var localPosition = ToLocalPosition(eventData.position, eventData.pressEventCamera);
211223

212224
foreach (var entry in _entries)
213225
{

0 commit comments

Comments
 (0)