Unity之 制作卷轴展开显示画面效果

一、效果

 

二、思路 

使用一个矩形图片添加Mask,Mask子物体下是要展示的卷轴bg图片

场景演示示例 :左侧灰色的举行图片是Mask,在场景中拖动bg图片后的效果。

这样卷轴滚动的画面就有了,用同样的方法在添加一个卷轴让他变得更加真实

 使用animator录制动画,移动卷轴,同时移动卷轴背景内容,就产生了卷轴展开的效果(内容效果的展示也是如此)注意内容的展示,Mask是控制大小,同时内容图片锚点要设置成如下

三、脚本项目下载

控制animator动画,类似进度条效果

using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
using UnityEngine.EventSystems;

/// <summary>
/// 卷轴拖动效果
/// </summary>
public class ScrollEffect : MonoBehaviour
{
    public float speed=0.2f;
    public RectTransform drayScroll;
    public Animator ani;

    void Start()
    {
        
        ani.speed = 0;

    }
    public bool isDray;
    float step;
    Vector3 mouseOldPos;
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            mouseOldPos = Input.mousePosition;
        }

        if (Input.GetMouseButton(0))
        {
            if (!isDray)
                isDray = GetFirstPickGameObject(Input.mousePosition) == drayScroll.gameObject;

            //单击卷轴可移动
            if (!isDray)
                return;

            Vector2 mousePos = Input.mousePosition - mouseOldPos;
            step += mousePos.x*speed * Time.deltaTime;

            if (mousePos.x>0)
            {
                if (step>=1)
                {
                    step = 1;
                }
            }
            else
            {
                if (step <= 0)
                {
                    step = 0;
                }
            }
            ani.Play("Test", 0, step);

            mouseOldPos = Input.mousePosition;

        }

        if (Input.GetMouseButtonUp(0))
        {
            isDray = false;
            ani.StopPlayback();
        }
    }


    #region UI射线检测
    /// <summary>
    /// 获取点击的UI类型Item
    /// </summary>
    /// <param name="position">点击屏幕坐标</param>
    /// <returns></returns>
    public GameObject GetFirstPickGameObject(Vector2 position)
    {
        EventSystem eventSystem = EventSystem.current;
        PointerEventData pointerEventData = new PointerEventData(eventSystem);
        pointerEventData.position = position;
        //射线检测ui
        List<RaycastResult> uiRaycastResultCache = new List<RaycastResult>();
        eventSystem.RaycastAll(pointerEventData, uiRaycastResultCache);
        if (uiRaycastResultCache.Count > 0)
            return uiRaycastResultCache[0].gameObject;
        return null;
    }
    #endregion

}

项目下载:

链接:https://pan.baidu.com/s/13H_OIAcEEDc078tBUEiduw?pwd=syq1 
提取码:syq1

animator

1.倒放: ani.speed = -1; 0停止 1正方 -1倒放

要在Unity中实现卷轴效果,可以通过以下步骤进行: 1. 创建一个Plane对象作为卷轴的背景,设置好材质和纹理。 2. 在卷轴上添加一个Cylinder对象,作为卷轴的主体,设置好材质和纹理。 3. 将卷轴的Cylinder对象的旋转中心点放在底部,使得卷轴可以像真正的卷轴一样展开和收起。 4. 在代码中使用Transform.RotateAround()函数控制卷轴展开和收起,同时可以通过修改卷轴的旋转角度实现卷轴的滚动效果。 以下是一个简单的示例代码: ```csharp public class ScrollController : MonoBehaviour { public float scrollSpeed = 10f; public float unfoldSpeed = 50f; public float foldSpeed = 20f; private bool isUnfolding = false; private bool isFolding = false; void Update() { if (Input.GetKeyDown(KeyCode.U)) { isUnfolding = true; isFolding = false; } if (Input.GetKeyDown(KeyCode.F)) { isFolding = true; isUnfolding = false; } if (isUnfolding) { float step = unfoldSpeed * Time.deltaTime; transform.RotateAround(transform.position + Vector3.down * 0.5f, Vector3.right, step); if (transform.rotation.eulerAngles.x >= 180f) { isUnfolding = false; } } if (isFolding) { float step = foldSpeed * Time.deltaTime; transform.RotateAround(transform.position + Vector3.down * 0.5f, Vector3.left, step); if (transform.rotation.eulerAngles.x <= 0f) { isFolding = false; } } float scroll = Input.GetAxis("Mouse ScrollWheel"); if (scroll != 0f) { float step = scrollSpeed * Time.deltaTime * scroll; transform.RotateAround(transform.position + Vector3.down * 0.5f, Vector3.forward, step); } } } ``` 在这个示例代码中,我们使用鼠标滚轮控制卷轴的滚动,使用键盘按键控制卷轴展开和收起。注意,在代码中我们使用了Transform.RotateAround()函数来控制卷轴的旋转。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值