雷火电竞-中国电竞赛事及体育赛事平台

歡迎來到入門教程網!

C#教程

當前位置:主頁 > 軟件編程 > C#教程 >

Unity3D實現扭動擠壓瀏覽效果

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C#教程|點擊:

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

Demo源碼:點擊打開鏈接

最終實現了下按如下圖這么一種瀏覽效果:

效果圖一

效果圖二

可能要實現這么一種效果用動畫插件會很快,但總感覺有點大材小用
這里我向大家分享一個極簡方式來實現這么一種效果

目錄結構如下

其中Items有4個Image子節(jié)點

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

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

完成這些步驟后接下來就是代碼實現了
在Items添加如下腳本組件

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
public class Items : MonoBehaviour
{
 public List<GameObject> items = new List<GameObject>();
 
 //縮放時間
 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個子節(jié)點下添加如下腳本組件

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);
 //點擊
 public VoidDelegate onClick;
 //鼠標按下
 public VoidDelegate onDown;
 //鼠標抬起
 public VoidDelegate onUp;
 //鼠標移入
 public VoidDelegate onEnter;
 //鼠標移出
 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下按如下圖方式設值


可以按自己喜好調整數值。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持我們。

上一篇:Unity shader實現高斯模糊效果

欄    目:C#教程

下一篇:Unity實現游戲卡牌滾動效果

本文標題:Unity3D實現扭動擠壓瀏覽效果

本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/4881.html

網頁制作CMS教程網絡編程軟件編程腳本語言數據庫服務器

如果侵犯了您的權利,請與我們聯系,我們將在24小時內進行處理、任何非本站因素導致的法律后果,本站均不負任何責任。

聯系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網 版權所有