【NGUI】Unity中ScrollView3D滚动效果

来源:互联网 发布:无人快餐工厂 知乎 编辑:程序博客网 时间:2024/06/04 18:24
using UnityEngine;using System.Collections;/// <summary>/// 脚本位置:预制体身上/// </summary>public class RoleItem : MonoBehaviour {    // role    卡片的深蓝背景    // active  toggle激活的时候显示浅蓝的图片    // texture 人物图片    // 用来调整深度     public UIWidget[] widgets;    // 人物图片    public UITexture RenWutexture;    /// <summary>    /// tween完成后记录当前位置    /// </summary>    public void Finish()    {         TweenPosition tp = gameObject.GetComponent<TweenPosition>();        tp.from = gameObject.transform.localPosition;       // 正常动画播放一次就不能在播放了,使用这句代码可以是动画再次播放        tp.ResetToBeginning();    }} 


using UnityEngine;using System.Collections;using System.Collections.Generic;/// <summary>/// 脚本位置:ui摄像机/// 脚本功能:生成人物卡牌数组,控制卡牌移动/// </summary>public class SelectRole : MonoBehaviour{// 卡牌数组的父物体public GameObject cardsObj;// 单边卡牌的数量(以最大数量为基数 + 中间卡牌数1)private int halfCardNumber = 0;// x轴的移动距离private int _movX = 150;// y轴的移动距离private int _movY = 50;// 一个人物卡牌上需要调整深度的组件数量private int count = 3;private List<RoleItem> _roleList = new List<RoleItem> ();Texture[] textureList ;// Use this for initializationvoid Start (){// 加载图片数组textureList = Resources.LoadAll<Texture> ("Pictures");// 最大深度也可以说是最前面那张图片的默认深度int maxDepth = textureList.Length / 2 + 1;// 单边卡牌数 既 计算的默认深度 (比如8张图,中间1张,左右一个4张,一个3张)// 所以这个单边卡牌的数量就是4+1 = 5张halfCardNumber = maxDepth;// 遍历Resources加载到的所有图片for (int i = 0; i < textureList.Length; i++) {            // 加载人物卡牌图片预设体(注意不能加载到UIRoot的外面,那样会自动创建多个UICamera)GameObject card = Instantiate (Resources.Load<GameObject> ("Role"));  // 将生成的人物卡牌的父亲设置为卡牌数组          card.transform.parent = cardsObj.transform;// 大小设置为1(根据自己当前需求更改父物体的大小即可)card.transform.localScale = Vector3.one;// NGUI和UGUI一样无法给预制体添加触发方法,所以只能通过代码控制添加触发方法// 触发方法添加给UIToggle的当值改变的时候,调用当前脚本的RoleToggleChange方法EventDelegate.Add (card.GetComponent<UIToggle>().onChange, RoleToggleChange);           // RoleItem就是一个保存人物卡片上三张图片深度数据的类脚本RoleItem item = card.GetComponent<RoleItem> ();                   // 获取生成的预制体卡牌的一个texture属性,赋值为当前自己所在的预制体身上的图片 // 注意:赋值之前,要保证这个mainTexture原本就是有图的   item.RenWutexture.mainTexture = textureList [i];#region 设置名字// 设置人物卡牌的命名排序规则// 当前生成的预制体名字为默认 最大深度 既居中那张的数字// 默认加载的第0张图片是居中的,加载顺序与文件夹中图片的顺序无关card.name = maxDepth.ToString ();if (i > 0) {// 奇数设置为右边,下标为正数if (i % 2 == 1) {// 计算名字maxDepth--;card.name = maxDepth.ToString ();}                // 偶数设置为左边,下标为负数                else {// 这里深度不需要再减的原因是已经在奇数位减过了// 比如9张牌,居中为5,先奇数减一变成4,那么下一次就是偶数,应该是-4了card.name = "-" + maxDepth.ToString ();}} #endregion// 初始化人物卡牌的位置 // 调用设置人物卡牌的深度以及位置SetDepthAndPosition (item, 0, 0);_roleList.Add(item);}        }// 设置角色深度及位置// role :角色// dir  :往左=1,往右=-1,初始化=0// index无实际意义,表示这个下标下一次期望变换到这个数private void SetDepthAndPosition (RoleItem role, int dir, int index){// 深度int indexDepth = 0;#region 发生移动// 左右移动后,重新排序命名// 第一次初始化的时候并不调用if (dir != 0) {// index通过当前名字转换过来的// 最中间位置的编号变化,右移:原-4变5;左移:原5变-4// 正常情况下,左移,所有数都会-1// 但中间那张牌不是,需要减一,再乘一个负数// 左一负数那张,需要变成正数if (index * dir > halfCardNumber) {// 原中间位置向左或向右移动后的序号// 设置深度,也是名字indexDepth = -dir * (halfCardNumber - 1);Debug.Log ("change");} else {// 非中间位置卡片移动,当卡片数为奇数时,最后一张卡片的x坐标为原来坐标的相反数;偶数是则为相反数-1// index=0既,左面的-1要到右面变为+1if (index == 0) {// 当前列表中,如果是偶数个元素// 如果是负数->正数  深度设置为1或者2(偶数个设置为2,奇数个设置为1)indexDepth = textureList.Length % 2 == 1 ? dir : dir * 2;} else {// 不是最深层的人物卡片,不需要切换正负坐标// 直接设置为它期望变成的值,不符合要求的中间卡牌NUM+1,已经在上面处理了indexDepth = index;}}// 设置当前名字role.name = indexDepth.ToString ();  }#endregion#region 如果移动方向为0的时候,表示初始化 if (dir == 0) {// 设置深度为我的名字,如果是负数在下面有判断// 不在这里转换的原因是,需要利用正负号设置左右移动的数值indexDepth = int.Parse (role.gameObject.name);}#endregion// 获取人物卡片的TweenPostion组件TweenPosition tp = role.GetComponent<TweenPosition> ();// -(_half + indexDepth)如果是9张图,当前第为-4的话,就是左面靠前数第一张,所以移动一个单位// 设置X坐标(需要使用正负符号)int x = indexDepth < 0 ? -(halfCardNumber + indexDepth) * _movX : (halfCardNumber - indexDepth) * _movX;// 设置Y,Z坐标(都为正值,需要转换绝对值)indexDepth = Mathf.Abs (indexDepth);tp.to = new Vector3 (x, (halfCardNumber - indexDepth) * _movY, 0);// 设置图片深度(层级)// role是RoleItem脚本for (int i = 0; i < role.widgets.Length; i++) {// 如果不乘一个系数,就会出现左2的人物图片可能比左1的背景图片深度高的情况// 这里设置预制体的时候,一定必须按照,背景图片,toggle激活状态图片,人物图片的顺序拖拽role.widgets [i].depth = count * indexDepth + i;}   // 如果当前的indexDepth值,是全局中默认的图片中间那张的深度// 就把toggle设置为truerole.GetComponent<UIToggle> ().value = indexDepth == halfCardNumber ? true : false;// 播放tween动画// 默认生成位置都是中间,需要播放一次动画才能移动到正确的位置tp.PlayForward ();}/// <summary>/// 左边/// </summary>public void LeftClick (){//重新排列顺序foreach (RoleItem role in _roleList) {int index = int.Parse (role.name); SetDepthAndPosition (role, 1, ++index);} }/// <summary>/// 右边/// </summary>public void RightClick (){//重新排列顺序foreach (RoleItem role in _roleList) {int index = int.Parse (role.name);SetDepthAndPosition (role, -1, --index);}}// 鼠标选中某个人物的时候public void RoleToggleChange (){ // 当前点击的toggle的值if (UIToggle.current.value) {// 计算当前深度和下标int index = int.Parse (UIToggle.current.name);// 计算需要移动的次数int moveCount = halfCardNumber - Mathf.Abs(index);for (int i = 0; i < moveCount; i++) {if (index > 0)LeftClick ();elseRightClick ();}                     }}}


