【Unity开发】Unity实现英雄联盟选择皮肤效果
2015-11-28 20:16
627 查看
using UnityEngine; using System.Collections; /// <summary> /// 脚本位置:Card预制体身上 /// 脚本功能:保存Card所有数据信息,用于更换图片 /// </summary> public class CardInfo : MonoBehaviour { // 保存3张图片的深度信息 // 背景图片-》toggle激活图片-》人物图片 public UIWidget [] widgets; // 用于控制图片的切换 public UITexture cardTexture; // 当TweenPosition动画播放完成的时候 // 执行该方法 public void OnTweenPositionFinish() { // 获取自身当前的tween动画 TweenPosition tp = GetComponent<TweenPosition>(); // 设置动画的起点 tp.from = gameObject.transform.localPosition; // 将动画重置成开始状态 tp.ResetToBeginning(); } }
using UnityEngine; using System.Collections; using System.Collections.Generic; /// <summary> /// 脚本位置:UI Root /// 脚本功能:生成预制体,改变预制体图片 /// </summary> public class SelectCard : MonoBehaviour { // 所有卡牌的父物体 public GameObject cardManager; // 中间卡牌的下标索引 private int centerCardIndex = 0; private int moveX = 180; private int moveY = 40; // 每张card上需要调整depth的组件数量 private int componetCount = 3; // 用于更换card图片的texture数组 private Texture[] cardTexture; private List<CardInfo> cardInfoList = new List<CardInfo> (); void Start () { // 加载图片数组 cardTexture = Resources.LoadAll<Texture>("Pictures"); // 最大索引 int maxIndex = cardTexture.Length / 2 + 1; // 中间卡牌数索引 (比如8张图,中间1张,左右一个4张,一个3张) // 所以这个单边卡牌的索引就是4+1 = 5 centerCardIndex = maxIndex; // 根据图片数组长度生成Card模板预制体 // 遍历Resources加载到的所有图片 for (int i = 0; i < cardTexture.Length; i++) { // 加载人物卡牌图片预设体(注意不能加载到UIRoot的外面,那样会自动创建多个UICamera) GameObject goCardPrefab = Instantiate(Resources.Load<GameObject>("Card")); // 设置card的父物体 goCardPrefab.transform.parent = cardManager.transform; // 设置card的大小 goCardPrefab.transform.localScale = Vector3.one; // 给toggle添加监听事件 EventDelegate.Add(goCardPrefab.GetComponent<UIToggle>().onChange,OnToggleValueChange); // item就是一个保存人物卡片上三张图片深度数据的类脚本 CardInfo item = goCardPrefab.GetComponent<CardInfo>(); // 获取生成的预制体卡牌的一个texture属性,赋值为当前自己所在的预制体身上的图片 // 注意:赋值之前,要保证这个mainTexture原本就是有图的 item.cardTexture.mainTexture = cardTexture[i]; // 设置人物卡牌的命名排序规则 // 当前生成的预制体名字为默认 最大索引 既 居中那张的数字 // 默认加载的第0张图片是居中的,加载顺序与文件夹中图片的顺序无关 goCardPrefab.name = maxIndex.ToString(); // 判断生成物体的编号 if (i > 0) { // 奇数设置为右边,下标为正数 if (i % 2 == 1) { maxIndex--; goCardPrefab.name = maxIndex.ToString(); } // 偶数设置为左边,下标为负数 else { // 这里深度不需要再减的原因是已经在奇数位减过了 // 比如9张牌,居中为5,先奇数减一变成4,那么下一次就是偶数,应该是-4了 goCardPrefab.name = "-" + maxIndex.ToString(); } } // 初始化人物卡牌的位置 // 调用设置人物卡牌的深度以及位置 SetPositionAndDepth(item, 0, 0); cardInfoList.Add(item); } } // Update is called once per frame void Update () { } // direction表示移动方向 // 1:左移 -1:右移 0:初始化(不移动) void SetPositionAndDepth(CardInfo cardInfo, int direction, int nextIndex) { int nameIndex = 0; if (direction == 0) { nameIndex = int.Parse(cardInfo.name); } // 左右移动后,重新排序命名 // 第一次初始化的时候并不调用 if (direction != 0) { // nameIndex通过当前名字转换过来的 // 最中间位置的编号变化,右移:原-4变5;左移:原5变-4 // 正常情况下,左移,所有数都会+1 // 但中间那张牌不是,需要减一,再乘一个负数 if (nextIndex * direction > centerCardIndex) { nameIndex = -direction * (centerCardIndex - 1); } else { // index=0既,左面的-1要到右面变为+1 或者右面的 +1 要移动到左面变成-1 // 但是这个需要一个判断,判断一共有多少张卡牌,如果是偶数的情况下,好吧,你画张图就知道了~ if (nextIndex == 0) { nameIndex = cardInfoList.Count % 2 == 1 ? direction : direction * 2; } else { // 移动后,索引变成nextIndex,符合条件,可以赋值 nameIndex = nextIndex; } } // 发生了移动,改变了索引,需要更改名字 cardInfo.name = nameIndex.ToString(); } // 获取当前卡牌的tween组件,设置to坐标 TweenPosition tp = cardInfo.GetComponent<TweenPosition>(); // 根据当前卡牌名字的正负,来判断是左移还是右移 int x = nameIndex > 0 ? (centerCardIndex - nameIndex) * moveX :-(centerCardIndex + nameIndex) * moveX; // 因为Y值都是向上,所以需要一个正数判断Y的位移 nameIndex = Mathf.Abs(nameIndex); tp.to = new Vector3(x,(centerCardIndex - nameIndex) * moveY, 0); // 设置当前移动卡牌的3张图片的深度 for (int i = 0; i < cardInfo.widgets.Length; i++) { cardInfo.widgets[i].depth = nameIndex * componetCount + i; } // 如果当前移动的卡牌的index是centercardindex // 将toggle设置为选中状态 cardInfo.GetComponent<UIToggle>().value = nameIndex == centerCardIndex ? true : false; // 向前播放动画 tp.PlayForward(); } // 当点击向左旋转的按钮时 void OnClickLeftButton() { foreach (CardInfo item in cardInfoList) { int nowIndex = int.Parse(item.name); SetPositionAndDepth(item, 1, ++nowIndex); } } // 当点击向右旋转的按钮时 void OnClickRightButton() { foreach (CardInfo item in cardInfoList) { int nowIndex = int.Parse(item.name); SetPositionAndDepth(item, -1, --nowIndex); } } void OnToggleValueChange() { if (UIToggle.current.value) { // 获取当前点击的toggle的物体名字 int index = int.Parse(UIToggle.current.name); // 需要移动的单位 int needMoveCount = centerCardIndex - Mathf.Abs(index); for (int i = 0; i < needMoveCount; i++) { if (index > 0) { OnClickLeftButton(); } else { OnClickRightButton(); } } } } }
相关文章推荐
- .net web api ioc unity usage
- Mac系统 unity VScode 开发
- unity3d 代码设置贴图Read/Write Enable属性
- 使用git对unity3d项目进行版本控制
- unity3D 实战 《小十传奇》系列之三:控制层(下)
- Unity官方实例教程 Roll-a-Ball(二)
- Unity官方实例教程 Space Shooter
- 从unity3d官网下载教程
- {Unity} iOS 9 字体的坑
- {Unity} Unity2D camera size和分辨率
- {Unity} Vector2输出log的坑
- unity3D 实现NGUI物体朝鼠标点击位置移动
- Unity基本原理
- Unity3d 引擎原理详细介绍
- Unity 3D主要特性和缺陷
- [Unity框架]PureMVC在unity中的简单使用
- 游戏存档
- 关于游戏中的分享之ShareSDK的研究
- unity3D制作血条跟随的研究
- Unity3D独立游戏开发日记(二):摆放建筑物