物体拖拽与旋转
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
public class WXC_ObjectDrag : MonoBehaviour
{
public float xMove = 5;
public float yMove = 5;
public float wheelSpeed =0.005f;
public float minBeiShu;
public float maxBeiShu;
public float move_Speed;
Vector3 sca;
Vector3 pos;
Quaternion rot;
public static bool isTrue = true;
public static bool isOn = true;
Vector3 vecDown;
Vector3 vecScroll;
void Start()
{
}
void Update()
{
if (this.gameObject.activeSelf == true)
{
if (Input.GetMouseButtonDown(0))
{
vecDown = Input.mousePosition;
}
if (Input.GetMouseButtonDown(2))
{
vecScroll = Input.mousePosition;
}
if (Input.GetMouseButton(0))
{
if (isTrue == true && isOn == true)
{
Vector3 deltaPos = Input.mousePosition - vecDown;
Vector3 vec = deltaPos.normalized;
this.transform.RotateAround(this.transform.position, -Vector3.up, vec.x * xMove);
this.transform.RotateAround(this.transform.position, Vector3.right, vec.y * yMove);
}
}
if (isTrue == true && isOn == true)
{
transform.localScale += new Vector3(Input.mouseScrollDelta.y * wheelSpeed, Input.mouseScrollDelta.y * wheelSpeed, Input.mouseScrollDelta.y * wheelSpeed);
}
if (Input.GetMouseButton(2))
{
Vector3 scPos = Input.mousePosition - vecScroll;
Vector3 vec1 = scPos.normalized;
transform.Translate(vec1.x * move_Speed, vec1.y * move_Speed, 0, Space.World);
}
if (transform.localScale.x < sca.x * minBeiShu)
{
transform.localScale = sca * minBeiShu;
}
else if (transform.localScale.x > sca.x * maxBeiShu)
{
transform.localScale = sca * maxBeiShu;
}
}
}
void OnEnable()
{
isTrue = true;
sca = transform.localScale;
pos = transform.localPosition;
rot = transform.localRotation;
}
void OnDisable()
{
transform.localPosition = pos;
transform.localScale = sca;
transform.localRotation = rot;
}
}