EnvelopContent.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System;
  2. using UnityEngine;
  3. [RequireComponent(typeof(UIWidget))]
  4. [AddComponentMenu("NGUI/Examples/Envelop Content")]
  5. public class EnvelopContent : MonoBehaviour
  6. {
  7. private void Start()
  8. {
  9. this.mStarted = true;
  10. this.Execute();
  11. }
  12. private void OnEnable()
  13. {
  14. if (this.mStarted)
  15. {
  16. this.Execute();
  17. }
  18. }
  19. [ContextMenu("Execute")]
  20. public void Execute()
  21. {
  22. if (this.targetRoot == base.transform)
  23. {
  24. Debug.LogError("Target Root object cannot be the same object that has Envelop Content. Make it a sibling instead.", this);
  25. }
  26. else if (NGUITools.IsChild(this.targetRoot, base.transform))
  27. {
  28. Debug.LogError("Target Root object should not be a parent of Envelop Content. Make it a sibling instead.", this);
  29. }
  30. else
  31. {
  32. Bounds bounds = NGUIMath.CalculateRelativeWidgetBounds(base.transform.parent, this.targetRoot, false, true);
  33. float num = bounds.min.x + (float)this.padLeft;
  34. float num2 = bounds.min.y + (float)this.padBottom;
  35. float num3 = bounds.max.x + (float)this.padRight;
  36. float num4 = bounds.max.y + (float)this.padTop;
  37. UIWidget component = base.GetComponent<UIWidget>();
  38. component.SetRect(num, num2, num3 - num, num4 - num2);
  39. base.BroadcastMessage("UpdateAnchors", SendMessageOptions.DontRequireReceiver);
  40. }
  41. }
  42. public Transform targetRoot;
  43. public int padLeft;
  44. public int padRight;
  45. public int padBottom;
  46. public int padTop;
  47. private bool mStarted;
  48. }