//拖动物体,使用UGUI来使图片随着鼠标移动,移动时是中心点在鼠标点
public static void DragItem(GameObject go, PointerEventData data)
{
RectTransformUtility.ScreenPointToWorldPointInRectangle(go.transform.GetComponent(), Input.mousePosition, data.enterEventCamera, out m_dragPosition);
go.transform.position = m_dragPosition;
}
//横向拖动物体(仅在x轴上)
public static void DragHorizontalItem(GameObject go, PointerEventData data, int min = 0, int max = 0)
{
RectTransformUtility.ScreenPointToWorldPointInRectangle(go.transform.GetComponent(), Input.mousePosition, data.enterEventCamera, out m_dragPosition);
Vector3 vec = new Vector3(m_dragPosition.x, go.transform.position.y, m_dragPosition.z);
if (min != 0 || max != 0)
{
if (m_dragPosition.x <= min || m_dragPosition.x >= max)
{
vec = new Vector3(0, go.transform.position.y, m_dragPosition.z);
}
}
go.transform.position = vec;
}
//-------------------让物体随着鼠标移动,物体的中心不在鼠标点-------------------------
//只能在Game场景下使用
public static void DragItemOnMousePoint(GameObject go, PointerEventData data)
{
go.GetComponent().anchoredPosition += data.delta * GetScreenScale();
}
//得到Game场景UI界面缩放比例系数,如果是-1就表示有问题
static float GetScreenScale()
{
CanvasScaler cs = GameObject.Find("Game/Camera/UIRoot").GetComponent<CanvasScaler>();
if (cs == null)
{
LogSystem.Log("can not find cs!");
return -1;
}
float scale = cs.referenceResolution.x / Screen.width;
return scale;
}
//判断一个时间点是不是今天
public static bool IsToday(long time)
{
//将时间戳转换成日期
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
DateTime tempDateTime = dtStart.AddSeconds(time);
//long timeStamp = (long)(DateTime.Now - dtStart).TotalMilliseconds;
DateTime nowTime = DateTime.Now;
if (tempDateTime.Year == nowTime.Year &&
tempDateTime.Month == nowTime.Month &&
tempDateTime.Day == nowTime.Day)
{
//如果要判断的时间点和今天的时间点(年,月,日)都一样的话,那时间点就是今天
return true;
}
return false;
}
//得到utc时间戳
public static long GetUtcTime()
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
long timeStamp = (long)(DateTime.Now - startTime).TotalSeconds; // 相差秒数
return timeStamp;
}
//使某个物体显示到鼠标点击的位置
public static void SetPositionOnMousePosition(GameObject go, PointerEventData data)
{
Vector3 m_CurrentPos;
RectTransform rt = go.GetComponent();
if (rt != null)
{
RectTransformUtility.ScreenPointToWorldPointInRectangle(rt, Input.mousePosition, data.enterEventCamera, out m_CurrentPos);
go.transform.position = m_CurrentPos;
}
else
{
go.transform.position = Camera.main.ScreenToWorldPoint(data.pressPosition);
}
}