UnityUGUI流动效果轻松实现:告别复杂Shader,一招搞定动态背景

引言

在Unity开发过程中,我们常常会遇到这样一个需求:将一张简单的图片通过平铺效果进行展示,以此来减少图片的大小。这个操作非常简单,只需将Image的Type设置为Tiled,然后调整RectTransform的尺寸即可。但有时候,产品经理会提出更高的要求:让这个图片动起来。这听起来有点麻烦,对吧?当然,我们可以通过写shader来实现这个功能,但现在,我要给大家介绍一个更简单的办法,只需用代码控制即可实现。

实现流动效果

我们先来看一下最终的效果。箭头流动效果展示 这个流动的效果,其实是通过控制RawImage的UV坐标来实现的。
箭头流动效果展示

实现步骤关键点

  1. 将原图片的格式设置为Repeat将原图片的格式设置为Repeat

  2. 创建RawImage,将箭头图片拖入,设置合适的大小和位置创建RawImage,拖入箭头图片,设置适合的大小位置

  3. 创建脚本,编写代码。创建脚本

  4. 将脚本挂载到RawImage上,设置参数,运行。设置参数

代码

using UnityEngine;
using UnityEngine.UI;

public class FlowArrowController : MonoBehaviour
{
    public RawImage flowArrow;
    public Vector2 originalSize;
    public FlowDirection direction = FlowDirection.Up;
    public float speed = 1f;

    private void Update()
    {
        FlowArrows(flowArrow, originalSize, direction, speed, Time.deltaTime);
    }

    private void FlowArrows(RawImage rawImage, Vector2 originalSize, FlowDirection direction, float speed, float deltaTime)
    {
        // 获取RawImage的当前UVRect
        Rect uvRect = rawImage.uvRect;

        // 根据RawImage的尺寸和原始图片尺寸计算平铺的UVRect高度和宽度
        float widthRatio = rawImage.rectTransform.rect.width / originalSize.x;
        float heightRatio = rawImage.rectTransform.rect.height / originalSize.y;
        uvRect.width = widthRatio;
        uvRect.height = heightRatio;

        // 根据方向更新UVRect的值
        switch (direction)
        {
            case FlowDirection.Up:
                uvRect.y = Mathf.Repeat(uvRect.y - (speed * deltaTime), 1);
                break;
            case FlowDirection.Down:
                uvRect.y = Mathf.Repeat(uvRect.y + (speed * deltaTime), 1);
                break;
            case FlowDirection.Left:
                uvRect.x = Mathf.Repeat(uvRect.x + (speed * deltaTime), 1);
                break;
            case FlowDirection.Right:
                uvRect.x = Mathf.Repeat(uvRect.x - (speed * deltaTime), 1);
                break;
        }

        // 更新RawImage的UVRect
        rawImage.uvRect = uvRect;
    }
}

public enum FlowDirection
{
    Up,
    Down,
    Left,
    Right
}

结论

通过上述的步骤和代码,我们就可以实现一个简单的流动效果。怎么样,是不是很简单呢?下次遇到类似的需求,就可以用这个方法来解决了。不过,如果产品经理的要求更高,比如要实现更复杂的流动效果,那可能还是需要用到shader。不过,那就不是这篇文章的内容了,我们下次再聊。

  • 6
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我正在帮您寻找相关资源并准备回答您的问题。 以下是一个抗锯齿的 Unity UGUI Shader 实现的示例代码: ``` Shader "Custom/UGUI-AntiAliasing" { Properties { _MainTex ("Texture", 2D) = "white" {} _Color ("Color", Color) = (1,1,1,1) _Multiplier ("Multiplier", Range(0, 1)) = 0.5 _Outline ("Outline", Range(0, 1)) = 0.2 } SubShader { Tags {"Queue"="Transparent" "RenderType"="Transparent"} LOD 100 Pass { ZWrite Off Cull Off Fog { Mode Off } Blend SrcAlpha OneMinusSrcAlpha CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; fixed4 color : COLOR; }; struct v2f { float2 uv : TEXCOORD0; float4 vertex : SV_POSITION; float4 color : COLOR; }; sampler2D _MainTex; float _Multiplier; float _Outline; fixed4 _Color; v2f vert (appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); o.uv = v.uv; o.color = v.color * _Color; return o; } fixed4 frag (v2f i) : SV_Target { fixed4 col = tex2D(_MainTex, i.uv) * i.color; float dist = length(ddx(i.vertex.xy) + ddy(i.vertex.xy)); float alpha = smoothstep(_Outline-_Multiplier, _Outline, dist); col.a *= alpha; return col; } ENDCG } } FallBack "UI/Default" } ``` 您可以将上述代码保存到一个`.shader` 文件中,再将其添加到您的 Unity 项目中,即可使用此Shader。 请注意:Shader中的`_Multiplier`和`_Outline`参数是用来控制描边和抗锯齿程度的。您可以根据自己的需求进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值