当前位置:   article > 正文

Unity 裁剪_unity 头像裁剪

unity 头像裁剪

最近用unity在做一个照片编辑的功能,所以就从框架里提炼出了一下代码,弄了一个小demo,话不多说,直接看效果。用的上的代码自取

请添加图片描述

##简单说一下逻辑吧,脚本一共就两个,一个拖拽脚本,一个处理图像的脚本
#场景里的UI层级如下图

在这里插入图片描述

#唯一挂载的脚本里面的东西是这些,如下图。(ps:我懒就全用拖拽的方式了)

在这里插入图片描述

#处理逻辑的脚本,代码比较简单,对着项目运行一下,简单看看就全懂了,里面也有部分注释
能拿到Sprite自然能对Sprite做你想做的事情

using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class Cutting : MonoBehaviour
{

public Sprite selectPhoto;
public Transform Obj;
public Button clickBtn;
public Button okBtn;
public Material guideMat;
private float cutRangeWidth;
private float cutRangeHeight;
private float ratio = 0;
private string curState = "CutFreeButton";

public Image tempImage;
public Image showImg;

private DragScript ltDrag;
private DragScript rtDrag;
private DragScript lbDrag;
private DragScript rbDrag;
private DragScript cenDrag;


public Toggle cutFreeTog;
public Toggle cut23Tog;
public Toggle cut11Tog;
public Toggle cut34Tog;
public Toggle cut43Tog;

RectTransform rbRect;
RectTransform lbRect;
RectTransform rtRect;
RectTransform ltRect;
RectTransform cen;


private void Start()
{
    clickBtn.onClick.AddListener(ShowCut);
    okBtn.onClick.AddListener(SetCut);
    ltDrag = Obj.Find("CutEditorLT").gameObject.AddComponent<DragScript>();
    rtDrag = Obj.Find("CutEditorRT").gameObject.AddComponent<DragScript>();
    lbDrag = Obj.Find("CutEditorLB").gameObject.AddComponent<DragScript>();
    rbDrag = Obj.Find("CutEditorRB").gameObject.AddComponent<DragScript>();
    cenDrag = Obj.Find("CutEditorCen").gameObject.AddComponent<DragScript>();
    ltDrag.Draging += DragLT;
    rtDrag.Draging += DragRT;
    lbDrag.Draging += DragLB;
    rbDrag.Draging += DragRB;
    cenDrag.Draging += DragCen;

    cutFreeTog.onValueChanged.AddListener(CutFree);
    cut23Tog.onValueChanged.AddListener(Cut23);
    cut11Tog.onValueChanged.AddListener(Cut11);
    cut34Tog.onValueChanged.AddListener(Cut34);
    cut43Tog.onValueChanged.AddListener(Cut43);

    rbRect = Obj.Find("CutEditorRB").GetComponent<RectTransform>();
    lbRect = Obj.Find("CutEditorLB").GetComponent<RectTransform>();
    rtRect = Obj.Find("CutEditorRT").GetComponent<RectTransform>();
    ltRect = Obj.Find("CutEditorLT").GetComponent<RectTransform>();
    cen = Obj.Find("CutEditorCen").GetComponent<RectTransform>();
   
}

/// <summary>
/// 裁剪(进入裁剪界面,初始化数据,其实也没啥可初始化的)
/// </summary>
private void ShowCut()
{
    cutRangeWidth = tempImage.preferredWidth;
    cutRangeHeight = tempImage.preferredHeight;
    
    Obj.Find("GuideMask").GetComponent<RectTransform>().sizeDelta = new Vector2(cutRangeWidth, cutRangeHeight);

    Obj.Find("CutEditorLT").GetComponent<Image>().color = Color.red;
    Obj.Find("CutEditorRT").GetComponent<Image>().color = Color.red;
    Obj.Find("CutEditorLB").GetComponent<Image>().color = Color.red;
    Obj.Find("CutEditorRB").GetComponent<Image>().color = Color.red;

    CutFree(false);
}


private void CutFree(bool isSelect)
{
    ChangeButtonState("CutFreeButton");

    float width = cutRangeWidth;
    float height = cutRangeHeight;
    UpdateCen(width, height, Vector2.zero);
    
    ltRect.anchoredPosition = new Vector2(-width / 2, height / 2);
    rtRect.anchoredPosition = new Vector2(width / 2, height / 2);
    lbRect.anchoredPosition = new Vector2(-width / 2, -height / 2);
    rbRect.anchoredPosition = new Vector2(width / 2, -height / 2);
}

private void Cut23(bool isSelect)
{
    ChangeButtonState("Cut23Button");
    SetCutRate(2 / 3f);
}

private void Cut11(bool isSelect)
{
    ChangeButtonState("Cut11Button");
    SetCutRate(1 / 1f);
}

private void Cut34(bool isSelect)
{
    ChangeButtonState("Cut34Button");
    SetCutRate(3 / 4f);
}

private void Cut43(bool isSelect)
{
    ChangeButtonState("Cut43Button");
    SetCutRate(4 / 3f);
}

//设置比例
private void SetCutRate(float rate)
{
    float width;
    float height;
    if (cutRangeWidth / cutRangeHeight > rate)
    {
        height = cutRangeHeight;
        width = cutRangeHeight * rate;
    }
    else
    {
        width = cutRangeWidth;
        height = cutRangeWidth / rate;
    }
    ltRect.anchoredPosition = new Vector2(-width / 2, height / 2);
    rtRect.anchoredPosition = new Vector2(width / 2, height / 2);
    lbRect.anchoredPosition = new Vector2(-width / 2, -height / 2);
    rbRect.anchoredPosition = new Vector2(width / 2, -height / 2);
    UpdateCen(width, height, Vector2.zero);
}

//处理Toggle按钮时间,设定比例,只能按照规定比例更改大小
private void ChangeButtonState(string objName)
{
    if (curState == objName)
        return;
    if (!string.IsNullOrEmpty(curState))

    curState = objName;
    switch (curState)
    {
        case "CutFreeButton":
            ratio = 0;
            break;
        case "Cut23Button":
            ratio = 2 / 3f;
            break;
        case "Cut11Button":
            ratio = 1 / 1f;
            break;
        case "Cut34Button":
            ratio = 3 / 4f;
            break;
        case "Cut43Button":
            ratio = 4 / 3f;
            break;
    }
}


#region   拖动角标事件
private void DragRB(PointerEventData data)
{
    if (Input.touchCount > 1)
        return;

    //Vector2 delta = Input.GetTouch(0).deltaPosition;
    Vector2 delta = data.delta;
    if (ratio != 0)
    {
        if (Mathf.Abs(delta.x) > Mathf.Abs(delta.y))
        {
            delta.y = delta.x / ratio * -1;
        }
        else
        {
            delta.x = delta.y * ratio * -1;
        }
    }
    

    Vector2 oriPos = rbRect.anchoredPosition;
    rbRect.transform.position += (Vector3)delta;
    Vector2 endPos = rbRect.anchoredPosition;
    bool state = ratio == 0 ? true : false;
    if (endPos.x > cutRangeWidth / 2)
    {
        if (ratio == 0)
            endPos.x = cutRangeWidth / 2;
        else
            endPos = oriPos;
    }
    else if (endPos.x < ltRect.anchoredPosition.x + 150)
    {
        if (ratio == 0)
            endPos.x = ltRect.anchoredPosition.x + 150;
        else
            endPos = oriPos;
    }
    if (endPos.y > ltRect.anchoredPosition.y - 100)
    {
        if (ratio == 0)
            endPos.y = ltRect.anchoredPosition.y - 100;
        else
            endPos = oriPos;
    }
    else if (endPos.y < -cutRangeHeight / 2)
    {
        if (ratio == 0)
            endPos.y = -cutRangeHeight / 2;
        else
            endPos = oriPos;
    }
    rbRect.anchoredPosition = endPos;

    lbRect.anchoredPosition = new Vector2(lbRect.anchoredPosition.x, endPos.y);
    rtRect.anchoredPosition = new Vector3(endPos.x, rtRect.anchoredPosition.y);

    Vector2 pos = Vector2.Lerp(lbRect.anchoredPosition, rtRect.anchoredPosition, 0.5f);
    float width = rtRect.anchoredPosition.x - lbRect.anchoredPosition.x;
    float height = rtRect.anchoredPosition.y - lbRect.anchoredPosition.y;
    UpdateCen(width, height, pos);
}

private void DragLB(PointerEventData data)
{
    //禁止双手操作
    if (Input.touchCount > 1)
        return;

    //Vector2 delta = Input.GetTouch(0).deltaPosition;
    Vector2 delta = data.delta;
    
    if (ratio != 0)
    {
        if (Mathf.Abs(delta.x) > Mathf.Abs(delta.y))
        {
            delta.y = delta.x / ratio;
        }
        else
        {
            delta.x = delta.y * ratio;
        }
    }
    

    Vector2 oriPos = lbRect.anchoredPosition;
    lbRect.transform.position += (Vector3)delta;
    Vector2 endPos = lbRect.anchoredPosition;
    if (endPos.x < -cutRangeWidth / 2)
    {
        if (ratio == 0)
            endPos.x = -cutRangeWidth / 2;
        else
            endPos = oriPos;
    }
    else if (endPos.x > rtRect.anchoredPosition.x - 150)
    {
        if (ratio == 0)
            endPos.x = rtRect.anchoredPosition.x - 150;
        else
            endPos = oriPos;
    }
    if (endPos.y > rtRect.anchoredPosition.y - 100)
    {
        if (ratio == 0)
            endPos.y = rtRect.anchoredPosition.y - 100;
        else
            endPos = oriPos;
    }
    else if (endPos.y < -cutRangeHeight / 2)
    {
        if (ratio == 0)
            endPos.y = -cutRangeHeight / 2;
        else
            endPos = oriPos;
    }
    lbRect.anchoredPosition = endPos;

    ltRect.anchoredPosition = new Vector3(endPos.x, rtRect.anchoredPosition.y);
    rbRect.anchoredPosition = new Vector2(rtRect.anchoredPosition.x, endPos.y);

    Vector2 pos = Vector2.Lerp(ltRect.anchoredPosition, rbRect.anchoredPosition, 0.5f);
    float width = rbRect.anchoredPosition.x - ltRect.anchoredPosition.x;
    float height = ltRect.anchoredPosition.y - rbRect.anchoredPosition.y;
    UpdateCen(width, height, pos);
}

private void DragRT(PointerEventData data)
{
    if (Input.touchCount > 1)
        return;

    //Vector2 delta = Input.GetTouch(0).deltaPosition;
    Vector2 delta = data.delta;
    if (ratio != 0)
    {
        if (Mathf.Abs(delta.x) > Mathf.Abs(delta.y))
        {
            delta.y = delta.x / ratio;
        }
        else
        {
            delta.x = delta.y * ratio;
        }
    }

    Vector2 oriPos = rtRect.anchoredPosition;
    rtRect.transform.position += (Vector3)delta;
    Vector2 endPos = rtRect.anchoredPosition;
    if (endPos.x > cutRangeWidth / 2)
    {
        if (ratio == 0)
            endPos.x = cutRangeWidth / 2;
        else
            endPos = oriPos;
    }
    else if (endPos.x < lbRect.anchoredPosition.x + 150)
    {
        if (ratio == 0)
            endPos.x = lbRect.anchoredPosition.x + 150;
        else
            endPos = oriPos;
    }
    if (endPos.y < lbRect.anchoredPosition.y + 100)
    {
        if (ratio == 0)
            endPos.y = lbRect.anchoredPosition.y + 100;
        else
            endPos = oriPos;
    }
    else if (endPos.y > cutRangeHeight / 2)
    {
        if (ratio == 0)
            endPos.y = cutRangeHeight / 2;
        else
            endPos = oriPos;
    }
    rtRect.anchoredPosition = endPos;

    rbRect.anchoredPosition = new Vector3(endPos.x, lbRect.anchoredPosition.y);
    ltRect.anchoredPosition = new Vector2(lbRect.anchoredPosition.x, endPos.y);

    Vector2 pos = Vector2.Lerp(ltRect.anchoredPosition, rbRect.anchoredPosition, 0.5f);
    float width = rbRect.anchoredPosition.x - ltRect.anchoredPosition.x;
    float height = ltRect.anchoredPosition.y - rbRect.anchoredPosition.y;
    UpdateCen(width, height, pos);
}

private void DragLT(PointerEventData data)
{
    if (Input.touchCount > 1)
        return;

    //Vector2 delta = Input.GetTouch(0).deltaPosition;

    //Vector2 delta = Input.mouseScrollDelta;
    //Debug.Log(data.delta);
    Vector2 delta = data.delta;

    if (ratio != 0)
    {
        if (Mathf.Abs(delta.x) > Mathf.Abs(delta.y))
        {
            delta.y = delta.x / ratio * -1;
        }
        else
        {
            delta.x = delta.y * ratio * -1;
        }
    }

    Vector2 oriPos = ltRect.anchoredPosition;
    ltRect.transform.position += (Vector3)delta;
    Vector2 endPos = ltRect.anchoredPosition;
    if (endPos.x < -cutRangeWidth / 2)
    {
        if (ratio == 0)
            endPos.x = -cutRangeWidth / 2;
        else
            endPos = oriPos;
    }
    else if (endPos.x > rbRect.anchoredPosition.x - 150)
    {
        if (ratio == 0)
            endPos.x = rbRect.anchoredPosition.x - 150;
        else
            endPos = oriPos;
    }
    if (endPos.y < rbRect.anchoredPosition.y + 100)
    {
        if (ratio == 0)
            endPos.y = rbRect.anchoredPosition.y + 100;
        else
            endPos = oriPos;
    }
    else if (endPos.y > cutRangeHeight / 2)
    {
        if (ratio == 0)
            endPos.y = cutRangeHeight / 2;
        else
            endPos = oriPos;
    }
    ltRect.anchoredPosition = endPos;

    lbRect.anchoredPosition = new Vector2(endPos.x, rbRect.anchoredPosition.y);
    rtRect.anchoredPosition = new Vector3(rbRect.anchoredPosition.x, endPos.y);

    Vector2 pos = Vector2.Lerp(ltRect.anchoredPosition, rbRect.anchoredPosition, 0.5f);
    float width = rtRect.anchoredPosition.x - lbRect.anchoredPosition.x;
    float height = rtRect.anchoredPosition.y - lbRect.anchoredPosition.y;
    UpdateCen(width, height, pos);
}

private void DragCen(PointerEventData data)
{
    Transform cen = Obj.Find("CutEditorCen").transform;
    RectTransform cenRect = Obj.Find("CutEditorCen").transform.GetComponent<RectTransform>();

    //Debug.Log(cen.position);

    float x = cen.position.x + data.delta.x;
    float y = cen.position.y + data.delta.y;

    //做限定  防止中心框拖出图片框
    float cenX = Mathf.Clamp(x, tempImage.transform.position.x - cutRangeWidth * 0.5f + cenRect.rect.width * 0.5f, tempImage.transform.position.x + cutRangeWidth * 0.5f - cenRect.rect.width * 0.5f);
    float cenY = Mathf.Clamp(y, tempImage.transform.position.y - cutRangeHeight * 0.5f + cenRect.rect.height * 0.5f, tempImage.transform.position.y + cutRangeHeight * 0.5f - cenRect.rect.height * 0.5f);

    cen.position = new Vector2(cenX, cenY);

    Vector2 cenPos = cen.gameObject.GetComponent<RectTransform>().anchoredPosition;
    float width = rtRect.anchoredPosition.x - lbRect.anchoredPosition.x;
    float height = rtRect.anchoredPosition.y - lbRect.anchoredPosition.y;

    if (cenPos.x < (-cutRangeWidth + width) / 2)
        cenPos.x = (-cutRangeWidth + width) / 2;
    else if (cenPos.x > (cutRangeWidth - width) / 2)
        cenPos.x = (cutRangeWidth - width) / 2;
    if (cenPos.y < (-cutRangeHeight + height) / 2)
        cenPos.y = (-cutRangeHeight + height) / 2;
    else if (cenPos.y > (cutRangeHeight - height) / 2)
        cenPos.y = (cutRangeHeight - height) / 2;

    ltRect.anchoredPosition = new Vector2(cenPos.x - width / 2, cenPos.y + height / 2);
    rtRect.anchoredPosition = new Vector2(cenPos.x + width / 2, cenPos.y + height / 2);
    lbRect.anchoredPosition = new Vector2(cenPos.x - width / 2, cenPos.y - height / 2);
    rbRect.anchoredPosition = new Vector2(cenPos.x + width / 2, cenPos.y - height / 2);

    UpdateGuide(width, height, cenPos);
}

#endregion


//更新中心点坐标
private void UpdateCen(float width, float height, Vector2 pos)
{
    var cen = Obj.Find("CutEditorCen").GetComponent<RectTransform>();
    cen.sizeDelta = new Vector2(width, height);
    cen.anchoredPosition = pos;

    UpdateGuide(width, height, cen.anchoredPosition);
}

//更新遮罩信息
private void UpdateGuide(float width, float height, Vector2 rectPos)
{
    guideMat.SetFloat("_Width", width);
    guideMat.SetFloat("_Height", height);
    guideMat.SetVector("_Center", new Vector4(rectPos.x, rectPos.y , 0, 0));
}

/// <summary>
/// 确认裁剪
/// </summary>
private void SetCut()
{
    float rate = 1;
    if (selectPhoto.texture.width / selectPhoto.texture.height >= 1f)
    {
        rate = selectPhoto.texture.height / cutRangeHeight;
    }
    else
    {
        rate = selectPhoto.texture.width / cutRangeWidth;
    }

    RectTransform lt = Obj.Find("CutEditorLT").GetComponent<RectTransform>();
    RectTransform rb = Obj.Find("CutEditorRB").GetComponent<RectTransform>();
    RectTransform cen =Obj.Find("CutEditorCen").GetComponent<RectTransform>();
    float width = (rb.anchoredPosition.x - lt.anchoredPosition.x) * rate;
    float height = (lt.anchoredPosition.y - rb.anchoredPosition.y) * rate;
    Vector2 cenPos = new Vector2(cen.anchoredPosition.x * rate + selectPhoto.texture.width / 2, cen.anchoredPosition.y * rate + selectPhoto.texture.height / 2);
    //InitProject.Instance.StartDoCoroutine(ProjectBehaviour.Instence.CutSprite(selectPhoto, Mathf.CeilToInt(width), Mathf.CeilToInt(height), cenPos, (result) =>
    //{
    //    ScreenAdaptation(result);
    //}));
    CutSprite(selectPhoto, Mathf.CeilToInt(width), Mathf.CeilToInt(height), cenPos, UpdataImage);

}


public void CutSprite(Sprite oriSprite, int width, int height, Vector2 center, Action<Sprite> finishedCB = null)
{
    Texture2D newTexture = new Texture2D(Mathf.CeilToInt(width), Mathf.CeilToInt(height));


    //遍历写入像素的过程
    int start_X = (int)(center.x - width / 2);
    start_X = start_X > 0 ? start_X : 1;
    int start_Y = (int)(center.y - height / 2);
    start_Y = start_Y > 0 ? start_Y : 1;
    int end_X = (int)(center.x + width / 2);
    end_X = end_X < oriSprite.rect.width ? end_X : (int)oriSprite.rect.width;
    int end_Y = (int)(center.y + height / 2);
    end_Y = end_Y < oriSprite.rect.height ? end_Y : (int)oriSprite.rect.height;
    
    for (int y = start_Y - 1; y < end_Y; y++)
    {
        for (int x = start_X - 1; x < end_X; x++)
        {
            Color color = oriSprite.texture.GetPixel(x, y);
            newTexture.SetPixel(x - start_X + 1, y - start_Y + 1, color);
        }
    }

    newTexture.anisoLevel = 2;
    newTexture.Apply();
    Sprite result = Sprite.Create(newTexture, new Rect(0, 0, newTexture.width, newTexture.height), new Vector2(0.5f, 0.5f));
    finishedCB?.Invoke(result);
}

public void UpdataImage(Sprite sprite)
{
    showImg.sprite = sprite;
    showImg.SetNativeSize();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447
  • 448
  • 449
  • 450
  • 451
  • 452
  • 453
  • 454
  • 455
  • 456
  • 457
  • 458
  • 459
  • 460
  • 461
  • 462
  • 463
  • 464
  • 465
  • 466
  • 467
  • 468
  • 469
  • 470
  • 471
  • 472
  • 473
  • 474
  • 475
  • 476
  • 477
  • 478
  • 479
  • 480
  • 481
  • 482
  • 483
  • 484
  • 485
  • 486
  • 487
  • 488
  • 489
  • 490
  • 491
  • 492
  • 493
  • 494
  • 495
  • 496
  • 497
  • 498
  • 499
  • 500
  • 501
  • 502
  • 503
  • 504
  • 505
  • 506
  • 507
  • 508
  • 509
  • 510
  • 511
  • 512
  • 513
  • 514
  • 515
  • 516
  • 517
  • 518
  • 519
  • 520
  • 521
  • 522
  • 523
  • 524
  • 525
  • 526
  • 527
  • 528
  • 529
  • 530
  • 531
  • 532
  • 533
  • 534
  • 535
  • 536
  • 537
  • 538
  • 539
  • 540
  • 541
  • 542
  • 543
  • 544
  • 545
  • 546
  • 547

}

#拖动脚本,需要挂载到四个角标和中心图片上的

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class DragScript : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler
{

    public Action<PointerEventData> BeginDrag;
    public Action<PointerEventData> EndDrag;
    public Action<PointerEventData> Draging;


    public void OnBeginDrag(PointerEventData eventData)
    {
        BeginDrag?.Invoke(eventData);
    }

    public void OnDrag(PointerEventData eventData)
    {
        Draging?.Invoke(eventData);
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        EndDrag?.Invoke(eventData);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

工程地址,用的自取
链接:https://pan.baidu.com/s/1d4sKndjHJvmnWoT5nOZ8pA
提取码:tshm

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/636980
推荐阅读
相关标签
  

闽ICP备14008679号