using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class macOsDocker : MonoBehaviour,IPointerMoveHandler,IPointerExitHandler
{
public Transform[] listItems;
public Button[] listBtns;
[Header("半轴的长度(世界坐标)")]
public float halfRange;
public float yMaxDis;
public float maxAddScale;
private void Reset()
{
listItems = new Transform[transform.childCount];
for (int i = 0; i < transform.childCount; i++)
{
listItems[i] = transform.GetChild(i);
}
listBtns = GetComponentsInChildren<Button>();
}
public void OnPointerExit(PointerEventData eventData)
{
for (int i = 0; i < listItems.Length; i++)
{
listItems[i].transform.localScale = Vector3.one;
Button btn = listBtns[i];
RectTransform btnRect = btn.transform as RectTransform;
btnRect.anchoredPosition = new Vector3(btnRect.anchoredPosition.x, 0, 0);
}
}
public void OnPointerMove(PointerEventData eventData)
{
float wPointx = eventData.pointerCurrentRaycast.worldPosition.x;
float leftX = wPointx - halfRange;
float rightX = wPointx + halfRange;
for (int i = 0; i < listItems.Length; i++)
{
var tran = listItems[i];
Button btn = listBtns[i];
if (tran.position.x<leftX || tran.position.x>rightX)
{
tran.localScale = Vector3.one;
RectTransform btnRect = btn.transform as RectTransform;
btnRect.anchoredPosition = new Vector3(btnRect.anchoredPosition.x,0,0);
}
else
{
float rate = (tran.position.x - leftX) / (halfRange * 2);
float sinValue = Mathf.Sin(rate*Mathf.PI);
tran.localScale = Vector3.one * (1 + sinValue * maxAddScale);
RectTransform btnRect = btn.transform as RectTransform;
btnRect.anchoredPosition = new Vector3(btnRect.anchoredPosition.x, sinValue*yMaxDis, 0);
}
}
RectTransform rect = transform as RectTransform;
LayoutRebuilder.ForceRebuildLayoutImmediate(rect);
}
}
直接使用horizontal layout 使用子级缩放,放大item之后还能保持间距,在使用layout的情况下控制item的y位置为此需要新建一个空节点然后就可以控制孩子内容的局部坐标
预览视频:https://www.bilibili.com/video/BV1Jov5e9EPt/?spm_id_from=333.999.0.0