前几天帮一个学生实现的照片墙的效果,分享给大家~~
效果:
脚本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
using DG.Tweening; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; public class MyTest : MonoBehaviour { public RectTransform prefab; int row = 10; // 行 int column = 20; // 列 int startXPos = 60; int startZPos = -100; float distanceRandomMinX = 55; float distanceRandomMaxX = 65; float distanceRandomMinY = 60; float distanceRandomMaxY = 60; float initMoveDistance = 1200; float enlargeSize = 5; float radiateSize = 220; List<List<RectTransform>> goList; Dictionary<RectTransform, Vector2> itemPosDict; List<RectTransform> changedItemList; // Use this for initialization void Start () { goList = new List<List<RectTransform>>(); itemPosDict = new Dictionary<RectTransform, Vector2>(); changedItemList = new List<RectTransform>(); CreateGos(); } void CreateGos() { // 生成所有物体,并添加到字典 for (int i = 0; i < row; i++) { List<RectTransform> gos = new List<RectTransform>(); goList.Add(gos); float lastPosX = 0; for (int j = 0; j < column; j++) { RectTransform item = (Instantiate(prefab.gameObject) as GameObject).GetComponent<RectTransform>(); item.name = i + " " + j; item.transform.SetParent(transform); Vector2 startPos = new Vector3(Random.Range(distanceRandomMinX, distanceRandomMaxX) + lastPosX, startZPos - i * Random.Range(distanceRandomMinY, distanceRandomMaxY)); item.anchoredPosition = startPos; Vector2 endPos = new Vector3(startPos.x - initMoveDistance, startZPos - i * Random.Range(distanceRandomMinY, distanceRandomMaxY)); Tweener tweener = item.DOAnchorPosX(endPos.x, Random.Range(1.8f, 2f)); // 缓动到目标位置 tweener.SetDelay(j * 0.1f + (row - i) * 0.1f); // 延时 tweener.SetEase(Ease.InSine); // 缓动效果 item.gameObject.SetActive(true); gos.Add(item); itemPosDict.Add(item, endPos); lastPosX = item.anchoredPosition.x; } } } public void OnMousePointEnter(RectTransform item) { // 缓动改变中心物体尺寸 item.DOScale(enlargeSize, 0.5f); Vector2 pos = itemPosDict[item]; changedItemList = new List<RectTransform>(); // 添加扩散物体到集合 foreach (KeyValuePair<RectTransform, Vector2> i in itemPosDict) { if(Vector2.Distance(i.Value, pos) < radiateSize) { changedItemList.Add(i.Key); } } // 缓动来解决扩散物体的动画 for (int i = 0; i < changedItemList.Count; i++) { Vector2 targetPos = itemPosDict[item] + (itemPosDict[changedItemList[i]] - itemPosDict[item]).normalized * radiateSize; changedItemList[i].DOAnchorPos(targetPos, 0.8f); } } public void OnMousePointExit(RectTransform go) { // 缓动恢复中心物体尺寸 go.DOScale(1, 1); // 缓动将扩散物体恢复到初始位置 for (int i = 0; i < changedItemList.Count; i++) { changedItemList[i].DOAnchorPos(itemPosDict[changedItemList[i]], 0.8f); } } } |
GitHub下载地址:
https://github.com/654306663/PhotoWall
- 本文固定链接: http://www.u3d8.com/?p=1546
- 转载请注明: 网虫虫 在 u3d8.com 发表过