Unity3D实现扭动挤压浏览效果

作者:即步 时间:2022-04-23 22:23:16 

最近的项目中,想做到一种能够吸引眼球的一种角色选择浏览效果

Demo源码:点击打开链接

最终实现了下按如下图这么一种浏览效果:

Unity3D实现扭动挤压浏览效果

效果图一

Unity3D实现扭动挤压浏览效果

效果图二

可能要实现这么一种效果用动画插件会很快,但总感觉有点大材小用
这里我向大家分享一个极简方式来实现这么一种效果

目录结构如下

其中Items有4个Image子节点

Unity3D实现扭动挤压浏览效果

在父节点Items下添加如下图横向布局组件

Unity3D实现扭动挤压浏览效果

在其4个Image子节点下添加如下图布局元素组件

Unity3D实现扭动挤压浏览效果

完成这些步骤后接下来就是代码实现了
在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个子节点下添加如下脚本组件


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下按如下图方式设值

Unity3D实现扭动挤压浏览效果

可以按自己喜好调整数值。

来源:https://blog.csdn.net/qq_33747722/article/details/74853681

标签:Unity3D,扭动,挤压
0
投稿

猜你喜欢

  • 在Winform框架界面中改变并存储界面皮肤样式的方法

    2021-06-08 09:08:24
  • JAVA实现将磁盘中所有空文件夹进行删除的代码

    2022-09-01 04:59:41
  • springboot aop里的@Pointcut()的配置方式

    2021-11-18 20:55:37
  • 一文搞懂MyBatis多数据源Starter实现

    2023-07-19 03:34:22
  • Android实现移动小球和CircularReveal页面切换动画实例代码

    2023-03-03 03:45:50
  • java实现简单计算器功能

    2021-06-17 11:21:23
  • 基于servlet实现统计网页访问次数

    2021-11-05 08:35:13
  • 关于maven使用过程中无法导入依赖的一些总结

    2021-12-16 01:51:20
  • 四种引用类型在JAVA Springboot中的使用详解

    2023-11-24 03:34:38
  • 如何让java只根据数据库表名自动生成实体类

    2022-02-24 04:25:52
  • 解决SpringMvc中普通类注入Service为null的问题

    2023-10-29 01:23:25
  • C#使用FileStream复制一个任意文件

    2022-04-17 19:55:17
  • Android Studio 升级到3.0后输入法中文状态下无法选词的终极解决方案

    2022-11-21 07:34:41
  • 使用SpringBoot实现微服务超时重试模式的示例

    2021-12-28 13:58:45
  • C#调用WebService的方法介绍

    2022-06-05 01:10:30
  • SpringBoot小程序推送信息的项目实践

    2021-12-07 04:23:34
  • Spring Boot 集成Mybatis实现主从(多数据源)分离方案示例

    2023-08-10 07:43:21
  • 浅谈Java消息队列总结篇(ActiveMQ、RabbitMQ、ZeroMQ、Kafka)

    2022-06-13 01:30:40
  • Java Swing中JTable渲染器与编辑器用法示例

    2022-11-02 09:23:13
  • java实现字符串四则运算公式解析工具类的方法

    2021-11-03 09:22:23
  • asp之家 软件编程 m.aspxhome.com