国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

Unity3D實(shí)現(xiàn)扭動(dòng)擠壓瀏覽效果

2020-01-24 00:09:07
字體:
供稿:網(wǎng)友

最近的項(xiàng)目中,想做到一種能夠吸引眼球的一種角色選擇瀏覽效果

Demo源碼:點(diǎn)擊打開鏈接

最終實(shí)現(xiàn)了下按如下圖這么一種瀏覽效果:

效果圖一

效果圖二

可能要實(shí)現(xiàn)這么一種效果用動(dòng)畫插件會(huì)很快,但總感覺有點(diǎn)大材小用
這里我向大家分享一個(gè)極簡方式來實(shí)現(xiàn)這么一種效果

目錄結(jié)構(gòu)如下

其中Items有4個(gè)Image子節(jié)點(diǎn)

在父節(jié)點(diǎn)Items下添加如下圖橫向布局組件

在其4個(gè)Image子節(jié)點(diǎn)下添加如下圖布局元素組件

完成這些步驟后接下來就是代碼實(shí)現(xiàn)了
在Items添加如下腳本組件

using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.UI; public class Items : MonoBehaviour{ public List<GameObject> items = new List<GameObject>();  //縮放時(shí)間 public float time = 1.3f;  //原先大小 public Vector2 oldSize;  //放大縮小速度 public float speed;  private void Start() { for (int i = 0; i < items.Count; i++) { EventTriggerListener.GetComponent(items[i]).onEnter = OnMouseEnter; EventTriggerListener.GetComponent(items[i]).onExit = OnMouseExit; } }   void OnMouseEnter(GameObject go) { EventTriggerListener.GetComponent(go).UpdateSize(oldSize * time, speed); }  void OnMouseExit(GameObject go) { EventTriggerListener.GetComponent(go).UpdateSize(oldSize, speed); }}

在其4個(gè)子節(jié)點(diǎn)下添加如下腳本組件

using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.EventSystems;using UnityEngine.UI; [RequireComponent(typeof(LayoutElement))]public class EventTriggerListener : EventTrigger{ public delegate void VoidDelegate(GameObject obj); //點(diǎn)擊 public VoidDelegate onClick; //鼠標(biāo)按下 public VoidDelegate onDown; //鼠標(biāo)抬起 public VoidDelegate onUp; //鼠標(biāo)移入 public VoidDelegate onEnter; //鼠標(biāo)移出 public VoidDelegate onExit;  private Vector2 currentSize; private Vector2 targetSize; private float speed = 4.0f;  public static EventTriggerListener GetComponent(GameObject obj) { EventTriggerListener listener = obj.GetComponent<EventTriggerListener>(); if (listener == null) { listener = obj.AddComponent<EventTriggerListener>(); }  return listener; }   public override void OnPointerClick(PointerEventData eventData) { if (onClick != null) { onClick(gameObject); } } public override void OnPointerDown(PointerEventData eventData) { if (onDown != null) onDown(gameObject); } public override void OnPointerUp(PointerEventData eventData) { if (onUp != null) onUp(gameObject); } public override void OnPointerEnter(PointerEventData eventData) { if (onEnter != null) onEnter(gameObject); } public override void OnPointerExit(PointerEventData eventData) { if (onExit != null) onExit(gameObject); }    private void Start() { targetSize = currentSize = new Vector2(this.GetComponent<LayoutElement>().preferredWidth, this.GetComponent<LayoutElement>().preferredHeight); }  private void Update() { if (currentSize != targetSize) { currentSize = Vector2.Lerp(currentSize, targetSize, Time.deltaTime * speed); if (Vector2.Distance(currentSize, targetSize) <= 0.01) { currentSize = targetSize; }  this.GetComponent<LayoutElement>().preferredWidth = currentSize.x; this.GetComponent<LayoutElement>().preferredHeight = currentSize.y; } }  public void UpdateSize(Vector2 size,float speed) { this.targetSize = size; this.speed = speed; }}

腳本掛載上去后,在Item下按如下圖方式設(shè)值


可以按自己喜好調(diào)整數(shù)值。

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 东至县| 和顺县| 榆林市| 罗田县| 徐水县| 民勤县| 滁州市| 奉贤区| 弥渡县| 和硕县| 肇州县| 乐安县| 十堰市| 稻城县| 诸暨市| 黄山市| 万荣县| 伊宁市| 香港 | 尼勒克县| 铜川市| 明溪县| 南通市| 米脂县| 绥芬河市| 恩施市| 农安县| 阿拉尔市| 新乡市| 工布江达县| 离岛区| 连江县| 延寿县| 长宁区| 新河县| 皋兰县| 徐闻县| 望江县| 青海省| 濮阳县| 镇原县|