using UnityEngine.UI;
using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
using Photon.Pun;
using Photon.Realtime;
public class Roll : MonoBehaviourPunCallbacks, IPunObservable
{
[Header("实例化参数")]
public GameObject _NamePrefab; //名字预制体
public Transform _Content; //存放Name的父物体
//public string[] _NameArray;
public List<string> _NameArray;
string[] TestNameArray;
[Header("滚动参数")]
public float Speed = 3f;
//想要显示的个数
public int _MaxContent;
[HideInInspector]
public float _MaxPositionY;
[HideInInspector]
public bool IsRoll = true;
//展示名字
public RectTransform _ShowName;
//中奖人名字
string _WinName = "";
// 抽奖按钮
public Button _DrowBtn = null;
bool IsDrop = false; //是否开始抽奖
public List<string> WinList;//中奖名单
[Header("结果显示面板")]
public Animator animResult;
public Button onceMore; //再抽一次
CanvasGroup canvasGroup;
//[Header("测试")]
//public GameObject obj;
private void Awake()
{
_NameArray = new List<string>();
string[] TestNameArray = new string[]
{
"钮兴德", "闽惜文", "段树", "淡绿凝", "归笑阳", "留昆", "黎慕山", "独小翠", "洛爰",
"旅凝海", "姜绍钧", "机宏畅", "悉天音", "和玉龙","朋航","望昊苍","靖又","国歌","廖俊贤",
"错涵阳"
};
_NameArray.AddRange(TestNameArray);
//存放数组对象
for (int i = 0; i < _NameArray.Count; i++)
{
_NamePrefab.transform.GetChild(0).GetComponent<Text>().text = _NameArray[i];
//实例化名字
GameObject _Name = Instantiate(_NamePrefab, _Content);
_Name.name = i.ToString();
}
//初始化
WinList = new List<string>();
canvasGroup = animResult.GetComponent<CanvasGroup>();
}
void Start()
{
animResult.gameObject.SetActive(false);
if (transform.GetComponent<GridLayoutGroup>() != null)
{
_MaxPositionY = transform.GetComponent<GridLayoutGroup>().cellSize.y * 0.5f + transform.GetComponent<GridLayoutGroup>().spacing.y;
}
if (transform.GetComponent<VerticalLayoutGroup>() != null)
{
_MaxPositionY = transform.GetComponent<VerticalLayoutGroup>().spacing + transform.GetChild(0).GetComponent<RectTransform>().rect.height * 0.5f;
}
SetActive();
_DrowBtn.onClick.AddListener(DrawFun);
//再抽一次
onceMore.onClick.AddListener(() => {
animResult.SetBool("IsShow", false);
StartCoroutine(HideResult());
Speed = 3;
});
}
//自定义同步
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.IsWriting)
{
stream.SendNext(Speed); //滚动速度
stream.SendNext(animResult.gameObject.activeInHierarchy); //结局面板的显隐
stream.SendNext(animResult.transform.GetChild(0).GetComponent<Text>().text); //结局文字显示
}
else
{
this.Speed = (float)stream.ReceiveNext();
this.animResult.gameObject.SetActive((bool)stream.ReceiveNext());
this.animResult.GetComponentInChildren<Text>().text = (string)stream.ReceiveNext();
}
}
//再抽一次
public void OnceMore()
{
animResult.SetBool("IsShow", false);
StartCoroutine(HideResult());
Speed = 3;
}
//点击抽奖
public void DrawFun()
{
if (!animResult.gameObject.activeInHierarchy)
{
GetComponent<PhotonView>().TransferOwnership(PhotonNetwork.LocalPlayer);
animResult.GetComponent<PhotonView>().TransferOwnership(PhotonNetwork.LocalPlayer);
_Content.GetComponent<PhotonView>().TransferOwnership(PhotonNetwork.LocalPlayer);
if (WinList.Count == _NameArray.Count)
{
animResult.gameObject.SetActive(true);
animResult.SetBool("IsShow", true);
animResult.GetComponentInChildren<Text>().text = "所有人都拿到过奖,请勿重复!!";
//中断协程
StopCoroutine(ReSelect());
}
//开始抽奖
if (!IsDrop)
{
print("开始抽奖!!");
Speed = 50f;
IsDrop = true;
}
else
{
_WinName = _ShowName.GetComponentInChildren<Text>().text;
StartCoroutine(ReSelect());
}
}
else
{
OnceMore();
}
}
IEnumerator ReSelect()
{
while (WinList.Contains(_WinName))
{
//等一下继续滚动,每0.01秒检查一下是否拿到了没获过奖的名字
yield return new WaitForSeconds(0.01f);
_WinName = _ShowName.GetComponentInChildren<Text>().text;
}
//中奖!!
Speed = 0f;
print("停!!");
IsDrop = false;
WinList.Add(_WinName);
print("恭喜 " + _WinName + " 中奖!!");
//显示结果页面
animResult.gameObject.SetActive(true);
animResult.SetBool("IsShow", true);
animResult.transform.GetChild(0).GetComponent<Text>().text = "恭喜 " + _WinName + " 获奖!!";
}
int middle;
RectTransform rectMiddle;
void FixedUpdate()
{
if (transform.childCount > _MaxContent)
{
if (IsRoll)
{
middle = Mathf.FloorToInt(_MaxContent * 0.5f);
for (int i = 0; i < _MaxContent + 1; i++)
{
transform.GetChild(i).transform.localPosition += new Vector3(0, Speed, 0);
if (transform.GetChild(i).transform.localPosition.y > _MaxPositionY)
{
transform.GetChild(i).SetAsLastSibling();
if (transform.childCount > _MaxContent + 1)
{
//隐藏最后一个
transform.GetChild(transform.childCount - 1).gameObject.SetActive(false);
//显示最大取值数
transform.GetChild(_MaxContent + 1).gameObject.SetActive(true);
}
}
_ShowName.GetComponentInChildren<Text>().text = transform.GetChild(Mathf.FloorToInt(_MaxContent * 0.5f)).GetComponentInChildren<Text>().text;
}
}
}
//if (Input.GetAxis("Mouse ScrollWheel") > 0)//往下滚动
//{
// IsRoll = false;
// transform.GetChild(transform.childCount - 1).SetAsFirstSibling();
// StopCoroutine("wait");
// StartCoroutine("wait");
//}
//if (Input.GetAxis("Mouse ScrollWheel") == 0)
//{
// RealSpeed = Speed;
//}
//if (Input.GetAxis("Mouse ScrollWheel") < 0)//向上滚动
//{
// RealSpeed = UpSpeed;
// IsRoll = true;
// StopCoroutine("wait");
//}
}
//显示可视范围内的名字
//隐藏掉结果页面
IEnumerator HideResult()
{
yield return new WaitForSeconds(0.3f);
animResult.gameObject.SetActive(false);
}
void SetActive()
{
for (int i = 0; i < transform.childCount; i++)
{
if (i > _MaxContent + 1)
{
transform.GetChild(i).gameObject.SetActive(false);
}
}
}
IEnumerator wait()
{
yield return new WaitForSeconds(1);
IsRoll = true;
yield break;
}
}