| 12345678910111213141516171819202122232425262728293031 |
- using UnityEngine;
- using UnityEngine.UI;
- namespace Olympus
- {
- // Nine-slice corners always receive one uniform scale. Capsule caps follow the
- // control's height; rectangular panels retain a fixed corner size in UI units.
- [ExecuteAlways, RequireComponent(typeof(Image))]
- public sealed class ProportionalSlice : MonoBehaviour
- {
- public bool capsule;
- public float cornerSize = 18;
- Image target;
- void OnEnable() { Apply(); }
- void LateUpdate() { Apply(); }
- public void Apply()
- {
- if (!target) target = GetComponent<Image>();
- if (!target.sprite || target.type != Image.Type.Sliced) return;
- Vector4 b = target.sprite.border;
- if (b == Vector4.zero) return;
- var r = target.rectTransform.rect;
- float wanted = capsule ? r.height * .48f : Mathf.Min(cornerSize, Mathf.Min(r.width, r.height) * .42f);
- float source = Mathf.Max(b.y, b.w);
- if (wanted <= 0 || source <= 0) return;
- float multiplier = source / (target.pixelsPerUnit * wanted);
- if (Mathf.Abs(target.pixelsPerUnitMultiplier - multiplier) > .0001f)
- target.pixelsPerUnitMultiplier = multiplier;
- }
- }
- }
|