今天练习的小游戏是辛运大转盘,主要内容:控制转盘或者指针转动,停止时弹出奖品。这里涉及xml数据的读取。
一、LuckRoller 转盘转动:
using UnityEngine;
using System.Collections;
using DoozyUI;
using UnityEngine.UI;
public class LuckRoller : UIPanel {
//public GameObject obj;
//幸运转盘
private Transform mRoolPanel;
public GameObject obja;
//指针
// private Transform Pointer;
//初始旋转速度
private float mInitSpeed;
//速度变化值
private float mDelta = 0.5f;
//转盘是否暂停
private bool isPause = true;
protected override void InitData()
{
//
}
protected override void InitEvent()
{
//
}
void Start()
{
//获取转盘
// mRoolPanel = this.transform.Find("Background");
//获取指针
// Pointer = this.transform.FindChild("Pointer");
}
//开始抽奖
public void OnClick()
{
if (isPause)
{
//随机生成一个初始速度
mInitSpeed = Random.Range(100, 500);
//开始旋转
isPause = false;
}
}
void Update()
{
if (!isPause)
{
//转动转盘(-1为顺时针,1为逆时针)
obja.transform.Rotate(new Vector3(0, 0, -1) * mInitSpeed * Time.deltaTime);
//转动指针(-1为顺时针,1为逆时针)
//Pointer.Rotate(new Vector3(0, 0, -1) * mInitSpeed * Time.deltaTime);
//让转动的速度缓缓降低
mInitSpeed -= mDelta;
//当转动的速度为0时转盘停止转动
if (mInitSpeed <= 0)
{
//转动停止
isPause = true;
UpdateData();
}
}
}
void UpdateData()
{
float angle = obja.transform.eulerAngles.z;
int i = (int)(angle / 45) + 1;
UnityEngine.Object notifyPanel = Resources.Load("Prefabs/NotificationPanel");
GameObject obj = GameObject.Instantiate(notifyPanel) as GameObject;
obj.transform.parent = GameObject.Find("LuckRoll").transform;
obj.transform.localPosition = Vector3.zero;
obj.transform.localScale = Vector3.one;
obj.transform.GetComponent<NotificationPanel>().SetPrePanel(gameObject, i);
}
}
二、LuckRollerDataManager 转盘奖品数据管理:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PlateData
{
public int Id;
public string Name;
public string Image;
}
public class LuckRollerDataManager : MonoBehaviour {
public static LuckRollerDataManager _instance;
public static LuckRollerDataManager Instance
{
get { return _instance; }
}
private List<PlateData> PlateItem = new List<PlateData>();
void Awake()
{
_instance = this;
FileUtil.loadConfig<List<PlateData>>("PlateAwardData", ref PlateItem, null);
}
public PlateData GetDataById(int id)
{
for (int i = 0; i < PlateItem.Count; i++)
{
if (PlateItem[i].Id == id)
{
return PlateItem[i];
}
}
return null;
}
}
三、PlateAwardData 奖品数据XML 表:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<ArrayOfPlateData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<PlateData>
<Id>1</Id>
<Name>苹果</Name>
<Image>PicA</Image>
</PlateData>
<PlateData>
<Id>2</Id>
<Name>草莓</Name>
<Image>PicB</Image>
</PlateData>
<PlateData>
<Id>3</Id>
<Name>香蕉</Name>
<Image>PicC</Image>
</PlateData>
<PlateData>
<Id>4</Id>
<Name>葡萄</Name>
<Image>PicD</Image>
</PlateData>
<PlateData>
<Id>5</Id>
<Name>菠萝</Name>
<Image>PicE</Image>
</PlateData>
<PlateData>
<Id>6</Id>
<Name>雪梨</Name>
<Image>PicF</Image>
</PlateData>
<PlateData>
<Id>7</Id>
<Name>柠檬</Name>
<Image>PicF</Image>
</PlateData>
<PlateData>
<Id>8</Id>
<Name>西瓜</Name>
<Image>PicF</Image>
</PlateData>
</ArrayOfPlateData>