您的位置:首页 > 移动开发 > Unity3D

unity 之2D游戏简单操作

2015-07-23 16:37 573 查看
unity 做2D项目也很方便。 首先要调整camera的模式,camera 的检视面板参数如下:



perspective 模式就是平时用的 模式。摄像机到游戏物体是有角度的张开, 而 orthographic 模式则没有,两者的区别从侧面看就一目了然了:

Perspective类型





Orthographic





这两张图是截取雨松前辈的图,这是侧视图,相信大家一眼就看出了区别,不再过多解释;

下面是移动篇 ,按下WASD 控制 摄像机的上下左右移动。 按下 IJKL 控制 小马驹的移动(按下时小马驹序列图播放,松开时停留在最后一帧)。

图片是JPG格式的,由于切图的时候的失误没有保存为 PNG 格式,就用了JPG 格式。所以 shader的选择上选什么shader 也不重要了我就用了Transparent/Diffuse 这个自带的shader;

买代码我说个J8:

using UnityEngine;
using System.Collections;

public class MoveTest : MonoBehaviour
{
private GameObject horses;
private Object[] images;
private float timer;
public float fps=10f;//一秒10帧;
private int currentFrame;
// Use this for initialization
void Start ()
{
GameObject plane = GameObject.Find("Plane");
//得到面默认宽度
float size_x = plane.GetComponent<MeshFilter>().mesh.bounds.size.x;
//得到面宽度的缩放比例
float scal_x = plane.transform.localScale.x;
//得到面默认高度
float size_z = plane.GetComponent<MeshFilter>().mesh.bounds.size.z;
//得到面高度缩放比例
float scal_z = plane.transform.localScale.z;

//原始宽度乘以缩放比例计算出真实宽度
float mapWidth = size_x * scal_x;
float mapHeight = size_z * scal_z;

Debug.Log("得到面的位置:"+plane.transform.position);
Debug.Log("得到面的宽度:"+ mapWidth);
Debug.Log("得到面的高度:"+ mapHeight);

horses = GameObject.Find ("player");
images = Resources.LoadAll ("xulieImages")as Object[];
}

// Update is called once per frame
void Update ()
{
if (Input.GetKey (KeyCode.A)) {
this.transform.Translate (-0.1f, 0f, 0f, Space.World);
}
if (Input.GetKey (KeyCode.D)) {
this.transform.Translate (0.1f, 0f, 0f, Space.World);
}
if (Input.GetKey (KeyCode.W)) {
this.transform.Translate (0f, 0.1f, 0f, Space.World);
}

if (Input.GetKey (KeyCode.S)) {
this.transform.Translate (0f, -0.1f, 0f, Space.World);
}
//this is  horse's:
if (Input.GetKey (KeyCode.J)) {
horses.transform.Translate (-0.1f, 0f, 0f, Space.World);
DrawImages(images);
}
else
{
horses.renderer.material.mainTexture=images[currentFrame]as Texture;
}
if (Input.GetKey (KeyCode.L)) {
horses.transform.Translate (0.1f, 0f, 0f, Space.World);
DrawImages(images);
}
else
{
horses.renderer.material.mainTexture=images[currentFrame]as Texture;
}
if (Input.GetKey (KeyCode.I)) {
horses.transform.Translate (0f, 0.1f, 0f, Space.World);
DrawImages(images);
}
else
{
horses.renderer.material.mainTexture=images[currentFrame]as Texture;
}

if (Input.GetKey (KeyCode.K)) {
horses.transform.Translate (0f, -0.1f, 0f, Space.World);
DrawImages(images);
}
else
{
horses.renderer.material.mainTexture=images[currentFrame]as Texture;
}

}

public void DrawImages(Object[]useImages)
{
timer+=Time.deltaTime;
//序列图切换函数
if (timer>=1.0/fps) {
currentFrame++;
timer=0;
//溢出归零;
if (currentFrame>=useImages.Length) {
currentFrame=0;
}
}
horses.renderer.material.mainTexture=useImages[currentFrame]as Texture;
}

}


该脚本我把它挂在了 游戏对象Main Camera 上。 player 是一个plan 对象:



动态改变的是 play 这个面片的材质的主贴图;上面的脚本用到了 Resources.LoadAll 函数,下面解释一下 这个函数的使用方法 以及一个官方的案例:

Resources.LoadAll 加载全部

static function LoadAll (path : string, type : Type) : Object[]

加载Resources文件夹中的path文件夹或者文件中的所有资源。

如果path是一个文件夹,文件中的所有资源都将被返回。如果path为一个文件,只有这个资源将被返回。只有type类型的物体将被返回。Path相对于Resources文件夹。Resources文件夹可以在Assets文件夹中的任何位置。

//加载"Resources/Texture"文件夹中所有资源
//然后从列表中选择随机的一个
//注意:Random.Range这里返回 [低,高)范围,例如,高值不包括在内。


using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
void Start() {
GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube);
Object[] textures = Resources.LoadAll("Textures", typeof(Texture2D));
Texture2D texture = textures[Random.Range(0, textures.Length)];
go.renderer.material.mainTexture = texture;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: