Skip to content

Commit 2b93b85

Browse files
committed
Fixed position cast in canvas space not compatible with resizable screen issue.
1 parent 0e68c29 commit 2b93b85

File tree

1 file changed

+60
-8
lines changed

1 file changed

+60
-8
lines changed

Assets/JCSUnity/Scripts/Actions/JCS_PositionCastAction.cs

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,31 @@ public class JCS_PositionCastAction
3737
[SerializeField]
3838
private bool mTestWithKey = false;
3939

40-
[Tooltip("Check to see the current casting position.")]
41-
[SerializeField]
42-
private Vector3 mCastPosition = Vector3.zero;
43-
4440
[Tooltip("Key to test if cast to screen space.")]
4541
[SerializeField]
4642
private KeyCode mCastToScreenKey = KeyCode.S;
4743

44+
[Tooltip("Test position to cast to screen space.")]
45+
[SerializeField]
46+
private Vector3 mCastToScreenPosition = Vector3.zero;
47+
4848
[Tooltip("Key to test if cast to world soace.")]
4949
[SerializeField]
5050
private KeyCode mCastToWorldKey = KeyCode.W;
51+
52+
[Tooltip("Test position to cast to world space.")]
53+
[SerializeField]
54+
private Vector3 mCastToWorldPosition = Vector3.zero;
5155
#endif
5256

57+
58+
[Header("** Check Variables (JCS_PositionCastAction) **")]
59+
60+
[Tooltip("")]
61+
[SerializeField]
62+
private JCS_PanelRoot mPanelRoot = null;
63+
64+
5365
[Header("** Runtime Variables (JCS_PositionCastAction) **")]
5466

5567
[Tooltip("")]
@@ -64,23 +76,36 @@ public class JCS_PositionCastAction
6476
// setter / getter
6577
//------------------------------
6678
#if (UNITY_EDITOR)
67-
public Vector3 CastPosition { get { return this.mCastPosition; } }
79+
public Vector3 CastToScreenPosition { get { return this.mCastToScreenPosition; } }
80+
public Vector3 CastToWorldPosition { get { return this.mCastToWorldPosition; } }
6881
#endif
6982
public Vector3 PositionOffset { get { return this.mPositionOffset; } set { this.mPositionOffset = value; } }
7083

7184
//========================================
7285
// Unity's function
7386
//------------------------------
87+
private void Start()
88+
{
89+
// Only need it for the UI.
90+
if (GetObjectType() == JCS_UnityObjectType.UI ||
91+
GetObjectType() == JCS_UnityObjectType.TEXT)
92+
{
93+
// Get panel root, in order to calculate the
94+
// correct distance base on the resolution.
95+
mPanelRoot = this.GetComponentInParent<JCS_PanelRoot>();
96+
}
97+
}
98+
7499
#if (UNITY_EDITOR)
75100
private void Update()
76101
{
77102
if (!mTestWithKey)
78103
return;
79104

80105
if (JCS_Input.GetKeyDown(mCastToScreenKey))
81-
mCastPosition = CastToScreen(mCastPosition);
106+
CastToScreen(mCastToScreenPosition);
82107
if (JCS_Input.GetKeyDown(mCastToWorldKey))
83-
mCastPosition = CastToWorld(mCastPosition);
108+
CastToWorld(mCastToWorldPosition);
84109
}
85110
#endif
86111

@@ -91,20 +116,44 @@ private void Update()
91116
// Public Functions
92117

93118
/// <summary>
119+
/// Make a canvas space object to a world space's position.
94120
///
121+
/// NOTE(jenchieh): Make UI object (canvas space) on top of the
122+
/// world space game object.
95123
/// </summary>
96124
/// <param name="pos"></param>
97125
/// <returns></returns>
98126
public Vector3 CastToScreen(Vector3 pos)
99127
{
100128
JCS_Camera jcsCam = JCS_Camera.main;
129+
JCS_ResizeUI resizeUI = JCS_ResizeUI.instance;
130+
131+
132+
Vector3 positionOffset = mPositionOffset;
133+
134+
if (mPanelRoot != null)
135+
{
136+
positionOffset.x /= mPanelRoot.PanelDeltaWidthRatio;
137+
positionOffset.y /= mPanelRoot.PanelDeltaHeightRatio;
138+
}
139+
101140

102141
switch (GetObjectType())
103142
{
104143
case JCS_UnityObjectType.TEXT:
105144
case JCS_UnityObjectType.UI:
106145
{
107-
this.LocalPosition = jcsCam.WorldToCanvasSpace(pos) + (Vector2)mPositionOffset;
146+
Vector2 worldToCanvasSpace = jcsCam.WorldToCanvasSpace(pos);
147+
148+
float targetScale = resizeUI.TargetScale;
149+
150+
if (targetScale != 0.0f)
151+
{
152+
worldToCanvasSpace.x /= resizeUI.TargetScale;
153+
worldToCanvasSpace.y /= resizeUI.TargetScale;
154+
}
155+
156+
this.LocalPosition = worldToCanvasSpace + (Vector2)positionOffset;
108157
}
109158
break;
110159
}
@@ -113,7 +162,10 @@ public Vector3 CastToScreen(Vector3 pos)
113162
}
114163

115164
/// <summary>
165+
/// Make a 3D game object to canvas space's position.
116166
///
167+
/// NOTE(jenchieh): Make world space game object on top of
168+
/// the UI object (canvas space).
117169
/// </summary>
118170
/// <param name="pos"></param>
119171
/// <returns></returns>

0 commit comments

Comments
 (0)