在开发的时候可能会用到的一些UI帮助函数,是基于UGUI的:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class UHelper : MonoBehaviour {
private static int GetActiceChildCount(Transform content)
{
int count = 0;
for (int i = 0; i < content.childCount; ++i)
{
if (content.GetChild(i).gameObject.activeSelf)
{
++count;
}
}
return count;
}
/// <summary>
/// 简单的重设滑动区域大小---按unity自带content锚点做
/// </summary>
/// <param name="content"></param>
/// <param name="eachrowNum"></param>
public static void ResetContentSize(GameObject content, int eachrowNum, RectTransform.Axis type, out Vector2 newPos)
{
RectTransform conRect = content.transform.GetComponent<RectTransform>();
newPos = conRect.sizeDelta;
int count = GetActiceChildCount(content.transform);
//个数小于行数
if ((count + 1) <= eachrowNum)
{
return;
}
GridLayoutGroup ridGroup = content.transform.GetComponent<GridLayoutGroup>();
if (ridGroup != null)
{
int scale = count / eachrowNum; //每排多少个
if (count % eachrowNum != 0)
{
++scale;
}
float newSize = ridGroup.cellSize.x * scale + ridGroup.spacing.x * (scale - 1);
if (type == RectTransform.Axis.Vertical)
{
newSize = ridGroup.cellSize.y * scale + ridGroup.spacing.y * (scale - 1);
newSize += (ridGroup.padding.top + ridGroup.padding.bottom);
newPos = new Vector2(newPos.x, newSize);
}
else
{
newSize += (ridGroup.padding.left + ridGroup.padding.right);
newPos = new Vector2(newSize, newPos.y);
}
}
}
public static void SetVerticalLayoutGroup(GameObject content, int eachrowNum, RectTransform.Axis type, out Vector2 newPos)
{
RectTransform conRect = content.transform.GetComponent<RectTransform>();
newPos = conRect.sizeDelta;
VerticalLayoutGroup layoutGroup = content.transform.GetComponent<VerticalLayoutGroup>();
if (layoutGroup != null)
{
float newSize = 0;
foreach (Transform Tform in content.transform)
{
LayoutElement item = Tform.GetComponent<LayoutElement>();
if (type == RectTransform.Axis.Vertical)
{
newSize += item.minHeight + layoutGroup.spacing;
}
else
{
newSize += item.minWidth + layoutGroup.spacing;
newSize += (layoutGroup.padding.left + layoutGroup.padding.right);
}
}
if (type == RectTransform.Axis.Vertical)
{
newSize += (layoutGroup.padding.top + layoutGroup.padding.bottom);
}
else
{
newSize += (layoutGroup.padding.left + layoutGroup.padding.right);
}
newPos = new Vector2(newPos.x, newSize);
}
}
/// <summary>
/// 重新设置滑动区域的大小,返回大小
/// </summary>
/// <param name="content"></param>
/// <param name="headItem"></param>
/// <returns></returns>
public static Vector2 ResetContentSize(GameObject content, Transform headItem, RectTransform.Axis type = RectTransform.Axis.Vertical)
{
RectTransform conRect = content.transform.GetComponent<RectTransform>();
Vector2 v2 = new Vector2(conRect.sizeDelta.x, conRect.sizeDelta.y);
int count = GetActiceChildCount(content.transform);
if (count <= 0)
{
return v2;
}
headItem = content.transform.GetChild(0);
if (headItem != null)
{
if (type == RectTransform.Axis.Vertical)
{
float headSize = headItem.GetComponent<RectTransform>().sizeDelta.x;
//是否用到了布局
float contentSize = conRect.sizeDelta.x;
float newSize = 0; //新的高度
if (content.transform.GetComponent<GridLayoutGroup>())
{
GridLayoutGroup ridGroup = content.transform.GetComponent<GridLayoutGroup>();
headSize = ridGroup.cellSize.x;
int scale = (int)(contentSize / (headSize + ridGroup.spacing.x)); //每排多少个
if (scale == 0)
{
return new Vector2(conRect.sizeDelta.x, conRect.sizeDelta.y);
}
int listCount = count / scale;
//布局x,y间隔
float Interval_y = ridGroup.spacing.y;
if (count % scale == 0)
{
newSize = ridGroup.cellSize.y * listCount + (listCount - 1) * Interval_y;
}
else
{
newSize = ridGroup.cellSize.y * (listCount + 1) + listCount * Interval_y;
}
newSize += ridGroup.padding.top;
if (newSize < conRect.sizeDelta.y)
{
newSize = conRect.sizeDelta.y;
}
v2 = new Vector2(conRect.sizeDelta.x, newSize);
}
else
{
float scale = contentSize / headSize;
int listCount = (int)(count / scale);
headSize = headItem.GetComponent<RectTransform>().sizeDelta.y;
if (count % scale == 0)
{
newSize = headSize * listCount;
}
else
{
newSize = headSize * (listCount + 1);
}
if (newSize < conRect.sizeDelta.y)
{
newSize = conRect.sizeDelta.y;
}
v2 = new Vector2(conRect.sizeDelta.x, newSize);
}
}
else
{
float headSize = headItem.GetComponent<RectTransform>().sizeDelta.y;
//是否用到了布局
float contentSize = conRect.sizeDelta.y;
float newSize = 0; //新的高度
if (content.transform.GetComponent<GridLayoutGroup>())
{
GridLayoutGroup ridGroup = content.transform.GetComponent<GridLayoutGroup>();
headSize = ridGroup.cellSize.y;
int scale = (int)(contentSize / (headSize + ridGroup.spacing.x)); //每排多少个
if (scale == 0)
{
scale = 1;
}
int listCount = count / scale;
//布局x,y间隔
float Interval_x = ridGroup.spacing.x;
if (count % scale == 0)
{
newSize = ridGroup.cellSize.x * listCount + (listCount - 1) * Interval_x;
}
else
{
newSize = ridGroup.cellSize.x * (listCount + 1) + listCount * Interval_x;
}
newSize += ridGroup.padding.left * 2;
if (newSize < conRect.sizeDelta.x)
{
newSize = conRect.sizeDelta.x;
}
v2 = new Vector2(newSize, conRect.sizeDelta.y);
}
else
{
float scale = contentSize / headSize;
int listCount = (int)(count / scale);
headSize = headItem.GetComponent<RectTransform>().sizeDelta.x;
if (count % scale == 0)
{
newSize = headSize * listCount;
}
else
{
newSize = headSize * (listCount + 1);
}
if (newSize < conRect.sizeDelta.x)
{
newSize = conRect.sizeDelta.x;
}
v2 = new Vector2(newSize, conRect.sizeDelta.y);
}
}
}
return v2;
}
/// <summary>
/// 隐藏所有子物体
/// </summary>
/// <param name="content"></param>
public static void HideAllContentChild(GameObject content, bool active = false)
{
if (content == null || content.transform.childCount == 0)
{
return;
}
foreach (Transform child in content.transform)
{
child.gameObject.SetActive(active);
}
}
/// <summary>
/// 清除所有子物体
/// </summary>
/// <param name="content"></param>
public static void ClearContentChild(GameObject content)
{
if (content == null || content.transform.childCount == 0)
{
return;
}
while (content.transform.childCount > 0)
{
GameObject.DestroyImmediate(content.transform.GetChild(0).gameObject);
}
}
/// <summary>
/// 往父对象中添加指定的子对象
/// </summary>
/// <param name="parent">父对象</param>
/// <param name="prefab">子对象</param>
/// <returns>新加的子对象</returns>
public static GameObject AddChild(Transform parent, GameObject prefab)
{
GameObject go = GameObject.Instantiate(prefab) as GameObject;
if (go != null && parent != null)
{
Transform t = go.transform;
t.SetParent(parent, false);
go.layer = parent.gameObject.layer;
}
return go;
}
/// <summary>
/// 销毁对象,照搬自NGUI的NGUITools
/// </summary>
/// <param name="obj"></param>
public static void Destroy(UnityEngine.Object obj)
{
if (obj != null)
{
if (Application.isPlaying)
{
if (obj is GameObject)
{
GameObject go = obj as GameObject;
go.transform.parent = null;
}
UnityEngine.Object.Destroy(obj);
}
else UnityEngine.Object.DestroyImmediate(obj);
}
}
}
/// <summary>
/// 把当前对象移动到新位置(2D)
/// </summary>
/// <param name="obj"></param>
/// <param name="NewPos"></param>
/// <param name="duration"></param>
/// <param name="z"></param>
public static void MoveTo(GameObject obj, Vector2 NewPos, float duration, float z)
{
Animation anim = obj.GetComponent<Animation>();
//if (anim.GetClipCount() > 1)
//{
// anim.RemoveClip("Moveto");
//}
anim.enabled = true;
AnimationClip animclip = new AnimationClip();
#if UNITY_5
animclip.legacy = true;
#endif
AnimationCurve curvex = AnimationCurve.Linear(0, obj.transform.localPosition.x, duration, NewPos.x);
AnimationCurve curvey = AnimationCurve.Linear(0, obj.transform.localPosition.y, duration, NewPos.y);
AnimationCurve curvez = AnimationCurve.Linear(0, z, duration, z);
AnimationCurve curvenable = AnimationCurve.Linear(0, 1, duration, 0);
animclip.SetCurve("", typeof(Transform), "localPosition.x", curvex);
animclip.SetCurve("", typeof(Transform), "localPosition.y", curvey);
animclip.SetCurve("", typeof(Transform), "localPosition.z", curvez);
animclip.SetCurve("", typeof(Animation), "m_Enabled", curvenable);
anim.AddClip(animclip, "Moveto");
anim.Play("Moveto");
Destroy(animclip, duration);
}
public static void MoveTo(GameObject obj, Vector2 startpos, Vector2 NewPos, float duration, float z)
{
Animation anim = obj.GetComponent<Animation>();
//if (anim.GetClipCount() > 1)
//{
// anim.RemoveClip("Moveto");
//}
anim.enabled = true;
AnimationClip animclip = new AnimationClip();
#if UNITY_5
animclip.legacy = true;
#endif
AnimationCurve curvex = AnimationCurve.Linear(0, startpos.x, duration, NewPos.x);
AnimationCurve curvey = AnimationCurve.Linear(0, startpos.y, duration, NewPos.y);
AnimationCurve curvez = AnimationCurve.Linear(0, z, duration, z);
AnimationCurve curvenable = AnimationCurve.Linear(0, 1, duration, 0);
animclip.SetCurve("", typeof(Transform), "localPosition.x", curvex);
animclip.SetCurve("", typeof(Transform), "localPosition.y", curvey);
animclip.SetCurve("", typeof(Transform), "localPosition.z", curvez);
animclip.SetCurve("", typeof(Animation), "m_Enabled", curvenable);
anim.AddClip(animclip, "Moveto");
anim.Play("Moveto");
Destroy(animclip, duration);
}