纳金网
标题:
拼图游戏部分代码
[打印本页]
作者:
狂风大尉
时间:
2014-6-25 00:53
标题:
拼图游戏部分代码
var Tu : Texture2D;
private var first = Vector2.zero;
private var second = Vector2.zero;
private var tempx : float = 0;
private var tempy : float = 0;
//unity3d脚本http://www.unitymanual.com
function OnGUI () {
GUI.DrawTexture (Rect (tempx, tempy, 50, 50),Tu);
if(Event.current.type == EventType.MouseDown){
first = Event.current.mousePosition ;
}
if(Event.current.type == EventType.MouseDrag){
second = Event.current.mousePosition ;
tempx += (second.x-first.x);
tempy += (second.y-first.y);
first = second;
}
}
function Update () {
}
复制代码
下面贴一段我修改后的代码,这段代码可以实现鼠标必须点击在图片上才能拖动图片,且可以拖动多张图片。拖动某一张图片时,其他图片不会收影响。unity3d脚本http://www.unitymanual.com利用这个思想,你可以实现其他的很多功能,不仅仅是拖动图片,例如可以实现判断图片是否拖入某一正确区域等等(拼图游戏可以用上述方法实现)。
var Tu : Texture2D;
var Tu1 : Texture2D;
private var first = Vector2.zero;
private var second = Vector2.zero;
private var tempx : float = 0;
private var temp1x : float = 500;
private var tempy : float = 0;
private var temp1y : float = 0;
var IsTu : int = 0;
var IsTu1 : int = 0;
function Update () {
}
function OnGUI () {
GUI.DrawTexture (Rect (tempx, tempy, 50, 50),Tu);
GUI.DrawTexture (Rect (temp1x, temp1y, 50, 50),Tu1);
if(Event.current.type == EventType.MouseDown){
first = Event.current.mousePosition ;
}
if(first.x > tempx && first.x < (tempx+50) && first.y > tempy && first.y < (tempy+50) ){
IsTu = 1;
}
if(first.x > temp1x && first.x < (temp1x+50) && first.y > temp1y && first.y < (temp1y+50) ){
IsTu1 = 1;
}
if(Event.current.type == EventType.MouseDrag && IsTu == 1){
second = Event.current.mousePosition ;
tempx += (second.x-first.x);
tempy += (second.y-first.y);
first = second;
IsTu = 0;
}
if(Event.current.type == EventType.MouseDrag && IsTu1 == 1){
second = Event.current.mousePosition ;
temp1x += (second.x-first.x);
temp1y += (second.y-first.y);
first = second;
IsTu1 = 0;
}
}
复制代码
作者:
hyui
时间:
2014-6-25 02:11
Thanks for sharing your experience!
作者:
wucnj
时间:
2014-6-25 09:12
感谢分享!!!
作者:
hariboot
时间:
2014-6-25 09:18
MARK THIS CONTENT
作者:
yanbo8806
时间:
2014-7-6 21:01
MARK THIS CONTENT
欢迎光临 纳金网 (http://wwww.narkii.com/club/)
Powered by Discuz! X2.5