104 lines
3.0 KiB
C#
104 lines
3.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace MMO
|
|
{
|
|
public static class MMOUtils
|
|
{
|
|
public static Sprite LoadTaskIcon(MMOTaskType pType)
|
|
{
|
|
string tPath = "";
|
|
|
|
switch (pType)
|
|
{
|
|
case MMOTaskType.GetDiamond: tPath = "MMOImage/TaskIcon/Robux"; break;
|
|
case MMOTaskType.PassLevel:
|
|
case MMOTaskType.DailyLevel: tPath = "MMOImage/TaskIcon/Level"; break;
|
|
case MMOTaskType.WatchRV:
|
|
case MMOTaskType.DailyAds: tPath = "MMOImage/TaskIcon/WatchAd"; break;
|
|
case MMOTaskType.RiseRank: tPath = "MMOImage/TaskIcon/Rank"; break;
|
|
}
|
|
|
|
return Resources.Load<Sprite>(tPath);
|
|
}
|
|
|
|
public static string GetMaskedID(string pID)
|
|
{
|
|
string tMaskedID = pID;
|
|
if (tMaskedID.Length >= 2)
|
|
{
|
|
tMaskedID = tMaskedID.Replace(tMaskedID.Substring(1, Mathf.Min(3, tMaskedID.Length - 1)), "***");
|
|
}
|
|
|
|
return tMaskedID;
|
|
}
|
|
|
|
public static T RandomPick<T>(Dictionary<T, float> pDataDic)
|
|
{
|
|
if (pDataDic == null || pDataDic.Count == 0)
|
|
{
|
|
return default(T);
|
|
}
|
|
|
|
Dictionary<T, Vector2> tRandomDic = new Dictionary<T, Vector2>();
|
|
float tBottomLine = 0;
|
|
float tTopLine = 0;
|
|
foreach (T tKey in pDataDic.Keys)
|
|
{
|
|
tTopLine += pDataDic[tKey];
|
|
tRandomDic.Add(tKey, new Vector2(tBottomLine, tTopLine));
|
|
tBottomLine = tTopLine;
|
|
//Debug.Log(string.Format("{0}:{1}", tKey.ToString(), tRandomDic[tKey].ToString()));
|
|
}
|
|
|
|
T tPickedEle = default(T);
|
|
float tRandomNum = Random.Range(0, tTopLine);
|
|
|
|
foreach (T tKey in tRandomDic.Keys)
|
|
{
|
|
if (tRandomNum > tRandomDic[tKey].x && tRandomNum <= tRandomDic[tKey].y)
|
|
{
|
|
tPickedEle = tKey;
|
|
break;
|
|
}
|
|
}
|
|
|
|
return tPickedEle;
|
|
}
|
|
|
|
#region UI Grey
|
|
private static Material grayMat;
|
|
private static Material GetGrayMat()
|
|
{
|
|
if (grayMat == null)
|
|
{
|
|
Shader shader = Shader.Find("Custom/UI-Gray");
|
|
if (shader == null)
|
|
{
|
|
Debug.Log("UI-Gray shader null");
|
|
return null;
|
|
}
|
|
Material mat = new Material(shader);
|
|
grayMat = mat;
|
|
}
|
|
|
|
return grayMat;
|
|
}
|
|
|
|
public static void SetImageGray(Image pImg, bool pGray)
|
|
{
|
|
if (pGray)
|
|
{
|
|
pImg.material = GetGrayMat();
|
|
pImg.SetMaterialDirty();
|
|
}
|
|
else
|
|
{
|
|
pImg.material = null;
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
} |