现在我们学学怎么用ngui 做卡牌特效,可能刚开始接触有点困难。但是很多东西都是越学越简单。
写了两个卡牌特效。有三种效果。一种是横着的。点击相应卡牌。将点击拍片置顶。另外一种是竖像的。点击卡片。突出并置顶。有移动效果。另外一个为发牌特效。看工程里。
如下图所示:

横向:




竖向:

如下图,修改为竖向。只需将该标志量设置为true

好了上代码:
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. public class SceneLuckyCard : MonoBehaviour
  5. {
  6. #region 数据定义
  7. private List<string> mNameList = new List<string>();
  8. private GameObject mItem;
  9. private UIPanel mMovePanel;
  10. private int CardEffectOffset = 80;
  11. public List<GameObject> cardList;
  12. private int indexCard;//当前选中卡--
  13. private int cardCount;
  14. private int maxDepth = 50;//最大深度--
  15. private Vector3 indexPosition;
  16. public float timeSpan = 0.2f;
  17. public bool isIEnum = true;
  18. private List<Vector3> posList;
  19. public Vector2 offsetCard = new Vector2(20f, 100f);//偏移--
  20. /// <summary> 是否竖向 </summary>
  21. public bool direction = false;
  22. #endregion
  23. // Use this for initialization
  24. void Start ()
  25. {
  26. mItem = PublicFunction.GetComponentByLocalPath<Transform>(this.gameObject, "PanelMove/Items/Item").gameObject;
  27. mMovePanel = PublicFunction.GetComponentByLocalPath<UIPanel>(this.gameObject, "PanelMove");
  28. mNameList.Add("德玛西亚"); mNameList.Add("皮城女警"); mNameList.Add("影流之主"); mNameList.Add("末日浩劫"); mNameList.Add("荒野屠夫"); mNameList.Add("刀锋之影"); mNameList.Add("提莫对账"); mNameList.Add("麦林炮手"); mNameList.Add("荒野屠夫"); mNameList.Add("刀锋之影");
  29. StartCoroutine(CloneItem(mNameList.Count));
  30. Debug.LogError(mNameList.Count);
  31. indexCard = mNameList.Count - 1;
  32. cardCount = mNameList.Count;
  33. }
  34. IEnumerator CloneItem(int num)
  35. {
  36. if (num >= 9)
  37. {
  38. CardEffectOffset -= 15;
  39. }
  40. for (int i = 0; i < num; i++)
  41. {
  42. GameObject item = Instantiate(mItem) as GameObject;
  43. item.transform.parent = mItem.transform.parent;
  44. item.transform.localEulerAngles = Vector3.zero;
  45. item.transform.localScale = Vector3.one;
  46. item.transform.localPosition = new Vector3(-900,0,0);
  47. item.name = i.ToString();
  48. cardList.Add(item);
  49. UIEventListener listener = UIEventListener.Get(item);
  50. listener.onClick = OnClickCard;
  51. yield return new WaitForSeconds(0.2f) ;
  52. FlightEffect(i,item);
  53. }
  54. if (posList != null && posList.Count > 0) posList.Clear();
  55. else posList = new List<Vector3>();
  56. foreach (GameObject go in cardList)
  57. {
  58. posList.Add(go.transform.localPosition);
  59. }
  60. }
  61. void FlightEffect(int index,GameObject item)
  62. {
  63. TweenPosition tp = item.AddComponent<TweenPosition>();
  64. tp.from = item.transform.localPosition;
  65. tp.to = new Vector3(300 - (CardEffectOffset * index), item.transform.localPosition.y, 0);
  66. tp.duration = 0.2f;
  67. UIWidget[] tran = item.GetComponentsInChildren<UIWidget>(true);
  68. foreach (UIWidget tra in tran)
  69. {
  70. tra.depth += (index*5);
  71. }
  72. }
  73. void OnClickCard(GameObject go)
  74. {
  75. int tempindex = 0;
  76. for (int i = 0; i < cardList.Count; i++)
  77. {
  78. if (cardList[i].name == go.name)
  79. {
  80. tempindex = i; break;
  81. }
  82. }
  83. if (tempindex != indexCard)
  84. {
  85. //cardList[indexCard].GetComponent<UIWidget>().color = Color.grey;
  86. indexCard = tempindex;
  87. UIWidget uw = go.GetComponent<UIWidget>();
  88. uw.depth = maxDepth;
  89. uw.color = Color.white;
  90. indexPosition = new Vector3(0, go.transform.localPosition.y, 0);
  91. if (direction)
  92. TweenPosition.Begin(go, timeSpan, indexPosition);
  93. StartCoroutine(SetCardOrder(timeSpan / 2f, isIEnum, direction));
  94. SetDepth(uw.depth, go);
  95. }
  96. }
  97. public void SetCardOrder()
  98. {
  99. //StartCoroutine(SetCardOrder(0.01f, false));
  100. }
  101. public IEnumerator SetCardOrder(float span, bool isienum, bool erect)
  102. {
  103. int tempDepth = maxDepth;
  104. if (indexCard > 0)
  105. {
  106. for (int i = indexCard; i > 0; --i)
  107. {
  108. Vector3 tempPos = indexPosition;
  109. if (i != indexCard) { tempPos = posList[i]; }
  110. GameObject go = cardList[i - 1];
  111. if (erect)//如果竖立。则有移动效果
  112. {
  113. posList[i - 1] = new Vector3(tempPos.x + offsetCard.x, tempPos.y + offsetCard.y, tempPos.z);
  114. TweenPosition.Begin(go, timeSpan, posList[i - 1]);
  115. }
  116. UIWidget uw = go.GetComponent<UIWidget>();
  117. if (uw != null)
  118. {
  119. tempDepth -= 5;
  120. uw.depth = tempDepth;
  121. }
  122. SetDepth(uw.depth, go);
  123. //if (isienum) yield return new WaitForSeconds(span);
  124. }
  125. tempDepth = maxDepth;
  126. }
  127. if (indexCard < cardList.Count - 1)
  128. {
  129. for (int i = indexCard; i < cardCount - 1; ++i)
  130. {
  131. Vector3 tempPos = indexPosition;
  132. if (i != indexCard) { tempPos = posList[i]; }
  133. GameObject go = cardList[i + 1];
  134. if (erect)//如果竖立。则有移动效果
  135. {
  136. posList[i + 1] = new Vector3(tempPos.x + offsetCard.x, tempPos.y - offsetCard.y, tempPos.z);
  137. TweenPosition.Begin(go, timeSpan, posList[i + 1]);
  138. }
  139. UIWidget uw = go.GetComponent<UIWidget>();
  140. if (uw != null)
  141. {
  142. tempDepth -= 5;
  143. uw.depth = tempDepth;
  144. }
  145. SetDepth(uw.depth, go);
  146. //if (isienum) yield return new WaitForSeconds(span);
  147. }
  148. }
  149. yield return 1;
  150. }
  151. void SetDepth(int parentDepth,GameObject obj)
  152. {
  153. UIWidget[] tran = obj.GetComponentsInChildren<UIWidget>(true);
  154. PublicFunction.GetComponentByLocalPath<UILabel>(obj, "Label").depth = parentDepth +1;
  155. PublicFunction.GetComponentByLocalPath<UISprite>(obj, "Time").depth = parentDepth + 1;
  156. PublicFunction.GetComponentByLocalPath<UILabel>(obj, "Time/Label").depth = parentDepth + 1;
  157. }
  158. }