找回密码
 立即注册
楼主: 仙劍飛蓬

[脚本] 虚拟摇杠

[复制链接]
发表于 2013-9-13 10:47 | 显示全部楼层 |阅读模式
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表示摇杠上移

评分

参与人数 2鲜花 +2 收起 理由
wu6242420 + 1 很给力!
qwer4650987 + 1 很给力!

查看全部评分

发表于 2013-12-20 08:51 | 显示全部楼层

不错 不错 不错{:soso__3922851084632044791_6:}
发表于 2014-5-15 10:11 | 显示全部楼层
好 好 顶顶
发表于 2014-5-15 11:18 | 显示全部楼层

感谢楼主的无私分享!{:soso__11402694654016840197_7:}
发表于 2014-8-11 14:26 | 显示全部楼层

不错 不错 不错{:soso__3922851084632044791_6:}
发表于 2015-3-25 09:54 | 显示全部楼层
直接贴出源代码的朋友最给力!
发表于 2017-3-31 18:22 | 显示全部楼层
很不错
发表于 2017-3-31 17:39 | 显示全部楼层
楼主是超人
发表于 2017-3-31 17:49 | 显示全部楼层
真心顶
发表于 2017-3-31 17:42 | 显示全部楼层
难得一见的好帖
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-11-22 18:31 , Processed in 0.067573 second(s), 22 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表