unity UGUI UV按紋理大小平鋪並拖動
阿新 • • 發佈:2019-01-31
唔?重新溫習一下平鋪UV
using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; [AddComponentMenu("UI/ImageTile", 16)] public class ImageTile : MaskableGraphic, IBeginDragHandler, IDragHandler { [SerializeField] Texture m_Texture; [SerializeField] Vector2 m_UVPivot; [HideInInspector] public Vector2 offsetPos; Vector2 oldPos; protected ImageTile() { useLegacyMeshGeneration = false; } /// <summary> /// Returns the texture used to draw this Graphic. /// </summary> public override Texture mainTexture { get { if (m_Texture == null) { if (material != null && material.mainTexture != null) { return material.mainTexture; } return s_WhiteTexture; } return m_Texture; } } /// <summary> /// Texture to be used. /// </summary> public Texture texture { get { return m_Texture; } set { if (m_Texture == value) return; m_Texture = value; SetVerticesDirty(); SetMaterialDirty(); } } protected override void OnPopulateMesh(VertexHelper vh) { Texture tex = mainTexture; vh.Clear(); if (tex != null) { var r = GetPixelAdjustedRect(); var v = new Vector4(r.x, r.y, r.x + r.width, r.y + r.height); var uvOffset = new Vector2(offsetPos.x + v.x * m_UVPivot.x, offsetPos.y + v.y * m_UVPivot.y); var uv = new Vector4( (v.x + uvOffset.x) * tex.texelSize.x, (v.y + uvOffset.y) * tex.texelSize.y, (v.z + uvOffset.x) * tex.texelSize.x, (v.w + uvOffset.y) * tex.texelSize.y ); { var color32 = color; vh.AddVert(new Vector3(v.x, v.y), color32, new Vector2(uv.x, uv.y)); vh.AddVert(new Vector3(v.x, v.w), color32, new Vector2(uv.x, uv.w)); vh.AddVert(new Vector3(v.z, v.w), color32, new Vector2(uv.z, uv.w)); vh.AddVert(new Vector3(v.z, v.y), color32, new Vector2(uv.z, uv.y)); vh.AddTriangle(0, 1, 2); vh.AddTriangle(2, 3, 0); } } } public void OnBeginDrag(PointerEventData eventData) { if (!raycastTarget) return; oldPos = eventData.position + offsetPos; } public void OnDrag(PointerEventData eventData) { if (!raycastTarget) return; offsetPos = oldPos - eventData.position; SetVerticesDirty(); } }
using UnityEngine; using UnityEditor.UI; using UnityEngine.UI; using UnityEditor; [CustomEditor(typeof(ImageTile),true)] public class ImageTitleEditor : GraphicEditor { SerializedProperty m_Texture; SerializedProperty m_UVPivot; GUIContent m_UVPivotContent; [MenuItem("GameObject/UI/ImageTitle", priority = 0)] private static void CreateImageTitle() { var select = Selection.activeTransform; if (select == null) { select = FindCanvas(); } else { var com = select.GetComponent<Graphic>(); if (com == null) { select = FindCanvas(); } } var go = new GameObject("Image Title"); go.AddComponent<RectTransform>(); go.layer = 5; go.AddComponent<ImageTile>(); if(select!=null) go.transform.SetParent(select); } private static Transform FindCanvas() { var objs = GameObject.FindObjectsOfType<Canvas>(); if (objs.Length > 0) { return objs[objs.Length - 1].transform; } else { return CreateCanvas(); } } private static Transform CreateCanvas() { var go = new GameObject(); go.AddComponent<Canvas>(); go.AddComponent<CanvasScaler>(); go.AddComponent<GraphicRaycaster>(); return go.AddComponent<RectTransform>(); } protected override void OnEnable() { base.OnEnable(); m_UVPivotContent = new GUIContent("UV Pivot"); m_Texture = serializedObject.FindProperty("m_Texture"); m_UVPivot = serializedObject.FindProperty("m_UVPivot"); } public override void OnInspectorGUI() { serializedObject.Update(); EditorGUILayout.PropertyField(m_Texture); AppearanceControlsGUI(); RaycastControlsGUI(); EditorGUILayout.PropertyField(m_UVPivot, m_UVPivotContent); NativeSizeButtonGUI(); serializedObject.ApplyModifiedProperties(); } }