《Unity 简单实现物体拖拽.doc》由会员分享,可在线阅读,更多相关《Unity 简单实现物体拖拽.doc(2页珍藏版)》请在金锄头文库上搜索。
1、using UnityEngine;using System.Collections;using System.Collections.Generic;using UnityEngine.UI;using System;using UnityEngine.EventSystems;public class durg : MonoBehaviour private int mode = 0; GameObject gameObj; public GameObject btnTurn; Vector3 pos; Ray ray; RaycastHit hitInfo; LayerMask mask
2、 = 1 8; public EventSystem es; public GraphicRaycaster gr; void Awake() btnTurn.transform.GetChild(1).GetComponent().onClick.AddListener(BtnTurnRight); btnTurn.transform.GetChild(2).GetComponent().onClick.AddListener(BtnTurnLeft); btnTurn.gameObject.SetActive(false); private void BtnTurnRight() game
3、Obj.transform.eulerAngles += new Vector3(0, 15, 0); private void BtnTurnLeft() gameObj.transform.eulerAngles += new Vector3(0, -15, 0); void Update() if (Input.GetMouseButtonDown(0) ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hitInfo) if (hitInfo.transform.g
4、ameObject.tag = Furniture) gameObj = hitInfo.transform.gameObject; mode = 1; if (Input.GetMouseButton(0) if (check() return; else ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hitInfo, Mathf.Infinity, mask.value) if (mode = 1) gameObj.transform.position = hitI
5、nfo.point; btnTurn.gameObject.SetActive(false); if (Input.GetMouseButtonUp(0) mode = 0; if (check() return; else ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hitInfo) if (hitInfo.transform.gameObject.tag = Furniture) btnTurn.gameObject.SetActive(true); Vector
6、3 screenPos = Camera.main.WorldToScreenPoint(gameObj.transform.position); btnTurn.GetComponent().position = screenPos; else btnTurn.gameObject.SetActive(false); bool check() PointerEventData eventData = new PointerEventData(es); eventData.pressPosition = Input.mousePosition; eventData.position = Input.mousePosition; List list = new List(); gr.Raycast(eventData, list); return list.Count 0;