|
using UnityEngine;
using System.Collections;
public class mouse : MonoBehaviour {
public Texture2D tex;
public GUISkin skin;
float x = 0;
float y = 0;
GameObject yaogang;
bool collideryaogang = false; //****鼠标是否与物体选中***//
int stateofyaogang = 0;
static float Area_x =0;
static float Area_y =0;
Vector3 mousepos; //记录鼠标的坐标
public int yaogang_x =0;
public int yaogang_y =0;
// Use this for initialization
void Awake()
{
yaogang = GameObject.Find("Joy");
Area_x = yaogang.transform.position.x*Screen.width;
Area_y = yaogang.transform.position.y*Screen.height;
PlayerPrefs.SetInt("yaogang_x",yaogang_x);
PlayerPrefs.SetInt("yaogang_y",yaogang_y);
}
void Start ()
{
}
// Update is called once per frame
void Update ()
{
// //是否点击鼠标左键
// if(Input.GetMouseButtonDown(0))
// {
// //创建鼠标点击的射线
// Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// RaycastHit hit;
// //鼠标是否选中游戏对象
// if (Physics.Raycast(ray, out hit))
// {
// //得鼠标选中到游戏对象
// GameObject obj = hit.collider.gameObject;
// Debug.Log(obj);
// }
// }
position();
}
void position()
{
if(Input.mousePosition.x>yaogang.transform.position.x*Screen.width&&Input.mousePosition.x<yaogang.transform.position.x*Screen.width+58
&&Input.mousePosition.y>yaogang.transform.position.y*Screen.height&&Input.mousePosition.y<yaogang.transform.position.y*Screen.height+58)
{
collideryaogang = true;
}else
collideryaogang = false;
if(collideryaogang)
{
if(Input.GetMouseButtonDown(0))
{
mousepos = Input.mousePosition;
stateofyaogang = 1;
}
// if(Input.GetMouseButtonUp(0))
// {
// stateofyaogang = 2;
// Area_x = yaogang.transform.position.x*Screen.width;
// Area_y = yaogang.transform.position.y*Screen.height;
// }
}
if(Input.GetMouseButtonUp(0))
{
stateofyaogang = 2;
// Area_x = yaogang.transform.position.x*Screen.width;
// Area_y = yaogang.transform.position.y*Screen.height;
}
if(stateofyaogang == 1)
{
if(Input.mousePosition.x>Area_x-20&&Input.mousePosition.x<Area_x+70&&
Input.mousePosition.y>Area_y-20&&Input.mousePosition.y<Area_y+70)
{
yaogang.transform.position += new Vector3((Input.mousePosition.x-mousepos.x)/Screen.width,(Input.mousePosition.y-mousepos.y)/Screen.height,0);
mousepos = Input.mousePosition;
}
float distance_x = yaogang.transform.position.x*Screen.width-(Area_x);
float distance_y = yaogang.transform.position.y*Screen.height-(Area_y);
if(distance_x*distance_x>=distance_y*distance_y)
{
if(distance_x<-10)
{
yaogang_x = -1;
}else
if(distance_x>10)
{
yaogang_x = 1;
}else
yaogang_x =0;
yaogang_y = 0;
PlayerPrefs.SetInt("yaogang_x",yaogang_x);
PlayerPrefs.SetInt("yaogang_y",yaogang_y);
}
if(distance_y*distance_y>=distance_x*distance_x)
{
if(distance_y<-10)
{
yaogang_y = -1;
}else
if(distance_y>10)
{
yaogang_y = 1;
}else
yaogang_y = 0;
yaogang_x = 0;
PlayerPrefs.SetInt("yaogang_x",yaogang_x);
PlayerPrefs.SetInt("yaogang_y",yaogang_y);
}
}
}
void LaterUpdate()
{
}
void OnGUI()
{
// GUI.skin = skin;
// if(GUI.Button(new Rect(100,100,100,100),tex))
// {
//
// }
// GUILayout.Label("mousex:"+Input.mousePosition.x+" mousey:"+Input.mousePosition.y+" mousez:"+Input.mousePosition.z);
// GUILayout.Label("Area_x:"+(Area_x+29)+" Area_y:"+(Area_y+29));
// GUILayout.Label("yaogang_x:"+yaogang_x);
// GUILayout.Label("yaogang_y:"+yaogang_y);
// GUILayout.Label("distance_x:"+(yaogang.transform.position.x*Screen.width-(Area_x)));
// GUILayout.Label("distance_y:"+(yaogang.transform.position.y*Screen.height-(Area_y)));
}
}
//说明下:
yaogang_x :-1表示摇杠左移,1表示摇杠右移
yaogang_y :-1表示摇杠下移,1表示摇杠上移 |
评分
-
查看全部评分
|