powerfun-unity/Assets/Scripts/Scenes/Ride/Competiton/CompetitionRankingItem.cs

124 lines
4.1 KiB
C#

using Assets.Scripts;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace Assets.Scenes.Ride.Scripts
{
public class CompetitionRankingItem : MonoBehaviour
{
private RawImage Head;
private RawImage Country;
private Image Background;
private Text Name;
private Text Speed;
private Text Distance;
private Text Ratio;//功体比
private Text Rank;//当前排名
private Text Timer;
private int _userId = 0;
public int UserId { get { return _userId; } }
private string _headUrl=string.Empty;
private string _countryUrl = string.Empty;
private string _name = string.Empty;
private string _speed = string.Empty;
private string _distance = string.Empty;
private string _ratio = string.Empty;
public int rank { get; set; }
private Texture countryTexture;
CyclingController cyclingController;
Color selectedColor = new Color(0.9764706f, 0.1882353f, 0.5254902f);
Color unselectedColor = new Color(0.2078431f, 0.2078431f, 0.2627451f);
private void Awake()
{
cyclingController = FindObjectOfType<CyclingController>();
Background = transform.GetComponent<Image>();
Head = transform.Find("Head").GetComponent<RawImage>();
Country = transform.Find("Country").GetComponent<RawImage>();
Name = transform.Find("Name").GetComponent<Text>();
Speed = transform.Find("Speed").GetComponent<Text>();
Distance = transform.Find("Distance").GetComponent<Text>();
Ratio = transform.Find("Ratio").GetComponent<Text>();
Rank = transform.Find("Rank").GetComponent<Text>();
Timer = transform.Find("Timer")?.GetComponent<Text>();
countryTexture = Resources.Load<Sprite>("Images/flag_China_Person").texture;
UIManager.AddEvent(this.gameObject, UnityEngine.EventSystems.EventTriggerType.PointerClick, Watch);
}
private void Update()
{
if (cyclingController.currentPlayer.UserId == UserId)
{
Background.color = selectedColor;
Ratio.color = Color.white;
}
else
{
Background.color = unselectedColor;
}
}
//切换到当前用户视角
public void Watch(BaseEventData baseEventData)
{
cyclingController.ChangeCurrentPlayer(_userId);
}
public void setTimer(string timer)
{
Timer.text = timer;
}
public void setRatio(string ratio)
{
if (!_ratio.Equals(ratio))
{
Ratio.text = ratio;
}
}
//设置当前排名
public void SetRank(int rank)
{
Rank.text = rank.ToString().PadLeft(3,'0');
this.rank = rank;
}
public void setHead(string url)
{
if (!string.IsNullOrEmpty(url))
{
Utils.DisplayImage(Head, url, true);
var rect = ((RectTransform)Head.transform).rect;
Material material = Instantiate(Resources.Load<Material>("UI/Material/RoundedCornersTextureMaterial"));
material.SetVector(Shader.PropertyToID("_WidthHeightRadius"), new Vector4(rect.width, rect.height, rect.height, 0));
Head.material = material;
}
}
public void setCountry(string url)
{
Utils.DisplayImage(Country, url, true);
}
public void setName(string name)
{
if (!_name.Equals(name))
Name.text = name;
}
public void setSpeed(string speed)
{
if (!_speed.Equals(speed))
Speed.text = speed;
}
public void setDistance(string distance)
{
if (!_distance.Equals(distance))
Distance.text = distance;
}
public void setUserId(int userId)
{
_userId = userId;
}
}
}