【unity】【游戏开发】使用NGUI制作可以左右切换的菜单

来源:互联网 发布:阿里云学生认证条件 编辑:程序博客网 时间:2024/05/19 06:17

啦啦啦 博客の时间

周一到今天 不行不行的制作了项目中的一个小功能,

是一个左右切换的菜单 如图所示 (我是图:p),总结了一点点小心得,

拿出来嘚瑟一下,各位看官,我知道你们和我一样没钱,捧个人场吧(T_T)

上图咯


当当当当(自带背后闪金光)

效果就是这样啦(自行脑补动态效果)

点击哪边就会向哪边自动缩小的;



其实呢这只是两块板的(0_0)

我们暂且称为b1 和b2

使用了tweenposition 和tweenscale 来搞定的


上代码

 private void AddTween()    {        Board1s = this.Board1.AddComponent<TweenScale>();        Board2s = this.Board2.AddComponent<TweenScale>();        Board1p = this.Board1.AddComponent<TweenPosition>();        Board2p = this.Board2.AddComponent<TweenPosition>();        Board1s.from = new Vector3(1, 1, 1);        Board1s.to = new Vector3(0,0,0);        Board1s.duration = 1f;        Board1p.from = new Vector3(0, 0, 0);        Board1p.to = new Vector3(-400, 0, 0);        Board1p.duration = 1f;        Board2s.from = new Vector3(0, 0,0);        Board2s.to = new Vector3(1, 1, 1);        Board2s.duration = 1f;        Board2p.from = new Vector3(400, 0, 0);        Board2p.to = new Vector3(0, 0, 0);        Board2p.duration = 1f;    }
<pre name="code" class="csharp"> public void LeftBtn()    {        if (IsFirst)        {            AddTween();            IsFirst = false;        }        if (IsRight == true)        {            IsRight = false;            IsLeft = true;            this.setBoardData(IsLeft);            this.play(true);            this.setBegain();            return;        }        IsLeft = true;        IsRight = false;        this.setBoardData(IsLeft);        this.setBegain();        this.play(true);<pre name="code" class="csharp"> public void RightBtn()    {        if (IsFirst)        {            AddTween();            IsFirst = false;        }        if (IsLeft == true)        {            IsLeft = false;            IsRight = true;            this.setBoardData(IsLeft);            this.play(false);            this.setBegain();            return;        }        IsRight = true;        IsLeft = false;        this.setBoardData(IsLeft);        this.setBegain();        this.play(false);           }

  private void setBegain()    {        Board1s.ResetToBeginning();        Board1p.ResetToBeginning();        Board2s.ResetToBeginning();        Board2p.ResetToBeginning();    }    private void play(bool isForward)    {        if (isForward)        {            Board1s.PlayForward();            Board1p.PlayForward();            Board2s.PlayForward();            Board2p.PlayForward();        }        else        {            Board1s.PlayReverse();            Board1p.PlayReverse();            Board2p.PlayReverse();            Board2s.PlayReverse();        }    }

中间初始化的时候涉及到连续左右切换的情况 各位看官自己分析下 为什么这么做;

有助学习(此处有背后闪金光的微笑)


其实这工作法就是一种障眼法  和使用scrollview 做出来的效果是没有区别的  

但是这种做法是非常节省资源的 即使数据量很庞大 只要做好了 菜单上资源的切换 就可以了

你看啊  如果有1000条  你使用scrollview 要好多资源

这种方式只要两个就够了  并且在数据层也不会一次性全部加载完

而是使用哪个就加载哪个

啦啦啦 搞定了 写完了 (我听到有人在说  妈的智障。。。。听不到听不到)

2 0