Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- /// <summary>
- /// This script can be used to anchor an object to the m_side of the screen,
- /// or scale an object to always match the dimensions of the screen.
- /// </summary>
- [ExecuteInEditMode]
- public class TUIAnchorOnSprite : MonoBehaviour {
- public enum ScaleConstrainMode {
- None = 0,
- ConstrainX,
- ConstrainY
- };
- public UIWidget m_anchorWidget;
- public UIAnchor.Side m_side = UIAnchor.Side.Center;
- public Vector2 m_staticOffset = Vector2.zero;
- public Vector2 m_relativeOffset = Vector2.zero;
- public Vector2 m_scaleFromParent = new Vector2(100f, 100f);
- public ScaleConstrainMode m_scaleConstrainMode = ScaleConstrainMode.None;
- public Camera m_uiCamera = null;
- protected Transform m_trans;
- protected Transform m_anchorTrans;
- protected UILabel m_label;
- // Cache the transform.
- void Awake () {
- m_trans = transform;
- m_anchorTrans = m_anchorWidget.transform;
- m_label = m_anchorWidget.GetComponent<UILabel>();
- }
- // Use this for initialization
- void Start () {
- }
- // Automatically find the camera responsible for drawing the widgets under this object.
- void OnEnable () {
- if (m_uiCamera == null) {
- m_uiCamera = NGUITools.FindCameraForLayer(gameObject.layer);
- }
- UpdatePosition();
- }
- void Update() {
- UpdatePosition();
- }
- public void UpdatePosition () {
- Vector3 v = m_anchorTrans.localPosition;
- // first set our scale
- if (m_scaleFromParent.x != 100f || m_scaleFromParent.y != 100f) {
- // get the size of the parent widget
- Vector3 parentSize = m_anchorTrans.localScale;
- Vector3 newSize = new Vector3(m_scaleFromParent.x/100f * parentSize.x,
- m_scaleFromParent.y/100f * parentSize.y, 1f);
- if (m_scaleConstrainMode == ScaleConstrainMode.ConstrainX) {
- newSize.x = newSize.y;
- } else if (m_scaleConstrainMode == ScaleConstrainMode.ConstrainY) {
- newSize.y = newSize.x;
- }
- if (newSize != m_trans.localScale) {
- m_trans.localScale = newSize;
- }
- }
- // figure out our size
- Vector3 anchorScale = m_anchorTrans.localScale;
- // unless we're a UILabel
- if (m_label != null) {
- anchorScale = m_label.relativeSize;
- }
- if (m_side != UIAnchor.Side.Center) {
- if (m_side == UIAnchor.Side.Right || m_side == UIAnchor.Side.TopRight || m_side == UIAnchor.Side.BottomRight) {
- v.x = anchorScale.x/2f;
- }
- else if (m_side == UIAnchor.Side.Top || m_side == UIAnchor.Side.Bottom) {
- //v.x = cx;
- }
- else {
- v.x = -anchorScale.x/2f;
- }
- if (m_side == UIAnchor.Side.Top || m_side == UIAnchor.Side.TopRight || m_side == UIAnchor.Side.TopLeft) {
- v.y = anchorScale.y/2f;
- }
- else if (m_side == UIAnchor.Side.Left || m_side == UIAnchor.Side.Right) {
- //v.y = cy;
- }
- else {
- v.y = -anchorScale.y/2f;
- }
- }
- v.x += m_relativeOffset.x * anchorScale.x;
- v.y += m_relativeOffset.y * anchorScale.y;
- v.x += m_staticOffset.x;
- v.y += m_staticOffset.y;
- if (m_uiCamera.orthographic) {
- v.x = Mathf.RoundToInt(v.x);
- v.y = Mathf.RoundToInt(v.y);
- }
- // Wrapped in an 'if' so the scene doesn't get marked as 'edited' every frame
- Vector3 worldV = m_anchorTrans.parent.TransformPoint(v);
- worldV.z = m_trans.position.z;
- if (m_trans.position != worldV) {
- m_trans.position = worldV;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement