【Unity开发】Unity实现英雄联盟选择皮肤效果

来源:互联网 发布:项目经理考试软件 编辑:程序博客网 时间:2024/04/28 11:01
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 = 5centerCardIndex = 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 framevoid 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();}}}}}

0 0
原创粉丝点击