拿一張圖片剪切好備用
在Canvas下新建panel作為父物體
在下面建一個Image名為——Cell
在Cell下新建image,改Tag為Cell
在這個image上掛腳本:
using UnityEngine; using System.Collections; using UnityEngine.EventSystems; public class DragOnPic : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler { //記錄下自己的父物體. Transform myParent; //Panel,使拖拽是顯示在最上方. Transform tempParent; CanvasGroup cg; RectTransform rt; //記錄鼠標位置. Vector3 newPosition; void Awake() { //添加CanvasGroup組件用於在拖拽是忽略自己,從而檢測到被交換的圖片. cg = this.gameObject.AddComponent<CanvasGroup>(); rt = this.GetComponent<RectTransform>(); tempParent = GameObject.Find("Canvas").transform; } /// <summary> /// Raises the begin drag event. /// </summary> public void OnBeginDrag(PointerEventData eventData) { //拖拽開始時記下自己的父物體. myParent = transform.parent; //拖拽開始時禁用檢測. cg.blocksRaycasts = false; this.transform.SetParent(tempParent); } /// <summary> /// Raises the drag event. /// </summary> void IDragHandler.OnDrag(PointerEventData eventData) { //推拽是圖片跟隨鼠標移動. RectTransformUtility.ScreenPointToWorldPointInRectangle(rt, Input.mousePosition, eventData.enterEventCamera, out newPosition); transform.position = newPosition; } /// <summary> /// Raises the end drag event. /// </summary> public void OnEndDrag(PointerEventData eventData) { //獲取鼠標下面的物體. GameObject target = eventData.pointerEnter; //如果能檢測到物體. if (target) { //如果檢測到圖片,則交換父物體並重置位置. GameManager.SetParent(this.transform, target.transform, myParent); } else { this.transform.SetParent(myParent); this.transform.localPosition = Vector3.zero; } //拖拽結束時啟用檢測. cg.blocksRaycasts = true; //檢測是否完成拼圖. if (GameManager.CheckWin()) { Debug.Log("Win!!!"); } if (!GameManager.CheckWin()) { Debug.Log("沒拼好"); } } }
新建個腳本GameManager類,隨機生成圖片位置和拖拽時交換父物體和位置,不需要掛,
using System.Collections; using System.Collections.Generic; using UnityEngine; public class GameManager { /// <summary> /// Randoms the array. /// </summary> static public void RandomArray(Sprite[] sprites) { for (int i = 0; i < sprites.Length; i++) { //隨機抽取數字中的一個位置,並將這張圖片與第i張圖片交換. int index = Random.Range(i, sprites.Length); Sprite temp = sprites[i]; sprites[i] = sprites[index]; sprites[index] = temp; } } /// <summary> /// Sets the parent. /// </summary> static public void SetParent(Transform mine, Transform target, Transform oldParent) { //如果檢測到圖片,則交換父物體並重置位置. switch (target.tag) { case "Cell": mine.SetParent(target.parent); target.SetParent(oldParent); mine.localPosition = Vector3.zero; target.localPosition = Vector3.zero; break; default: mine.SetParent(oldParent); mine.localPosition = Vector3.zero; break; } } /// <summary> /// Checks is win. /// </summary> static public bool CheckWin() { for (int i = 0; i < ImageCreater._instance.transform.childCount; i++) { if (ImageCreater._instance.transform.GetChild(i).name != ImageCreater._instance.transform.GetChild(i).transform.GetChild(0).name) { return false; } } return true; } }
在panel上添加組件:Grid Layput Group 掛上腳本:
using UnityEngine; using System.Collections; using UnityEngine.UI; public class ImageCreater : MonoBehaviour { //單例模式 public static ImageCreater _instance; //存儲裁剪好圖片的數組. public Sprite[] sprites; //格子的預設體. public GameObject cellPrefab; void Start() { _instance = this; CreateImages(); } private void CreateImages() { //將圖片數組隨機排列. GameManager.RandomArray(sprites); //生產圖片. for (int i = 0; i < sprites.Length; i++) { //通過預設體生成圖片. GameObject cell = (GameObject)Instantiate(cellPrefab); //設置cell的名字方便檢測是否完成拼圖. cell.name = i.ToString(); //獲取cell的子物體. Transform image = cell.transform.GetChild(0); //設置顯示的圖片. image.GetComponent<Image>().sprite = sprites[i]; //設置子物體的名稱,方便檢測是否完成拼圖. int tempIndex = sprites[i].name.LastIndexOf('_'); image.name = sprites[i].name.Substring(tempIndex + 1); //將Cell設置為Panel的子物體. cell.transform.SetParent(this.transform); //初始化大小. cell.transform.localScale = Vector3.one; } } }
運行即可生成圖片,並達到拖拽,換位置信息的功能: