Unity:2019.4.36
VS:2019
1、总体预览
2、Scroll View设置
3、创建一个名叫StreamingAssets文件夹,在里面创建一个TestJson.json文件,用来存储数据
4、如需打包的话,需要将.Net改为4.0
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using UnityEngine.UI;
using System.IO;
using Newtonsoft.Json;
public class GameControl : MonoBehaviour
{
[SerializeField] //强制序列化
private List<string> person = new List<string>(); //人物列表
[SerializeField]
private RectTransform rotateObj; //旋转的物体
public Button startBtn; //开始按钮
private int personNums; //人物的总数
private int personAngle; //每个人物所对应的角度
private string filePath; //json文件的路径
public List<Data> datas = new List<Data>();
private RectTransform ContentTrans;
public GameObject prefab;
private string CurrentTime; //当前时间
private float[,] TextColors = new float[6, 3] //创建一个二维数组,用来存放每个字体的颜色
{
{60,141,250},
{241,102,151},
{203,163,0},
{63,199,255},
{236,69,63},
{55,218,85}
};
public Button ClearBtn; //一键清空按钮
// Start is called before the first frame update
void Start()
{
personNums = person.Count;
personAngle = 360 / personNums;
startBtn.onClick.AddListener(OnStartBtnClick); //给按钮绑定事件
filePath = Application.streamingAssetsPath + "/TestJson.json";
ContentTrans = GameObject.Find("Canvas").transform.GetChild(1).GetChild(0).GetChild(0).GetComponent<RectTransform>();
DataInit();
ClearBtn.onClick.AddListener(OnClearBtnClick);
}
private void OnClearBtnClick()
{
for (int i = 0; i < ContentTrans.childCount; i++)
{
Destroy(ContentTrans.GetChild(i).gameObject);
}
ContentTrans.sizeDelta = new Vector2(0, 120);
string jsonData = File.ReadAllText(filePath);
datas = JsonConvert.DeserializeObject<List<Data>>(jsonData);
datas.Clear();
string dataInfo = JsonConvert.SerializeObject(datas, Formatting.Indented);
File.WriteAllText(filePath, dataInfo);
}
public void OnStartBtnClick()
{
startBtn.interactable = false; //禁用按钮
int index = Random.Range(0, personNums); //随机获取的人物
int rotateNums = Random.Range(10, 20) * 360; //随机旋转的圈数
//使用Dotween中的函数,在6秒内速度由慢到快再到慢
rotateObj.DOLocalRotate(new Vector3(0, 0, rotateNums + index * personAngle), 6, RotateMode.FastBeyond360).SetEase(Ease.InOutCirc).OnComplete(() =>
{
CurrentTime = System.DateTime.Now.Year + "-" + string.Format("{0:00}", System.DateTime.Now.Month) + "-"
+ string.Format("{0:00}", System.DateTime.Now.Day) + " " + string.Format("{0:00}", System.DateTime.Now.Hour)
+ ":" + string.Format("{0:00}", System.DateTime.Now.Minute) + ":" + string.Format("{0:00}", System.DateTime.Now.Second);
Data data = new Data();
data.time = CurrentTime;
data.people = person[index];
datas.Add(data);
AddList(data);
string dataInfo = JsonConvert.SerializeObject(datas, Formatting.Indented); //序列化
File.WriteAllText(filePath, dataInfo); //写入文件
});
Invoke("actStartBtn", 6); //延迟6秒调用函数
}
private void actStartBtn()
{
startBtn.interactable = true; //激活按钮
}
private void DataInit() //数据初始化
{
string jsonData = File.ReadAllText(filePath); //读取文件中所有内容
if (jsonData == "[]") //判断读取的数据是否为空,
{
return;
}
datas = JsonConvert.DeserializeObject<List<Data>>(jsonData); //反序列化
foreach (Data data in datas)
{
AddList(data);
}
}
private void AddList(Data data) //创建Text组件
{
int i = 0;
if (ContentTrans.childCount > 3)
{
ContentTrans.sizeDelta = new Vector2(0, ContentTrans.rect.height + 30);
}
GameObject t = Instantiate(prefab); //实例化
t.transform.SetParent(ContentTrans, false); //给刚刚实例化的物体设置父物体
t.transform.SetSiblingIndex(0); //设置实例化物体的索引位置,让实例化的物体一直处在第一个
t.GetComponent<Text>().text = data.time + " 恭喜您获得" + data.people + "!";
foreach(string s in person) //循环遍历列表,判断当前人物的元素位置
{
if(s==data.people)
{
break;
}
i++;
}
t.GetComponent<Text>().color = new Color(TextColors[i, 0] / 255, TextColors[i, 1] / 255, TextColors[i, 2] / 255); //改变字体的颜色
}
public class Data
{
public string time;
public string people;
}
}
链接:https://pan.baidu.com/s/1QEiqoNtWMUYAEVS0vArKbQ
提取码:d6xt