天津23维预案
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

109 lines
3.6 KiB

using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections;
using System;
using UnityEngine.UI;
using AX.MessageSystem;
//给空间添加监听事件要实现的一些接口
public class DragBase : MonoBehaviour, IPointerDownHandler, IDragHandler, IPointerUpHandler,
IEndDragHandler
{
private RectTransform canvas; //得到canvas的ugui坐标
private RectTransform imgRect; //得到图片的ugui坐标
Vector2 offset = new Vector3(); //用来得到鼠标和图片的差值
Vector3 imgReduceScale = new Vector3(0.8f, 0.8f, 1); //设置图片缩放
Vector3 imgNormalScale = new Vector3(1, 1, 1); //正常大小
private GameObject TemporaryFather;
private GameObject Fahter;
Vector2 PosVector;
// Use this for initialization
void Start()
{
imgRect = GetComponent<RectTransform>();
canvas = GameObject.Find("Canvas").GetComponent<RectTransform>();
}
//当鼠标按下时调用 接口对应 IPointerDownHandler
public void OnPointerDown(PointerEventData eventData)
{
pos = transform.GetComponent<RectTransform>().localPosition;
Vector2 mouse;
RectTransformUtility.ScreenPointToLocalPointInRectangle(this.transform.parent as RectTransform, Input.mousePosition, canvas.GetComponent<Canvas>().worldCamera, out mouse);
vec2 = mouse;
}
Vector2 pos, vec2;
//当鼠标拖动时调用 对应接口 IDragHandler
public void OnDrag(PointerEventData eventData)
{
Vector2 mouseDrag = eventData.position; //当鼠标拖动时的屏幕坐标
Vector2 uguiPos = new Vector2(); //用来接收转换后的拖动坐标
////和上面类似
bool isRect = RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas, mouseDrag, eventData.enterEventCamera, out uguiPos);
Vector2 mouse;
RectTransformUtility.ScreenPointToLocalPointInRectangle(this.transform.parent as RectTransform, Input.mousePosition,
canvas.GetComponent<Canvas>().worldCamera, out mouse);
Vector2 off = mouse - vec2;
vec2 = mouse;
pos = pos + off;
transform.GetComponent<RectTransform>().localPosition = pos;
}
//当鼠标抬起时调用 对应接口 IPointerUpHandler
public void OnPointerUp(PointerEventData eventData)
{
offset = Vector2.zero;
//if (!this.name.Contains("TwoArrows"))
//{
// this.gameObject.transform.SetParent(Fahter.transform);
//}
}
//当鼠标结束拖动时调用 对应接口 IEndDragHandler
public void OnEndDrag(PointerEventData eventData)
{
offset = Vector2.zero;
//if (!this.name.Contains("TwoArrows"))
//{
// this.gameObject.transform.SetParent(Fahter.transform);
//}
}
void Update()
{
if (Input.GetAxis("Mouse ScrollWheel") > 0.1)
{
if (imgRect.localScale.x <= 5)
{
imgRect.localScale += new Vector3(0.1f, 0.1f, 0.1f);
}
}
else if (Input.GetAxis("Mouse ScrollWheel") < -0.1)
{
if (imgRect.localScale.x >= .5)
{
imgRect.localScale -= new Vector3(0.1f, 0.1f, 0.1f);
}
}
if (Input.GetKeyDown(KeyCode.Escape))
{
GameObject.Find("Main Camera").GetComponent<MouseFollowRotation>().enabled = true;
GameObject.Find("Main Camera").GetComponent<MiddleButtonDrag>().enabled = true;
this.gameObject.SetActive(false);
}
}
}