UITools.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections;
  4. public class UITools : Editor {
  5. [MenuItem("My Tools/Fit Anchor to Rect")]
  6. static void MenuItem()
  7. {
  8. foreach (GameObject go in Selection.gameObjects)
  9. {
  10. RectTransform selectedRect = go.GetComponent<RectTransform>();
  11. RectTransform parentRect = go.transform.parent.GetComponent<RectTransform>();
  12. if (!selectedRect || !parentRect) return;
  13. Undo.RecordObject(selectedRect, "Change Anchor");
  14. selectedRect.anchorMin = new Vector2(selectedRect.anchorMin.x + selectedRect.offsetMin.x / parentRect.rect.width,
  15. selectedRect.anchorMin.y + selectedRect.offsetMin.y / parentRect.rect.height);
  16. selectedRect.anchorMax = new Vector2(selectedRect.anchorMax.x + selectedRect.offsetMax.x / parentRect.rect.width,
  17. selectedRect.anchorMax.y + selectedRect.offsetMax.y / parentRect.rect.height);
  18. selectedRect.offsetMin = selectedRect.offsetMax = Vector2.zero;
  19. }
  20. }
  21. [MenuItem("My Tools/Clear Playerprefs")]
  22. static void ClearPrefs()
  23. {
  24. PlayerPrefs.DeleteAll();
  25. }
  26. }