下面是具体的代码,注意,我这里用到了NGUI这个插件。大概的原理很简单,就用了2张图片Sprite,同时向下移动,当上面一张图片移动到下面一张图片原来的位置时,就把这2张图片向上移到初始的位置。这时,还需要交换显示图片,接着继续移动即可。为了显示很多滚动图片,只需要不断改变图片名字即可。代码中,抽奖部分使用了概率控制的随机抽取。当我们确定了rollingCountTarget这个变量时,就确定了最终抽奖结果,在Update函数里判断,如果遇到这个结果便停下来。当然,速度也要控制好。如果你用NGUI的话,只需要给脚本绑定两个Sprite图片即可,同时把Panel对象的Clipping设为Soft Clip,设定好显示范围即可。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
public UISprite Icons;//中间的图片
public UISprite IconsA;//上方的图片
private float rollSpeed;//滚动速度变量
private int rollingCount;//图片滚动次数变量
private int rollingCountTarget;//目标图片滚动次数
private Vector3 originPos;//图片原始位置
private Vector3 originPosA;//上方图片原始位置
private float rollCount;//总的滚动次数
private string[] LotteryImageNames;//图片名字数组
private bool IsRolling;
public const int LotteryItemTypeCount = 7;//可以抽到的物品类型数目
private const float maxRollSpeed = 2.0f;//最大滚动速度
private const float minRollSpeed = 0.4f;//最小滚动速度
void Start()
{
rollCount = 3;
IsRolling = false;
//初始化图片原始位置
originPos = Icons.transform.position;
originPosA = IconsA.transform.position;
//先不显示图片
Icons.enabled = false;
IconsA.enabled = false;
//初始化要滚动的图片名字数组
LotteryImageNames = new string[LotteryItemTypeCount];
LotteryImageNames[0] = "Joey_Equipment01";
LotteryImageNames[1] = "Joey_Equipment02";
LotteryImageNames[2] = "Joey_Equipment03";
LotteryImageNames[3] = "Joey_Equipment04";
LotteryImageNames[4] = "Joey_Equipment05";
LotteryImageNames[5] = "Joey_Equipment06";
LotteryImageNames[6] = "Joey_Equipment01";
}
void Update()
{
if (IsRolling)
{
Icons.transform.Translate(Vector3.down * Time.deltaTime * rollSpeed);
IconsA.transform.Translate(Vector3.down * Time.deltaTime * rollSpeed);
if (IconsA.transform.position.y <= originPos.y)
{
Icons.transform.position = new Vector3(Icons.transform.position.x,
originPos.y, Icons.transform.position.z);
IconsA.transform.position = new Vector3(IconsA.transform.position.x,
originPosA.y, IconsA.transform.position.z);
rollingCount++;
//互换图片
Icons.spriteName = IconsA.spriteName;
IconsA.spriteName = LotteryImageNames[(rollingCount + 1) % LotteryItemTypeCount];
rollSpeed = rollSpeed - (maxRollSpeed - minRollSpeed) / (rollingCountTarget +
LotteryItemTypeCount * rollCount);
}
if (rollingCount == rollingCountTarget + LotteryItemTypeCount * rollCount)
{
IsRolling = false;
IconsA.enabled = false;
}
}
}
//用随机数确定图片需要滚动的次数,同时控制随机概率
public void GenerateRollingCount()
{
int num = Random.Range(0, 99);
if (num >= 0 && num < 20)
{
rollingCountTarget = 0;
}
else if (num >= 20 && num < 35)
{
rollingCountTarget = 1;
}
else if (num >= 35 && num < 45)
{
rollingCountTarget = 2;
}
else if (num >= 45 && num < 55)
{
rollingCountTarget = 3;
}
else if (num >= 55 && num < 65)
{
rollingCountTarget = 4;
}
else if (num >= 65 && num < 85)
{
rollingCountTarget = 5;
}
else if (num >= 85 && num < 100)
{
rollingCountTarget = 6;
}
}
//抽奖按钮
void Lottery()
{
GenerateRollingCount();
rollingCount = 0;
rollSpeed = maxRollSpeed;
IsRolling = true;
Icons.enabled = true;
IconsA.enabled = true;
}
|