unity UGUI UV按纹理大小平铺并拖动

唔?重新温习一下平铺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;
    }

    /// 
    /// Returns the texture used to draw this Graphic.
    /// 
    public override Texture mainTexture
    {
        get
        {
            if (m_Texture == null)
            {
                if (material != null && material.mainTexture != null)
                {
                    return material.mainTexture;
                }
                return s_WhiteTexture;
            }

            return m_Texture;
        }
    }

    /// 
    /// Texture to be used.
    /// 
    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);
            uvOffset /= transform.localScale;
            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();
            if (com == null)
            {
                select = FindCanvas();
            }
        }

        var go = new GameObject("Image Title");
        go.AddComponent();
        go.layer = 5;
        go.AddComponent();
        if(select!=null) go.transform.SetParent(select);
    }

    private static Transform FindCanvas()
    {
        var objs = GameObject.FindObjectsOfType();
        if (objs.Length > 0)
        {
            return objs[objs.Length - 1].transform;
        }
        else
        {
           return CreateCanvas();
        }
    }

    private static Transform CreateCanvas()
    {
        var go = new GameObject();
        go.AddComponent();
        go.AddComponent();
        go.AddComponent();
        return go.AddComponent();
    }

    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();
    }
}

 

你可能感兴趣的:(unity3d,uv,平铺,拖拽,unity,imge)