using UnityEngine;
using System.Collections;
using System;
public class UITool
{
public static int RepositionByHeight(List<HeightItem> items,float offset_y = 2.0f)
{
if (items == null)
return 0;
float height = 0.0f;
float pos_y = 0.0f;
if (items.Count > 1)
{
pos_y = items [0].transform.localPosition.y;
}
for(int i = 1; i < items.Count ; i++)
{
float item_height = items[i - 1].getHeight() + offset_y;
Vector3 cur_pos = items[i].transform.localPosition;
pos_y -= item_height;
cur_pos.y = pos_y;
if(items[i].gameObject.activeSelf == true)
height += item_height;
items[i].transform.localPosition = cur_pos;
if(i == items.Count - 1)
{
height += items[i].getHeight();
}
}
return (int)height;
}
public static void SetToggle(UIToggle toggle, bool value)
{
if(toggle != null)
{
toggle.value = value;
}
}
public static bool IsActive(GameObject go)
{
if(go == null)
{
return false;
}
if(go.activeSelf == false || go.activeInHierarchy == false)
{
return false;
}
return true;
}
public static void setActive(GameObject go, bool active = true)
{
if(go != null && go.activeSelf != active)
{
go.SetActive(active);
}
}
public static void setActive(UIWidget widget, bool active = true)
{
if(widget != null && widget.gameObject.activeSelf != active)
{
widget.gameObject.SetActive(active);
}
}
public static void setActive(MonoBehaviour monobehavior, bool active = true)
{
if(monobehavior != null && monobehavior.gameObject.activeSelf != active)
{
monobehavior.gameObject.SetActive(active);
}
}
public static void setActive(ParticleSystem ps , bool active = true)
{
if(ps != null && ps.gameObject.activeSelf != active)
{
ps.gameObject.SetActive(active);
}
}
public static void GridReposition(UIGrid grid)
{
if(grid != null)
{
grid.Reposition();
}
}
public static void GridReposition(GameObject go)
{
if(go == null)
{
return;
}
GridReposition(go.GetComponent<UIGrid>());
}
public static void ScrollResetPosition(UIScrollView scroll)
{
if(scroll != null)
{
scroll.ResetPosition();
}
}
public static void ScrollResetPosition(GameObject go)
{
if(go == null)
{
return;
}
ScrollResetPosition(go.GetComponent<UIScrollView>());
}
public static void ClearLab(UILabel lab)
{
if(lab != null)
{
lab.Clear();
}
}
public static void ClearSprite(UISprite sprite)
{
if(sprite != null)
{
sprite.Clear();
}
}
public static void ClearUIButton(ref UIButton button)
{
if(button != null)
{
button.Clear();
}
button = null;
}
public static void ClearTexture(UITexture texture)
{
if (texture != null)
texture.mainTexture = null;
}
public static void ClearInput(UIInput input)
{
if(input != null)
{
input.value = string.Empty;
}
}
/// <summary>
/// Gets the lab text.
/// </summary>
/// <returns>The lab text.</returns>
/// <param name="lab">Lab.</param>
public static string GetLabText(UILabel lab)
{
if(lab != null)
{
return lab.text;
}
return "";
}
public static void SetSpriteWidth(UISprite sprite, int width)
{
if(sprite != null)
{
sprite.width = width;
}
}
public static void AddComponent(GameObject go, string component)
{
if(go != null)
{
go.AddComponent(component);
}
}
public static Color[] BoolColors = new Color[]{
new Color(0,255,0,255), new Color(255,0,0,255)
};
/// <summary>
/// true 绿色 false 红色
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static Color GetBoolColor(bool value)
{
return value ? BoolColors[0] / 255 : BoolColors[1] / 255;
}
/// <summary>
/// 清空GameObject列表
/// </summary>
/// <returns>The list.</returns>
/// <param name="list">List.</param>
public static void ClearList(List<GameObject> list)
{
if (list == null)
{
return;
}
for (int i = 0; i < list.Count; i++)
{
NGUITools.Destroy(list[i].gameObject);
}
list.Clear();
}
/// <summary>
/// Clears the grid.
/// </summary>
/// <returns>The grid.</returns>
/// <param name="grid">Grid.</param>
public static void ClearGrid(UIGrid grid)
{
if (grid == null)
return;
List<Transform> list = grid.GetChildList();
if (list.Count == 0)
return;
for (int i = 0, count = list.Count; i < count; i++)
{
Transform trans = list[i] as Transform;
NGUITools.Destroy(trans.gameObject);
}
}
/// <summary>
/// 毫秒转换为字符串
/// </summary>
/// <param name="msecTime"></param>
/// <returns></returns>
public static string FormatTimeByMsec(long msecTime, string s1 = ":", string s2 = ":")
{
return FormatTimeBySecond(msecTime / 1000);
}
/// <summary>
/// 秒转换为字符串
/// </summary>
/// <param name="secondTime"></param>
/// <returns></returns>
public static string FormatTimeBySecond(long secondTime, string s1 = ":", string s2 = ":")
{
int h = (int)(secondTime / 60 / 60);
int m = (int)(secondTime / 60) % 60;
int s = (int)(secondTime) % 60;
return String.Concat(h.ToString().PadLeft(2, '0'), s1, m.ToString().PadLeft(2, '0'), s2 + s.ToString().PadLeft(2, '0'));
}
/// <summary>
/// 数字转换为以万为单位 w
/// </summary>
/// <returns></returns>
public static String TransformWan(long num)
{
if (num < 10000)
{
return num.ToString();
}
float fNum = (float)num / 10000.0f;
return fNum.ToString("F1") + "w";
}
public static int GetDayOfWeekSecTime(TimeInfo time)
{
return GetDayOfWeekSecTime(time.m_Week, time.m_Hour, time.m_Minute, time.m_Second);
}
/// <summary>
/// DateTime转换成秒
/// </summary>
/// <returns>The day of week sec time.</returns>
/// <param name="time">Time.</param>
public static int GetDayOfWeekSecTime(ref DateTime time)
{
int week = (int)time.DayOfWeek;
return GetDayOfWeekSecTime(week, time.Hour, time.Minute, time.Second);
}
/// <summary>
/// 日期转换成秒
/// </summary>
/// <returns>The day of week sec time.</returns>
/// <param name="week">Week.</param>
/// <param name="hour">Hour.</param>
/// <param name="minute">Minute.</param>
/// <param name="second">Second.</param>
public static int GetDayOfWeekSecTime(int week, int hour = 0, int minute = 0, int second = 0)
{
return ((week * 24 + hour) * 60 + minute) * 60 + second;
}
/// <summary>
/// 秒转换成天数字符串
/// </summary>
/// <returns></returns>
public static string TransformDayBySecond(long secondTime)
{
string foreStr = "";
long days = (secondTime / 60 / 60 /24);
long weeks = (secondTime / 60 / 60 / 24 / 7);
long months = (secondTime / 60 / 60 / 24 / 7 / 4);
if (months > 0)
{
months = months > 12 ? 12 : months;
foreStr =months+"月前";
}
else if (weeks > 0)
{
foreStr = weeks +"周前";
}
else
{
if (days ==0)
{
foreStr = "今天";
}
else if(days==1)
{
foreStr = "昨天";
}else
{
foreStr = (int)days + "天前";
}
}
long leftSceondTime = secondTime - days*24*60*60;
int h = (int)(leftSceondTime / 60 / 60);
int m = (int)(leftSceondTime / 60) % 60;
return foreStr + String.Concat(h.ToString().PadLeft(2, '0'), ":", m.ToString().PadLeft(2, '0'));
}
}
UITools
最新推荐文章于 2022-07-15 10:32:46 发布