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的地方
}
}
unity开发炉石传说系列卡牌生成代码部分代码
最新推荐文章于 2024-09-09 09:41:54 发布