//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

第二版

using UnityEngine;using System.Collections;public class CardInfos : MonoBehaviour {public UIWidget[] widgets;public UITexture  cardTexture;public void OnTweenPositionFinish(){TweenPosition tp = GetComponent<TweenPosition>();tp.from = gameObject.transform.localPosition;tp.ResetToBeginning();}}

using UnityEngine;using System.Collections;using System.Collections.Generic;/// <summary>/// 脚本位置:ui摄像机/// 脚本功能:生成人物卡牌数组,控制卡牌移动/// </summary>public class SelectCard : MonoBehaviour {// 卡牌数组的父物体public GameObject cardManager;// 单边卡牌的数量(以最大数量为基数 + 中间卡牌数1)private int   halfCardNumber = 0;private int moveX = 150;private int moveY = 30;private int   componetCount = 3;private List<CardInfos>  cardInfosList = new List<CardInfos> ();private Texture[] cardsTextures;void Start(){cardsTextures = Resources.LoadAll<Texture>("Pictures");int maxIndex = cardsTextures.Length / 2 + 1;halfCardNumber = maxIndex;for (int i = 0; i < cardsTextures.Length; i++) {GameObject goCard = Instantiate(Resources.Load<GameObject>("Card"));goCard.transform.parent = cardManager.transform;goCard.transform.localScale = Vector3.one;EventDelegate.Add(goCard.GetComponent<UIToggle>().onChange,OnToggleValueChange);CardInfos item = goCard.GetComponent<CardInfos>();item.cardTexture.mainTexture = cardsTextures[i];goCard.name = maxIndex.ToString();if (i > 0) {if (i % 2 == 1 ) {maxIndex--;goCard.name = maxIndex.ToString();} else {goCard.name = "-" + maxIndex.ToString();}}SetDepthAndPosition(item, 0, 0);cardInfosList.Add(item);}}// 往左=1  往右=-1,初始化=0public void SetDepthAndPosition(CardInfos cardInfo, int direction, int nextIndex){int tempIndex = 0;if (direction != 0) {if (nextIndex * direction > halfCardNumber) {tempIndex = -direction * (halfCardNumber - 1);} else {if (nextIndex == 0) {tempIndex = cardInfosList.Count % 2 == 1 ? direction : direction * 2;} else {tempIndex = nextIndex;}}cardInfo.gameObject.name = tempIndex.ToString();}if (direction == 0) {tempIndex = int.Parse(cardInfo.gameObject.name);}TweenPosition tp = cardInfo.GetComponent<TweenPosition>();int x = tempIndex > 0 ? (halfCardNumber - tempIndex) * moveX : -(halfCardNumber + tempIndex) * moveX;tempIndex = Mathf.Abs(tempIndex);tp.to = new Vector3(x, (halfCardNumber - tempIndex) * moveY, 0);for (int i = 0; i < cardInfo.widgets.Length; i++) {cardInfo.widgets[i].depth = componetCount * tempIndex + i;}cardInfo.GetComponent<UIToggle>().value = tempIndex == halfCardNumber ? true : false;tp.PlayForward();}public void OnClickLeft(){foreach (CardInfos cardInfo in cardInfosList) {int nowIndex = int.Parse(cardInfo.name);SetDepthAndPosition(cardInfo, 1, ++nowIndex);}}public void OnClickRight(){foreach (CardInfos cardInfo in cardInfosList) {int nowIndex = int.Parse(cardInfo.name);SetDepthAndPosition(cardInfo, -1, --nowIndex);}}public void OnToggleValueChange(){if (UIToggle.current.value) {int index = int.Parse(UIToggle.current.name);int needMoveCount = halfCardNumber - Mathf.Abs(index);for (int i = 0; i < needMoveCount; i++) {if (index > 0) {OnClickLeft();} else {OnClickRight();}}}}}



0 0
原创粉丝点击