2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Joystick Pack(Unity Asset)で1回目だけコントローラーが表示されないときの対応方法

Posted at

Joystick Pack を Floating で利用すると、1回目の操作のときだけタップしたところにコントローラーが表示されないバグがありました。その修正方法です。

対応方法

Joystock.cs の ScreenPointToAnchorePosition メソッドに以下3-6行目のコードを追加します。

    protected Vector2 ScreenPointToAnchoredPosition(Vector2 screenPosition)
    {
3        if (cam == null && canvas.renderMode == RenderMode.ScreenSpaceCamera)
4        {
5            cam = canvas.worldCamera;
6        }
        Vector2 localPoint = Vector2.zero;
        if (RectTransformUtility.ScreenPointToLocalPointInRectangle(baseRect, screenPosition, cam, out localPoint))
        {
            Vector2 pivotOffset = baseRect.pivot * baseRect.sizeDelta;
            return localPoint - (background.anchorMax * baseRect.sizeDelta) + pivotOffset;
        }
        return Vector2.zero;
    }

補足

上記対応しないと cam に canvas.worldCamera が設定されないまま位置計算されて、canvas の scale が考慮されていないに位置に Joystick が表示されます。2回目以降は、FlotingJoystick.cs の OnPointerDown -> base.OnPointerDown(eventData) -> Joystick.cs の OnDrag の中で cam が設定されるので問題にならず、タップした位置にコントローラーが表示されます。

所感

こういう特定の条件下において1回目以降は正常に動くパターンみたいなのはAssetとしても対応が難しいものだなと思いまました。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?