Advertisement
Guest User

TUIAnchorOnSprite

a guest
Sep 17th, 2012
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.31 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. /// <summary>
  5. /// This script can be used to anchor an object to the m_side of the screen,
  6. /// or scale an object to always match the dimensions of the screen.
  7. /// </summary>
  8.  
  9. [ExecuteInEditMode]
  10. public class TUIAnchorOnSprite : MonoBehaviour {
  11.  
  12.     public enum ScaleConstrainMode {
  13.         None = 0,
  14.         ConstrainX,
  15.         ConstrainY
  16.     };
  17.    
  18.     public UIWidget m_anchorWidget;
  19.     public UIAnchor.Side m_side = UIAnchor.Side.Center;
  20.     public Vector2 m_staticOffset = Vector2.zero;
  21.     public Vector2 m_relativeOffset = Vector2.zero;
  22.     public Vector2 m_scaleFromParent = new Vector2(100f, 100f);
  23.     public ScaleConstrainMode m_scaleConstrainMode = ScaleConstrainMode.None;
  24.     public Camera m_uiCamera = null;
  25.    
  26.     protected Transform m_trans;
  27.     protected Transform m_anchorTrans;
  28.     protected UILabel m_label;
  29.    
  30.     // Cache the transform.
  31.     void Awake () {
  32.         m_trans = transform;
  33.         m_anchorTrans = m_anchorWidget.transform;
  34.         m_label = m_anchorWidget.GetComponent<UILabel>();
  35.     }
  36.  
  37.     // Use this for initialization
  38.     void Start () {
  39.    
  40.     }
  41.  
  42.     // Automatically find the camera responsible for drawing the widgets under this object.
  43.     void OnEnable () {
  44.         if (m_uiCamera == null) {
  45.             m_uiCamera = NGUITools.FindCameraForLayer(gameObject.layer);
  46.         }
  47.         UpdatePosition();
  48.     }
  49.    
  50.     void Update() {
  51.         UpdatePosition();
  52.     }
  53.    
  54.     public void UpdatePosition () {
  55.         Vector3 v = m_anchorTrans.localPosition;
  56.        
  57.         // first set our scale
  58.         if (m_scaleFromParent.x != 100f || m_scaleFromParent.y != 100f) {
  59.             // get the size of the parent widget
  60.             Vector3 parentSize = m_anchorTrans.localScale;
  61.             Vector3 newSize = new Vector3(m_scaleFromParent.x/100f * parentSize.x,
  62.                 m_scaleFromParent.y/100f * parentSize.y, 1f);
  63.             if (m_scaleConstrainMode == ScaleConstrainMode.ConstrainX) {
  64.                 newSize.x = newSize.y;
  65.             } else if (m_scaleConstrainMode == ScaleConstrainMode.ConstrainY) {
  66.                 newSize.y = newSize.x;
  67.             }
  68.             if (newSize != m_trans.localScale) {
  69.                 m_trans.localScale = newSize;
  70.             }
  71.         }
  72.        
  73.         // figure out our size
  74.         Vector3 anchorScale = m_anchorTrans.localScale;
  75.         // unless we're a UILabel
  76.         if (m_label != null) {
  77.             anchorScale = m_label.relativeSize;
  78.         }
  79.        
  80.         if (m_side != UIAnchor.Side.Center) {
  81.             if (m_side == UIAnchor.Side.Right || m_side == UIAnchor.Side.TopRight || m_side == UIAnchor.Side.BottomRight) {
  82.                 v.x = anchorScale.x/2f;
  83.             }
  84.             else if (m_side == UIAnchor.Side.Top || m_side == UIAnchor.Side.Bottom) {
  85.                 //v.x = cx;
  86.             }
  87.             else {
  88.                 v.x = -anchorScale.x/2f;
  89.             }
  90.  
  91.             if (m_side == UIAnchor.Side.Top || m_side == UIAnchor.Side.TopRight || m_side == UIAnchor.Side.TopLeft) {
  92.                 v.y = anchorScale.y/2f;
  93.             }
  94.             else if (m_side == UIAnchor.Side.Left || m_side == UIAnchor.Side.Right) {
  95.                 //v.y = cy;
  96.             }
  97.             else {
  98.                 v.y = -anchorScale.y/2f;
  99.             }
  100.         }
  101.  
  102.         v.x += m_relativeOffset.x * anchorScale.x;
  103.         v.y += m_relativeOffset.y * anchorScale.y;
  104.  
  105.         v.x += m_staticOffset.x;
  106.         v.y += m_staticOffset.y;
  107.  
  108.         if (m_uiCamera.orthographic) {
  109.             v.x = Mathf.RoundToInt(v.x);
  110.             v.y = Mathf.RoundToInt(v.y);
  111.         }
  112.        
  113.         // Wrapped in an 'if' so the scene doesn't get marked as 'edited' every frame
  114.         Vector3 worldV = m_anchorTrans.parent.TransformPoint(v);
  115.         worldV.z = m_trans.position.z;
  116.         if (m_trans.position != worldV) {
  117.             m_trans.position = worldV;
  118.         }
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement