照片贴纸DIY:主要包括 以下功能:------------------------------------------------>
选择贴纸: 在底部贴纸栏选择你想创建的贴纸
创建贴纸:点击相片即可创建一张你选择样式的贴纸
旋转贴纸:贴纸顶部有个小点,默认不显示(设置的alpha),按上去可左右滑动旋转贴纸(记住是左右,没写上下😂)
缩放贴纸:缩放写了两套,鼠标缩放(主要是为了验证功能); 鼠标左或右键按住贴纸不放,滚轮缩放; #;# 手势缩放(需打包测试或者 用Unity Remote5联机测试...抱歉,我都没做,太懒了)
拖拽贴纸:按住贴纸即可拖拽,做了范围限制
删除贴纸:选中一张生成好的贴纸(抱歉,我也没写选中状态改变),按删除按钮即可删除当前贴纸
演示:
好了,上代码:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.U2D; using UnityEngine.UI; public class StickerMgr : MonoBehaviour { public SpriteAtlas atlas; public GameObject obj; public Transform stickerRoot; //底部选择 int curStickerId = 0; Transform curSel = null; //当前选中的已生成的贴纸(删除用) public static GameObject deleteObj; // Start is called before the first frame update void Start() { } //选择贴纸样式 public void BtnEvent_StickerSelect(Transform t) { curStickerId = int.Parse(t.name); if (curSel) { curSel.localPosition = new Vector3(curSel.localPosition.x, curSel.localPosition.y - 25, 0); } t.localPosition = new Vector3(t.localPosition.x, t.localPosition.y + 25, 0); curSel = t; } //点击照片生成贴纸 public void BtnEvent_ClickPhoto() { if (curStickerId != 0) { CreateSticker(); } } //删除贴纸 public void BtnEvent_Delete() { if (deleteObj) { Destroy(deleteObj); deleteObj = null; } } //创建一张贴纸 void CreateSticker() { GameObject go = Instantiate(obj) as GameObject; go.transform.SetParent(stickerRoot); go.transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition); go.transform.localPosition = new Vector3(go.transform.localPosition.x, go.transform.localPosition.y, 0); go.transform.localScale = Vector3.one; go.transform.localEulerAngles = new Vector3(0, 0, Random.Range(-45, 45)); Image img = go.GetComponent<Image>(); img.sprite = atlas.GetSprite(curStickerId.ToString()); } }
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; public class DragItem : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IPointerDownHandler { public RectTransform photo; //左下、左上、右上、右下 Vector3[] re = new Vector3[4]; void Start() { photo.GetWorldCorners(re); } public void OnPointerDown(PointerEventData eventData) { StickerMgr.deleteObj = this.gameObject; } // begin dragging public void OnBeginDrag(PointerEventData eventData) { SetDraggedPosition(eventData); } // during dragging public void OnDrag(PointerEventData eventData) { SetDraggedPosition(eventData); } // end dragging public void OnEndDrag(PointerEventData eventData) { SetDraggedPosition(eventData); } /// <summary> /// set position of the dragged game object /// </summary> /// <param name="eventData"></param> private void SetDraggedPosition(PointerEventData eventData) { var rt = gameObject.GetComponent<RectTransform>(); // transform the screen point to world point int rectangle Vector3 globalMousePos; if (RectTransformUtility.ScreenPointToWorldPointInRectangle(rt, eventData.position, eventData.pressEventCamera, out globalMousePos)) { float x = globalMousePos.x, y = globalMousePos.y; rt.position = CheckPos(x,y); } } Vector2 CheckPos(float x, float y) { if (x < re[0].x) x = re[0].x; else if (x > re[2].x) x = re[2].x; if (y > re[2].y) y = re[2].y; else if (y < re[0].y) y = re[0].y; return new Vector2(x, y); } }
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; public class ScaleItem : MonoBehaviour, IPointerDownHandler, IPointerUpHandler { [Tooltip("最小缩放值")] public float minScale; [Tooltip("最大缩放值")] public float maxScale; bool pressFlag = false; //记录单个触屏位置 Vector2 screenPos = new Vector2(); //手指位置 Vector2 finger1 = new Vector2(), finger2 = new Vector2(); //手指移动距离 Vector2 mov1 = new Vector2(), mov2 = new Vector2(); //距离 float distance1 = 0, distance2 = 0; // Start is called before the first frame update void Start() { //开启多点触屏 Input.multiTouchEnabled = true; } void Update() { if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer) { //手势操作 FingerTouch(); } else { //鼠标滚动模拟 MouseControl(); } } public void OnPointerDown(PointerEventData eventData) { //print("按下"); pressFlag = true; } public void OnPointerUp(PointerEventData eventData) { //print("抬起"); pressFlag = false; } void FingerTouch() { if (Input.touchCount <= 0) return; if (Input.touchCount > 1) { for (int i = 0; i < Input.touchCount; ++i) { Touch touch = Input.touches[i]; if (touch.phase == TouchPhase.Ended) break; if (touch.phase == TouchPhase.Began) { if (i == 0) finger1 = touch.position; else finger2 = touch.position; distance1 = Vector2.Distance(finger1, finger2); } if (touch.phase == TouchPhase.Moved) { if (i == 0) finger1 = touch.position; else finger2 = touch.position; distance2 = Vector2.Distance(finger1, finger2); if (CheckTouchScale(distance1, distance2)) { //放大 ToScale(0.025f, true); } else { //缩小 ToScale(0.025f, false); } } } } } //手势缩放检测 bool CheckTouchScale(float dis1, float dis2) { return Mathf.Abs(dis1) - Mathf.Abs(dis2) >= 0; } //放大或缩小 void ToScale(float change, bool toBig) { if (!pressFlag) { return; } if (toBig) { if (transform.localScale.x < maxScale) { float size = transform.localScale.x + change; transform.localScale = new Vector3(size, size, 1); } } else { if (transform.localScale.x > minScale) { float size = transform.localScale.x - change; transform.localScale = new Vector3(size, size, 1); } } } void MouseControl() { //鼠标中键滚动 if (Input.GetAxis("Mouse ScrollWheel") > 0) { ToScale(0.025f, true); } else if (Input.GetAxis("Mouse ScrollWheel") < 0) { ToScale(0.025f, false); } } }
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; public class RotateItem : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler { Image selfImg; Transform parent; // Start is called before the first frame update void Start() { selfImg = transform.GetComponent<Image>(); parent = transform.parent; selfImg.color = new Color(1, 1, 1, 0.1f); } public void OnPointerDown(PointerEventData eventData) { selfImg.color = new Color(1, 1, 1, 1); StickerMgr.deleteObj = this.parent.gameObject; } public void OnPointerUp(PointerEventData eventData) { selfImg.color = new Color(1, 1, 1, 0.1f); } // during dragging public void OnDrag(PointerEventData eventData) { Vector3 v = parent.localEulerAngles; parent.localEulerAngles = new Vector3(0, 0, v.z - eventData.delta.x); } }
项目包地址:链接: https://pan.baidu.com/s/1KspQZGnUeNhVd-jfo3ynYw 提取码: 7dw2