得意黑字体安装
This commit is contained in:
1 parent
d0144205c4
commit
3ae43d1297
39 files changed
+8628
-817
No files matched your search
@@ -3,9 +3,8 @@ using UnityEngine.EventSystems;
|
||||
using UnityEngine.UI;
|
||||
|
||||
/// <summary>
|
||||
/// 屏幕虚拟摇杆(移动轮盘)。挂在预制体根节点上,子物体约定按名字查找:
|
||||
/// Background —— 底盘(RectTransform + Image)
|
||||
/// Handle —— 摇杆头(RectTransform + Image)
|
||||
/// 屏幕虚拟摇杆(移动轮盘)。挂在预制体根节点上,底盘和摇杆头两个引用
|
||||
/// (background / handle)在 Inspector / 预制体里接好,代码里不按名字查找节点。
|
||||
///
|
||||
/// 鼠标按住拖拽或手指触摸都可以操作,对外输出归一化方向 <see cref="Value"/>:
|
||||
/// x = 左右(右为正),y = 前后(上为正),模长 0~1 表示推杆幅度。
|
||||
@@ -14,8 +13,11 @@ using UnityEngine.UI;
|
||||
[DisallowMultipleComponent]
|
||||
public class VirtualJoystick : MonoBehaviour, IPointerDownHandler, IDragHandler, IPointerUpHandler
|
||||
{
|
||||
[Header("引用(留空则按子物体名自动查找)")]
|
||||
[Header("引用(必须在 Inspector / 预制体里指定)")]
|
||||
[Tooltip("摇杆底盘(RectTransform)。拖拽位置以它的轴心为圆心换算,必须是捕获点击的那个节点")]
|
||||
[SerializeField] private RectTransform background;
|
||||
|
||||
[Tooltip("摇杆头(RectTransform),跟着拖拽移动")]
|
||||
[SerializeField] private RectTransform handle;
|
||||
|
||||
[Header("手感(距离单位是 UI 像素,以底盘轴心为圆心)")]
|
||||
@@ -45,12 +47,9 @@ public class VirtualJoystick : MonoBehaviour, IPointerDownHandler, IDragHandler,
|
||||
private void Awake()
|
||||
{
|
||||
if (background == null)
|
||||
background = FindChildRect("Background");
|
||||
// 没有名为 Background 的子物体时,就把挂脚本的这个节点本身当作底盘
|
||||
if (background == null)
|
||||
background = transform as RectTransform;
|
||||
Debug.LogError("[VirtualJoystick] 没有指定摇杆底盘(background),请在 Inspector / 预制体里拖入。", this);
|
||||
if (handle == null)
|
||||
handle = FindChildRect("Handle");
|
||||
Debug.LogError("[VirtualJoystick] 没有指定摇杆头(handle),请在 Inspector / 预制体里拖入。", this);
|
||||
|
||||
_canvas = GetComponentInParent<Canvas>();
|
||||
if (handle != null)
|
||||
@@ -125,28 +124,4 @@ public class VirtualJoystick : MonoBehaviour, IPointerDownHandler, IDragHandler,
|
||||
if (handle != null)
|
||||
handle.anchoredPosition = _handleHomePosition + direction * (magnitude * radius);
|
||||
}
|
||||
|
||||
private RectTransform FindChildRect(string childName)
|
||||
{
|
||||
Transform child = transform.Find(childName);
|
||||
if (child == null)
|
||||
{
|
||||
// 允许更深层级,比如 Root/Art/Background
|
||||
foreach (RectTransform rt in GetComponentsInChildren<RectTransform>(true))
|
||||
{
|
||||
if (rt != null && rt != transform && rt.name == childName)
|
||||
return rt;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
return child as RectTransform;
|
||||
}
|
||||
|
||||
private void OnValidate()
|
||||
{
|
||||
if (handle == null)
|
||||
handle = FindChildRect("Handle");
|
||||
if (background == null)
|
||||
background = FindChildRect("Background") ?? transform as RectTransform;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user