本文主要是介绍unity3d拼图__附带资源,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
要达到吸附效果则需要每个拼图上挂载碰撞体
达到整张图片的替换效果需要选中所有拼图部件把材质球拖上去
、
制作材料球
脚本挂载到随便一个空物体上
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class PinTu : MonoBehaviour
{// 记录有没有拼图被选中private GameObject selectedObject;// 拼图预制体GameObject[] dragObj;// 记录拼图应该放的点GameObject[] dropObj;// Start is called before the first frame updateqvoid Start(){dragObj = GameObject.FindGameObjectsWithTag("drag");dropObj = GameObject.FindGameObjectsWithTag("drop");foreach (var item in dragObj){item.transform.position = new Vector3(Random.Range(-0.2f, 0.3f), 0.815f, Random.Range(-0.1f,0.4f));}}// Update is called once per framevoid Update(){// 如果按下了鼠标左键if (Input.GetMouseButtonDown(0)){// 选中的物体为空if (selectedObject == null){// 存储的射线信息RaycastHit hit = CastRay();// 碰到的物体有碰撞器if (hit.collider != null){// 如果标签不是drag直接返回if (!hit.collider.CompareTag("drag")){return;}// 碰到的物体标签是--drag,为selectedObject赋值selectedObject = hit.collider.gameObject;// 设置鼠标光标不可见Cursor.visible = false;}}// 选中物体之后,再按一下鼠标左键---放下物体else{// 记录鼠标点击的点Vector3 position = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.WorldToScreenPoint(selectedObject.transform.position).z);Vector3 worldPosition = Camera.main.ScreenToWorldPoint(position);#region 吸附//遍历每个放置点,找到和鼠标点击最近的点Vector3 tmpdrop = Vector3.zero;float minDistance = 10.0f;foreach (var item in dropObj){if (Vector3.Distance(item.transform.position, worldPosition) <= minDistance){minDistance = Vector3.Distance(item.transform.position, worldPosition);tmpdrop = item.transform.position;}}// 如果最小距离小于---限定值,说明在拼图位置上,就赋值,否则就是鼠标位置if (minDistance < 0.04f){// 赋值selectedObject.transform.position = tmpdrop + new Vector3(0, 0.003f, 0);}else{selectedObject.transform.position = worldPosition;}#endregion//selectedObject.transform.position = worldPosition;selectedObject = null;Cursor.visible = true;}}// 如果selectedObject不为空,说明点击到了物体,物体跟随鼠标移动if (selectedObject != null){// position存储的是鼠标的x,y坐标,自身的z坐标---屏幕坐标Vector3 position = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.WorldToScreenPoint(selectedObject.transform.position).z);// 将屏幕坐标,转换为世界坐标Vector3 worldPosition = Camera.main.ScreenToWorldPoint(position);// 为选中的物体赋值--这里的y是固定的0.25selectedObject.transform.position = new Vector3(worldPosition.x, 0.811f, worldPosition.z);// 按下鼠标的右键,旋转if (Input.GetMouseButtonDown(1)){selectedObject.transform.rotation = Quaternion.Euler(new Vector3(selectedObject.transform.rotation.eulerAngles.x,selectedObject.transform.rotation.eulerAngles.y + 90f,selectedObject.transform.rotation.eulerAngles.z));}}}// 返回射线碰撞信息private RaycastHit CastRay(){// 射线最远的点Vector3 screenMousePosFar = new Vector3(Input.mousePosition.x,Input.mousePosition.y,Camera.main.farClipPlane);// 射线最近的点Vector3 screenMousePosNear = new Vector3(Input.mousePosition.x,Input.mousePosition.y,Camera.main.nearClipPlane);Vector3 worldMousePosFar = Camera.main.ScreenToWorldPoint(screenMousePosFar);Vector3 worldMousePosNear = Camera.main.ScreenToWorldPoint(screenMousePosNear);RaycastHit hit;Physics.Raycast(worldMousePosNear, worldMousePosFar - worldMousePosNear, out hit);return hit;}
}
这篇关于unity3d拼图__附带资源的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!