using System; using UnityEngine; public abstract class UIRect : MonoBehaviour { public GameObject cachedGameObject { get { if (this.mGo == null) { this.mGo = base.gameObject; } return this.mGo; } } public Transform cachedTransform { get { if (this.mTrans == null) { this.mTrans = base.transform; } return this.mTrans; } } public Camera anchorCamera { get { if (!this.mAnchorsCached) { this.ResetAnchors(); } return this.mCam; } } public bool isFullyAnchored { get { return this.leftAnchor.target && this.rightAnchor.target && this.topAnchor.target && this.bottomAnchor.target; } } public virtual bool isAnchoredHorizontally { get { return this.leftAnchor.target || this.rightAnchor.target; } } public virtual bool isAnchoredVertically { get { return this.bottomAnchor.target || this.topAnchor.target; } } public virtual bool canBeAnchored { get { return true; } } public UIRect parent { get { if (!this.mParentFound) { this.mParentFound = true; this.mParent = NGUITools.FindInParents(this.cachedTransform.parent); } return this.mParent; } } public UIRoot root { get { if (this.parent != null) { return this.mParent.root; } if (!this.mRootSet) { this.mRootSet = true; this.mRoot = NGUITools.FindInParents(this.cachedTransform); } return this.mRoot; } } public bool isAnchored { get { return (this.leftAnchor.target || this.rightAnchor.target || this.topAnchor.target || this.bottomAnchor.target) && this.canBeAnchored; } } public abstract float alpha { get; set; } public abstract float CalculateFinalAlpha(int frameID); public abstract Vector3[] localCorners { get; } public abstract Vector3[] worldCorners { get; } protected float cameraRayDistance { get { if (this.anchorCamera == null) { return 0f; } if (!this.mCam.orthographic) { Transform cachedTransform = this.cachedTransform; Transform transform = this.mCam.transform; Plane plane = new Plane(cachedTransform.rotation * Vector3.back, cachedTransform.position); Ray ray = new Ray(transform.position, transform.rotation * Vector3.forward); float result; if (plane.Raycast(ray, out result)) { return result; } } return Mathf.Lerp(this.mCam.nearClipPlane, this.mCam.farClipPlane, 0.5f); } } public virtual void Invalidate(bool includeChildren) { this.mChanged = true; if (includeChildren) { for (int i = 0; i < this.mChildren.size; i++) { this.mChildren.buffer[i].Invalidate(true); } } } public virtual Vector3[] GetSides(Transform relativeTo) { if (this.anchorCamera != null) { return this.mCam.GetSides(this.cameraRayDistance, relativeTo); } Vector3 position = this.cachedTransform.position; for (int i = 0; i < 4; i++) { UIRect.mSides[i] = position; } if (relativeTo != null) { for (int j = 0; j < 4; j++) { UIRect.mSides[j] = relativeTo.InverseTransformPoint(UIRect.mSides[j]); } } return UIRect.mSides; } protected Vector3 GetLocalPos(UIRect.AnchorPoint ac, Transform trans) { if (this.anchorCamera == null || ac.targetCam == null) { return this.cachedTransform.localPosition; } Rect rect = ac.targetCam.rect; Vector3 vector = ac.targetCam.WorldToViewportPoint(ac.target.position); Vector3 vector2 = new Vector3(vector.x * rect.width + rect.x, vector.y * rect.height + rect.y, vector.z); vector2 = this.mCam.ViewportToWorldPoint(vector2); if (trans != null) { vector2 = trans.InverseTransformPoint(vector2); } vector2.x = Mathf.Floor(vector2.x + 0.5f); vector2.y = Mathf.Floor(vector2.y + 0.5f); return vector2; } protected virtual void OnEnable() { this.mUpdateFrame = -1; if (this.updateAnchors == UIRect.AnchorUpdate.OnEnable) { this.mAnchorsCached = false; this.mUpdateAnchors = true; } if (this.mStarted) { this.OnInit(); } this.mUpdateFrame = -1; } protected virtual void OnInit() { this.mChanged = true; this.mRootSet = false; this.mParentFound = false; if (this.parent != null) { this.mParent.mChildren.Add(this); } } protected virtual void OnDisable() { if (this.mParent) { this.mParent.mChildren.Remove(this); } this.mParent = null; this.mRoot = null; this.mRootSet = false; this.mParentFound = false; } protected void Start() { this.mStarted = true; this.OnInit(); this.OnStart(); } public void Update() { if (!this.mAnchorsCached) { this.ResetAnchors(); } int frameCount = Time.frameCount; if (this.mUpdateFrame != frameCount) { if (this.updateAnchors == UIRect.AnchorUpdate.OnUpdate || this.mUpdateAnchors) { this.mUpdateFrame = frameCount; this.mUpdateAnchors = false; bool flag = false; if (this.leftAnchor.target) { flag = true; if (this.leftAnchor.rect != null && this.leftAnchor.rect.mUpdateFrame != frameCount) { this.leftAnchor.rect.Update(); } } if (this.bottomAnchor.target) { flag = true; if (this.bottomAnchor.rect != null && this.bottomAnchor.rect.mUpdateFrame != frameCount) { this.bottomAnchor.rect.Update(); } } if (this.rightAnchor.target) { flag = true; if (this.rightAnchor.rect != null && this.rightAnchor.rect.mUpdateFrame != frameCount) { this.rightAnchor.rect.Update(); } } if (this.topAnchor.target) { flag = true; if (this.topAnchor.rect != null && this.topAnchor.rect.mUpdateFrame != frameCount) { this.topAnchor.rect.Update(); } } if (flag) { this.OnAnchor(); } } this.OnUpdate(); } } public void UpdateAnchors() { if (this.isAnchored && this.updateAnchors != UIRect.AnchorUpdate.OnStart) { this.OnAnchor(); } } protected abstract void OnAnchor(); public void SetAnchor(Transform t) { this.leftAnchor.target = t; this.rightAnchor.target = t; this.topAnchor.target = t; this.bottomAnchor.target = t; this.ResetAnchors(); this.UpdateAnchors(); } public void SetAnchor(GameObject go) { Transform target = (!(go != null)) ? null : go.transform; this.leftAnchor.target = target; this.rightAnchor.target = target; this.topAnchor.target = target; this.bottomAnchor.target = target; this.ResetAnchors(); this.UpdateAnchors(); } public void SetAnchor(GameObject go, int left, int bottom, int right, int top) { Transform target = (!(go != null)) ? null : go.transform; this.leftAnchor.target = target; this.rightAnchor.target = target; this.topAnchor.target = target; this.bottomAnchor.target = target; this.leftAnchor.relative = 0f; this.rightAnchor.relative = 1f; this.bottomAnchor.relative = 0f; this.topAnchor.relative = 1f; this.leftAnchor.absolute = left; this.rightAnchor.absolute = right; this.bottomAnchor.absolute = bottom; this.topAnchor.absolute = top; this.ResetAnchors(); this.UpdateAnchors(); } public void ResetAnchors() { this.mAnchorsCached = true; this.leftAnchor.rect = ((!this.leftAnchor.target) ? null : this.leftAnchor.target.GetComponent()); this.bottomAnchor.rect = ((!this.bottomAnchor.target) ? null : this.bottomAnchor.target.GetComponent()); this.rightAnchor.rect = ((!this.rightAnchor.target) ? null : this.rightAnchor.target.GetComponent()); this.topAnchor.rect = ((!this.topAnchor.target) ? null : this.topAnchor.target.GetComponent()); this.mCam = NGUITools.FindCameraForLayer(this.cachedGameObject.layer); this.FindCameraFor(this.leftAnchor); this.FindCameraFor(this.bottomAnchor); this.FindCameraFor(this.rightAnchor); this.FindCameraFor(this.topAnchor); this.mUpdateAnchors = true; } public void ResetAndUpdateAnchors() { this.ResetAnchors(); this.UpdateAnchors(); } public abstract void SetRect(float x, float y, float width, float height); private void FindCameraFor(UIRect.AnchorPoint ap) { if (ap.target == null || ap.rect != null) { ap.targetCam = null; } else { ap.targetCam = NGUITools.FindCameraForLayer(ap.target.gameObject.layer); } } public virtual void ParentHasChanged() { this.mParentFound = false; UIRect y = NGUITools.FindInParents(this.cachedTransform.parent); if (this.mParent != y) { if (this.mParent) { this.mParent.mChildren.Remove(this); } this.mParent = y; if (this.mParent) { this.mParent.mChildren.Add(this); } this.mRootSet = false; } } protected abstract void OnStart(); protected virtual void OnUpdate() { } public UIRect.AnchorPoint leftAnchor = new UIRect.AnchorPoint(); public UIRect.AnchorPoint rightAnchor = new UIRect.AnchorPoint(1f); public UIRect.AnchorPoint bottomAnchor = new UIRect.AnchorPoint(); public UIRect.AnchorPoint topAnchor = new UIRect.AnchorPoint(1f); public UIRect.AnchorUpdate updateAnchors = UIRect.AnchorUpdate.OnUpdate; protected GameObject mGo; protected Transform mTrans; protected BetterList mChildren = new BetterList(); protected bool mChanged = true; protected bool mStarted; protected bool mParentFound; [NonSerialized] private bool mUpdateAnchors = true; [NonSerialized] private int mUpdateFrame = -1; [NonSerialized] private bool mAnchorsCached; [NonSerialized] private UIRoot mRoot; [NonSerialized] private UIRect mParent; [NonSerialized] private bool mRootSet; [NonSerialized] protected Camera mCam; [NonSerialized] public float finalAlpha = 1f; protected static Vector3[] mSides = new Vector3[4]; [Serializable] public class AnchorPoint { public AnchorPoint() { } public AnchorPoint(float relative) { this.relative = relative; } public void Set(float relative, float absolute) { this.relative = relative; this.absolute = Mathf.FloorToInt(absolute + 0.5f); } public void Set(Transform target, float relative, float absolute) { this.target = target; this.relative = relative; this.absolute = Mathf.FloorToInt(absolute + 0.5f); } public void SetToNearest(float abs0, float abs1, float abs2) { this.SetToNearest(0f, 0.5f, 1f, abs0, abs1, abs2); } public void SetToNearest(float rel0, float rel1, float rel2, float abs0, float abs1, float abs2) { float num = Mathf.Abs(abs0); float num2 = Mathf.Abs(abs1); float num3 = Mathf.Abs(abs2); if (num < num2 && num < num3) { this.Set(rel0, abs0); } else if (num2 < num && num2 < num3) { this.Set(rel1, abs1); } else { this.Set(rel2, abs2); } } public void SetHorizontal(Transform parent, float localPos) { if (this.rect) { Vector3[] sides = this.rect.GetSides(parent); float num = Mathf.Lerp(sides[0].x, sides[2].x, this.relative); this.absolute = Mathf.FloorToInt(localPos - num + 0.5f); } else { Vector3 position = this.target.position; if (parent != null) { position = parent.InverseTransformPoint(position); } this.absolute = Mathf.FloorToInt(localPos - position.x + 0.5f); } } public void SetVertical(Transform parent, float localPos) { if (this.rect) { Vector3[] sides = this.rect.GetSides(parent); float num = Mathf.Lerp(sides[3].y, sides[1].y, this.relative); this.absolute = Mathf.FloorToInt(localPos - num + 0.5f); } else { Vector3 position = this.target.position; if (parent != null) { position = parent.InverseTransformPoint(position); } this.absolute = Mathf.FloorToInt(localPos - position.y + 0.5f); } } public Vector3[] GetSides(Transform relativeTo) { if (this.target != null) { if (this.rect != null) { return this.rect.GetSides(relativeTo); } if (this.target.GetComponent() != null) { return this.target.GetComponent().GetSides(relativeTo); } } return null; } public Transform target; public float relative; public int absolute; [NonSerialized] public UIRect rect; [NonSerialized] public Camera targetCam; } public enum AnchorUpdate { OnEnable, OnUpdate, OnStart } }