unity開發爐石傳說系列卡牌生成程式碼部分程式碼
阿新 • • 發佈:2019-01-31
using UnityEngine; using System.Collections; using System.Collections.Generic; //生成卡牌 public class generatcard : MonoBehaviour { private Transform fromcard;//生成卡牌的地方 private Transform tocard;//生成卡牌後到達的地方 public GameObject generatedcontroller;//生成控制器 public GameObject gameprefab;//代表新生成的卡牌 public string[] names;//每張卡牌名字都不同,代表的是卡牌 private UISprite nowGeneratecard;//現在生成的卡 public int transformspeed = 20;//每秒改變20張牌 private float timer = 0;//計時器初始值 public float transformtime = 2.0f;//卡牌生成倒計時為時間是2秒 private bool isTransforming=false; // private List <GameObject> totalcards=new List<GameObject>(); void Awake() { } void Update() { if (Input.GetKeyDown(KeyCode.Space)) { Randomgeneratecards(); } if (isTransforming) timer += Time.deltaTime;//開始計時 int index = (int)(timer / (1f / transformspeed));//卡牌在生成前在不斷變化,變化間隔時間(頻率=時間/次數) index %= names.Length;//求餘數以免index長度超過names(names代表卡牌)的固定長度,這樣就可以迴圈了 nowGeneratecard.spriteName =names[index]; if (timer > transformtime)//如果計時器大於2秒了圖片就停止改變,並且計時器歸0 { isTransforming = false; timer=0; } } public void Randomgeneratecards()//隨機生成卡牌 { // GameObject go = GameObject.Instantiate(fromcard, fromcard.position, Quaternion.identity) as GameObject; GameObject go = NGUITools.AddChild(generatedcontroller, gameprefab);//把gamprfab放到generatedcontroller裡面然後賦值給go go.transform.position = fromcard.position; iTween.MoveTo(go ,tocard .position,1f);//把生成的go這個物體移動到tocard的地方 } }