• 点击按钮收缩功能


    要实现的效果就是,当点击长button时,长button相对应的下面两个段button会收进长button里,并且,下面的其他组件(这里是button)会相应的往上移动。

    左图为层级结构,右图为运行效果。

    注:我在Panel(3)上加了一个Vertical Layout Group组件用来布局,使里面三个panel纵向排列。

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using UnityEngine.EventSystems;
    
    public class Buttontask : MonoBehaviour {
    
        public Button[] buttons;//每个panel下按钮的集合
        public GameObject panel;//父物体panel
        private List<Vector3> childbuttonlocal = new List<Vector3>();//记录长button下子button的localposition
        private bool open=true;//是否是展开状态
       // private Vector3 openlocationposition;
        Vector2 sizedelta;//父对象panel的width和height
        // Use this for initialization
        void Start () {
            for (int  i=0;i< buttons.Length;i++)//展开状态下每个子按钮的相对位置
            {
                childbuttonlocal.Add(buttons[i].transform.localPosition);
               // Debug.Log(childbuttonlocal[i]);
            }
            sizedelta = panel.transform.GetComponent<RectTransform>().sizeDelta;//展开状态下panel的宽高
        }
    	
    	// Update is called once per frame
    	void Update () {
    		
    	}
        public void OnClick()
        {
            if (open)
            {
            //设置panel大小 panel.transform.GetComponent<RectTransform>().sizeDelta = new Vector2(panel.transform.GetComponent<RectTransform>().sizeDelta.x, 50f); for (int i=0; i<buttons.Length;i++) { // buttons[i].transform.localPosition = Vector3.Lerp(childbuttonlocal[i], new Vector3(childbuttonlocal[i].x,0,0), 1);
               buttons[i].transform.localPosition=new Vector(childbuttonlocal[i].x,0,0); //设置位置 buttons[i].gameObject.SetActive(false); } open = false; } else { panel.transform.GetComponent<RectTransform>().sizeDelta = sizedelta; for (int i = 0; i < buttons.Length; i++) { buttons[i].gameObject.SetActive(true); buttons[i].transform.localPosition = Vector3.Lerp(new Vector3(childbuttonlocal[i].x, 0, 0), childbuttonlocal[i], 1); } open = true; } } }

     当然可以用动画做,这样感觉更简单点,还能有动画效果,怪我懒,不想再去折腾动画了...

  • 相关阅读:
    SQL Server:创建索引视图
    Asp.Net常用函数
    SQL Server联机丛书:删除存储过程
    音乐知识全接触
    深入透析样式表滤镜
    有一天,爸妈会变老
    今天终于买到票啦~~
    今天,回到上海啦~~(附工作生涯回顾)
    十八问:怎么才是喜欢编程
    把旧光驱改CD播放机的方法
  • 原文地址:https://www.cnblogs.com/lanrenqilanming/p/7419348.html
Copyright © 2020-2023  润新知