Unity3D實(shí)現(xiàn)扭動(dòng)擠壓瀏覽效果
最近的項(xiàng)目中,想做到一種能夠吸引眼球的一種角色選擇瀏覽效果
Demo源碼:點(diǎn)擊打開(kāi)鏈接
最終實(shí)現(xiàn)了下按如下圖這么一種瀏覽效果:
效果圖一
效果圖二
可能要實(shí)現(xiàn)這么一種效果用動(dòng)畫(huà)插件會(huì)很快,但總感覺(jué)有點(diǎn)大材小用
這里我向大家分享一個(gè)極簡(jiǎn)方式來(lái)實(shí)現(xiàn)這么一種效果
目錄結(jié)構(gòu)如下
其中Items有4個(gè)Image子節(jié)點(diǎn)
在父節(jié)點(diǎn)Items下添加如下圖橫向布局組件
在其4個(gè)Image子節(jié)點(diǎn)下添加如下圖布局元素組件
完成這些步驟后接下來(lái)就是代碼實(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ù)值。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:Unity shader實(shí)現(xiàn)高斯模糊效果
欄 目:C#教程
下一篇:Unity實(shí)現(xiàn)游戲卡牌滾動(dòng)效果
本文標(biāo)題:Unity3D實(shí)現(xiàn)扭動(dòng)擠壓瀏覽效果
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/4881.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新聞效果的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法
- 01-10C#實(shí)現(xiàn)多線程下載文件的方法
- 01-10C#實(shí)現(xiàn)Winform中打開(kāi)網(wǎng)頁(yè)頁(yè)面的方法
- 01-10C#實(shí)現(xiàn)遠(yuǎn)程關(guān)閉計(jì)算機(jī)或重啟計(jì)算機(jī)的方法
- 01-10C#自定義簽名章實(shí)現(xiàn)方法
- 01-10C#文件斷點(diǎn)續(xù)傳實(shí)現(xiàn)方法
- 01-10winform實(shí)現(xiàn)創(chuàng)建最前端窗體的方法


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫(xiě)Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10delphi制作wav文件的方法
- 04-02jquery與jsp,用jquery
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子