| using UnityEngine; | 
| using System.Collections; | 
| using UnityEngine.UI; | 
| #if  UNITY_EDITOR | 
| using UnityEditor; | 
|   | 
| #endif | 
|   | 
| [System.Serializable] | 
| [RequireComponent(typeof(Image))] | 
| public class ImageFitterText : MonoBehaviour | 
| { | 
|     [SerializeField] Text m_Target; | 
|     [SerializeField] Vector3 delta = Vector3.zero; | 
|     [SerializeField] Vector2 sizeDelta = Vector2.zero; | 
|     [SerializeField] bool LockWidth = false; | 
|     [SerializeField] bool LockHeight = false; | 
|     [SerializeField] bool fiterRealTxtWidth = false; | 
|     [SerializeField] bool minSizeDeltaHeight = false; | 
|     [SerializeField] float minHeightValue = 0f; | 
|     RectTransform m_RectTransform; | 
|     RectTransform m_Rt { | 
|         get { | 
|             if (m_RectTransform == null) { | 
|                 m_RectTransform = this.transform as RectTransform; | 
|             } | 
|             return m_RectTransform; | 
|         } | 
|     } | 
|   | 
|     public bool FiterRealTxtWidth | 
|     { | 
|         set | 
|         { | 
|             fiterRealTxtWidth = value; | 
|         } | 
|     } | 
|     Vector3 targetLocalPositon = Vector3.zero; | 
|     Vector2 targetSizeDelta = Vector2.zero; | 
|     private void OnEnable() | 
|     { | 
|         if (m_Target == null) { | 
|             enabled = false; | 
|         } | 
|         //m_Rt = this.transform as RectTransform; | 
|     } | 
|     private void LateUpdate() | 
|     { | 
|         Refresh(); | 
|     } | 
|     public void Refresh() | 
|     { | 
|         if (m_Target == null) { | 
|             return; | 
|         } | 
|         Vector2 _sizeDelta = m_Target.rectTransform.sizeDelta + sizeDelta; | 
|         if (LockWidth) { | 
|             _sizeDelta.x = m_Rt.sizeDelta.x; | 
|         } | 
|         if (LockHeight) { | 
|             _sizeDelta.y = m_Rt.sizeDelta.y; | 
|         } | 
|         if (fiterRealTxtWidth) { | 
|             var _width = Mathf.Min(m_Target.rectTransform.rect.width, m_Target.preferredWidth); | 
|             _sizeDelta = m_Target.rectTransform.sizeDelta.SetX(_width) + sizeDelta; | 
|         } | 
|         targetSizeDelta = _sizeDelta; | 
|         targetLocalPositon = m_Target.transform.localPosition + delta; | 
|         if (minSizeDeltaHeight && targetSizeDelta.y < minHeightValue) | 
|         { | 
|             targetSizeDelta.y = minHeightValue; | 
|         } | 
|         if (targetSizeDelta != m_Rt.sizeDelta || targetLocalPositon != m_Rt.localPosition) { | 
|             m_Rt.sizeDelta = targetSizeDelta; | 
|             m_Rt.localPosition = targetLocalPositon; | 
|         } | 
|     } | 
| } | 
| #if UNITY_EDITOR | 
| [CustomEditor(typeof(ImageFitterText))] | 
| [CanEditMultipleObjects] | 
| public class ImageFitterTextEditor : Editor | 
| { | 
|     public ImageFitterText obj; | 
|     public override void OnInspectorGUI() | 
|     { | 
|         obj = target as ImageFitterText; | 
|         base.OnInspectorGUI(); | 
|         EditorGUILayout.BeginHorizontal(); | 
|         if (GUILayout.Button("Execute")) { | 
|             obj.Refresh(); | 
|         } | 
|         EditorGUILayout.EndHorizontal(); | 
|     } | 
| } | 
| #endif |