Unity3D 用按钮控制人物行走


Hi,推荐文件给你 "Unity3D用按钮控制人物行走.zip" http://vdisk.weibo.com/s/JaBAq



//动画数组
private var animUp:Object[];
private var animDown:Object[];
private var animLeft:Object[];
private var animRight:Object[];
//地图
private var map:Texture2D;
//当前人物的动画
private var tex:Object[];
//人物的x坐标
private var x: int;
//人物的y坐标
private var y: int;
//帧序列
private var nowFram :int;
//动画帧的总数量
private var mframeCount:int;
//每秒更新多少帧
private var fps:float = 3;

private var time:float = 0;


function Start () 
{
	//获取图片资源
	animUp = Resources.LoadAll("up");
	animDown = Resources.LoadAll("down");
	animLeft = Resources.LoadAll("left");
	animRight = Resources.LoadAll("right");
	//地图的图片
	map = Resources.Load("map/map");
	//设置默认动画
	tex = animUp;
}


function OnGUI()
{
	//绘制贴图
	GUI.DrawTexture(Rect(0,0,Screen.width,Screen.height),map,ScaleMode.StretchToFill,true,0);
	//绘制帧动画
	DrawAnimation(tex,Rect(x,y,32,48));
	
	//点击按钮移动人物
	if(GUILayout.RepeatButton("Up"))
	{
		y -= 2;
		tex = animUp;
	}
	
	if(GUILayout.RepeatButton("down"))
	{
		y += 2;
		tex = animDown;
	}
	
	if(GUILayout.RepeatButton("Left"))
	{
		x -= 2;
		tex = animLeft;
	}
	
	if(GUILayout.RepeatButton("right"))
	{
		x += 2;
		tex = animRight;
	}
}

function DrawAnimation(tex:Object[],rect:Rect)
{
	//绘制当前帧
	GUI.DrawTexture(rect,tex[nowFram],ScaleMode.StretchToFill,true,0);
	//计算帧时间
	time += Time.deltaTime;
	//超过限制帧则切换图片
	if(time >= 1.0 / fps)
	{
		nowFram++;
		
		time = 0;
		
		if(nowFram >= tex.Length)
		{
			nowFram = 0;
		}
	}
}


你可能感兴趣的:(Unity3D)