您当前的位置: 首页 >  unity

unity工具人

暂无认证

  • 3浏览

    0关注

    205博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

unity 可以点击翻页的滑栏

unity工具人 发布时间:2021-05-29 08:07:49 ,浏览量:3

需要先导入DOTween插件 脚本非常简单,仅仅是加减运算而已

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;

public class SelectTraget : MonoBehaviour
{
    public Button Btn_Left;
    public Button Btn_Right;
    public Transform ReferencePoint;//参考位置(显示区域中央)
    public Transform Content;//被移动物体 
    private int index;
    public int Indexer
    {
        get 
        { 
            return index;
        }
        set 
        {
            if (value  Content.transform.childCount-1)
            {
                print("最右");
                index = Content.transform.childCount - 1;
            }
            else
            {
                index = value;
                //移动
                float A = Content.GetChild(index).position.x - ReferencePoint.position.x;//差值;
                Content.DOMoveX(Content.position.x - A, 1f);
            }
            print(index);
        }
    }
    
    public void Awake()
    {
        Btn_Left = transform.Find("Btn_Left").GetComponent();
        Btn_Right = transform.Find("Btn_Right").GetComponent();

        Btn_Back = transform.parent.Find("Btn_Back").GetComponent();
        Btn_Sure = transform.parent.Find("Btn_Sure").GetComponent();
        ReferencePoint = transform.Find("ReferencePoint");

        Content = transform.Find("Viewport/Content");
        

        Btn_Left.onClick.AddListener(InputLeft);
        Btn_Right.onClick.AddListener(InputRight);
    }

    public void InputLeft()
    {
        print("Left");
        Indexer--;
    }
    public void InputRight()
    {
        print("Right");
        Indexer++;
    }
}

脚本挂在Scroll View 上 其它节点如下图 在这里插入图片描述 运行效果在这里插入图片描述

//版本二:加了几行,控制左右按钮显示和隐藏

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;

public class SelectTarget : MonoBehaviour
{
    public Button Btn_Left;
    public Button Btn_Right;
    public Transform ReferencePoint;//参考位置(显示区域中央)
    public Transform Content;//被移动物体 
    private int index;
    public int Indexer
    {
        get
        {
            return index;
        }
        set
        {
            if (value  Content.transform.childCount - 1)
            {
                index = Content.transform.childCount - 1;
            }
            else
            {
                index = value;
                float A = Content.GetChild(index).position.x - ReferencePoint.position.x;
                Content.DOMoveX(Content.position.x - A, 1f);
            }


            if (index == 0)
            {
                print(" //最左");
                Btn_Left.gameObject.SetActive(false);
            }
            else
                Btn_Left.gameObject.SetActive(true);
            if (index == Content.childCount-1)
            {
                print(" //最右");
                Btn_Right.gameObject.SetActive(false);
            }
            else
                Btn_Right.gameObject.SetActive(true);
        }
    }

    public void Awake()
    {
        Btn_Left = transform.Find("Btn_Left").GetComponent();
        Btn_Right = transform.Find("Btn_Right").GetComponent();             
        ReferencePoint = transform.Find("ReferencePoint");
        Content = transform.Find("Viewport/Content");
        Btn_Left.onClick.AddListener(InputLeft);
        Btn_Right.onClick.AddListener(InputRight);
    }

    private void OnEnable()
    {       
        ResetList();
    }

    public void ResetList()
    {
        index = 0;
        float A = Content.GetChild(0).position.x - ReferencePoint.position.x;//差值;
        Content.DOMoveX(Content.position.x - A, 0);
        Btn_Left.gameObject.SetActive(false);
    }

    public void InputLeft()
    {
        print("Left");
        Indexer--;
    }
    public void InputRight()
    {
        print("Right");
        Indexer++;
    }
}
关注
打赏
1656671177
查看更多评论
立即登录/注册

微信扫码登录

0.0804s