UIRect源码分析
UIRect是UIPanel和UIWidget的抽象基类,提供了一些共有的方法和共同的逻辑。其中最主要的逻辑是围绕Anchors更新位置,和提供了一些时序相关的抽象方法。
AnchorPoint
UIRect首先内置了AnchorPoint这么一个内联类,并定义了四个AnchorPoint类型的变量,分别对应上下左右四个方向的锚点。
其中有几个重要的字段:
public Transform target; // 表示目标对象
public float relative = 0f; // 目标对象的参考系坐标,默认为0、0.5、1三个值,用来表示左/中/右、上/中/下三个方向,也可以用自定义的值
public int absolute = 0; // 相对坐标
[System.NonSerialized]
public UIRect rect; // target对应的UIRect,可能为Null
[System.NonSerialized]
public Camera targetCam; // target对应的Camera
AnchorPoint类同时还提供一些方法供UIRect或者Editor使用。例如每次为一个AnchorPoint赋值一个新的target都会调用SetToNearest(...)
获取目标对象最近的边和相对位置等等。
一个比较重要且好用的方法是:
/// <summary>
/// Convenience function that returns the sides the anchored point is anchored to.
/// </summary>
public Vector3[] GetSides (Transform relativeTo)
{
if (target != null)
{
if (rect != null) return rect.GetSides(relativeTo);
#if UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_4_7
if (target.camera != null) return target.camera.GetSides(relativeTo);
#else
var cam = target.GetComponent<Camera>();
if (cam != null) return cam.GetSides(relativeTo);
#endif
}
return null;
}
GetSides可以用来获取target的UIRect四条边(顺序为左、上、右、下)相对于某个transform(通常是parent)的位置。
一些重要的类成员
每个UIRect都内置了四个方向的锚点:
/// <summary>
/// Left side anchor.
/// </summary>
public AnchorPoint leftAnchor = new AnchorPoint();
/// <summary>
/// Right side anchor.
/// </summary>
public AnchorPoint rightAnchor = new AnchorPoint(1f);
/// <summary>
/// Bottom side anchor.
/// </summary>
public AnchorPoint bottomAnchor = new AnchorPoint();
/// <summary>
/// Top side anchor.
/// </summary>
public AnchorPoint topAnchor = new AnchorPoint(1f);
同时定义了一个枚举AnchorUpdate来表示不同的更新方式:
public enum AnchorUpdate
{
OnEnable,
OnUpdate,
OnStart,
}
很有意思的一点是,UIRect作为一个需要被频繁访问的类,对于一些会被频繁访问的字段,使用了缓存。
/// <summary>
/// Game object gets cached for speed. Can't simply return 'mGo' set in Awake because this function may be called on a prefab.
/// </summary>
public GameObject cachedGameObject { get { if (mGo == null) mGo = gameObject; return mGo; } }
/// <summary>
/// Transform gets cached for speed. Can't simply return 'mTrans' set in Awake because this function may be called on a prefab.
/// </summary>
public Transform cachedTransform { get { if (mTrans == null) mTrans = transform; return mTrans; } }
查阅了相关资料,知道了原来MonoBehavior的gameObject和transform字段其实都是一个getter,调用了GetComponent<>()函数(其中Unity5以后的版本,transform已经在MonoBahavior中缓存了)。因此如果是访问频率非常高的字段,用缓存会带来性能上的提高。
这里参考了回答:Caching of transform gameobject accessors really necessary?
Yeah, things like .transform are actually slow properties that do gameObject.GetComponent<xxx>(). If you are accessing those properties repeatedly, like in Update() functions, caching that property can save some CPU power (I think they are included under "overhead" in the profiler).
As I understand it, in Unity 5 they got rid of all of the property getters except for .transform, and that one they now cache for you.
同时UIRect还维护了一个树形结构,用来表示一个其在场景中的父子关系。
[System.NonSerialized] protected BetterList<UIRect> mChildren = new BetterList<UIRect>();
[System.NonSerialized] UIRoot mRoot;
[System.NonSerialized] UIRect mParent;
其中后两者有对应的getter,分别是root和parent。
还有一个重要的字段是:
[System.NonSerialized] protected bool mChanged = true;
这个字段用来表示UIRect是否发生改变(例如Alpha发生改变)。唯有mChanged == true
时,Widget的几何信息才会被更新。
// UIWidget.cs
public bool UpdateGeometry (int frame)
{
// Has the alpha changed?
float finalAlpha = CalculateFinalAlpha(frame);
if (mIsVisibleByAlpha && mLastAlpha != finalAlpha) mChanged = true;
mLastAlpha = finalAlpha;
if (mChanged)
{
... // 会调用OnFill()
}
else if (mMoved && geometry.hasVertices)
{
...
}
mMoved = false;
mChanged = false;
return false;
}
其中上面的finalAlpha对应的是Widget最终的alpha值,受父Widget的alpha值影响。同时还有一个alpha字段表示local的alpha值。
localCorners和worldCorners两个抽象getter,方便获取四个角的局部/世界坐标,在UIPanel和UIWidget中都有实现。
/// <summary>
/// Local-space corners of the UI rectangle. The order is bottom-left, top-left, top-right, bottom-right.
/// </summary>
public abstract Vector3[] localCorners { get; }
/// <summary>
/// World-space corners of the UI rectangle. The order is bottom-left, top-left, top-right, bottom-right.
/// </summary>
public abstract Vector3[] worldCorners { get; }
UIRect的更新逻辑
主要的更新逻辑在Update函数中。
public void Update ()
{
if (!mAnchorsCached) ResetAnchors();
int frame = Time.frameCount;
#if UNITY_EDITOR
if (mUpdateFrame != frame || !Application.isPlaying)
#else
if (mUpdateFrame != frame)
#endif
{
#if UNITY_EDITOR
if (updateAnchors == AnchorUpdate.OnUpdate || mUpdateAnchors || !Application.isPlaying)
#else
if (updateAnchors == AnchorUpdate.OnUpdate || mUpdateAnchors)
#endif
UpdateAnchorsInternal(frame);
// Continue with the update
OnUpdate();
}
}
ResetAnchors()
保证AnchorPoint对应的UIRect被正确的引用。
判断mUpdateFrame != frame
保证同一帧最多只会刷新一次。
OnUpdate()
是锚点更新完成后执行的函数,是个虚函数,供子类重载。
/// <summary>
/// Abstract update functionality, ensured to happen after the targeting anchors have been updated.
/// </summary>
protected virtual void OnUpdate () { }
真正的更新逻辑是UpdateAnchorsInternal(frame)
。
/// <summary>
/// Update anchors.
/// </summary>
protected void UpdateAnchorsInternal (int frame)
{
mUpdateFrame = frame;
mUpdateAnchors = false;
bool anchored = false;
if (leftAnchor.target)
{
anchored = true;
if (leftAnchor.rect != null && leftAnchor.rect.mUpdateFrame != frame)
leftAnchor.rect.Update();
}
if (bottomAnchor.target)
{
anchored = true;
if (bottomAnchor.rect != null && bottomAnchor.rect.mUpdateFrame != frame)
bottomAnchor.rect.Update();
}
if (rightAnchor.target)
{
anchored = true;
if (rightAnchor.rect != null && rightAnchor.rect.mUpdateFrame != frame)
rightAnchor.rect.Update();
}
if (topAnchor.target)
{
anchored = true;
if (topAnchor.rect != null && topAnchor.rect.mUpdateFrame != frame)
topAnchor.rect.Update();
}
// Update the dimensions using anchors
if (anchored) OnAnchor();
}
这里其实就干了两件事情。
- 更新四个锚点对应的UIRect(递归更新)。
- 更新自己,即调用
OnAnchor()
。
mUpdateFrame保证了UIRect不会被重复更新。
OnAnchor是个纯虚函数,需要被派生类重载实现。
/// <summary>
/// Update the dimensions of the rectangle using anchor points.
/// </summary>
protected abstract void OnAnchor ();
OnAnchor实际上就是用来定义如何根据锚点更新自身位置大小的函数,NGUI大部分控件的更新逻辑都是一致的,即继承自UIWidget,调用UIWidget的OnAnchor。
一些有用的类方法
ResetAndUpdateAnchors
ResetAndUpdateAnchors可以强制让UIRect重置并更新Anchor(重置的目的是为了保证每个AnchorPoint引用的UIRect和Camera没有丢失)。
/// <summary>
/// Convenience method that resets and updates the anchors, all at once.
/// </summary>
public void ResetAndUpdateAnchors () { ResetAnchors(); UpdateAnchors(); }
Invalidate可以将mChanged标识为true,表示其几何信息发生改变,需要被更新(发生在UIGeometry中,前文有提到)。
GetSides
UIRect的GetSides是一个虚方法,虽然提供了实现,但感觉用的不多,更多的是调用重载后的方法。分别在UIPanel,UIWidget和UILabel中重载了,用于获取四条边相对于某个Transform的坐标。
默认其实是返回相机的四条边的相对坐标。如果没有相机,则返回自身的相对坐标 x 4 。
/// <summary>
/// Get the sides of the rectangle relative to the specified transform.
/// The order is left, top, right, bottom.
/// </summary>
public virtual Vector3[] GetSides (Transform relativeTo)
{
if (anchorCamera != null) return mCam.GetSides(cameraRayDistance, relativeTo);
Vector3 pos = cachedTransform.position;
for (int i = 0; i < 4; ++i)
mSides[i] = pos;
if (relativeTo != null)
{
for (int i = 0; i < 4; ++i)
mSides[i] = relativeTo.InverseTransformPoint(mSides[i]);
}
return mSides;
}
SetAnchor
提供了许多重载,用于调整Anchors(包括目标对象,relative,absolute)。仔细想想还是用得上的,例如我们可以把一个Icon从一个对象身上挂到另一个对象身上,而保持相对位置不变,等等。
SetRect
抽象函数,被Widget和Panel重载,用于手动调整大小。
/// <summary>
/// Set the rectangle manually. XY is the bottom-left corner.
/// </summary>
public abstract void SetRect (float x, float y, float width, float height);
一些抽象方法
- OnAnchor:更新锚点位置。
- OnUpdate:自更新逻辑,发生在OnAnchor之后(如果有)。
- OnStart:Start时。
- OnInit:Start或者OnEnable时。
一些Tips
在OnEnable的时候会调用OnInit,UIRect的OnInit到没有什么可说的。 UIPanel里的OnInit在一堆初始化之中,有一句mRebuild = true,这会导致这个UIPanel里的所有UIDrawCall重建(具体我们会在UIPanel里讲到)。 UIWidget的OnInit有一句RemoveFromPanel(),并会调用OnUpdate(通过Update()),又会重新创建(寻找)panel(会导致这个Widget所属的UIDrawCall会被重建,甚至所有UIDrawCall重建),并会调用Invalidate更新Visibility和FinalAlpha,并且这个方法是递归的,意思就是所有的子Widget都会更新一遍。 所以GameObject.SetActive还是慎重使用。 ———————————————— 版权声明:本文为CSDN博主「凯奥斯」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。 原文链接: https://blog.csdn.net/ecidevilin/article/details/52422987
对于频繁切换显示状态的UI可以采用以下两种方法来解决:
- 通过设置localPosition将控件移出或移入视野;
- 通过设置Widget的alpha值,可以将alpha设置为一个很小但不为0的值,例如0.01;