unity 之2D游戏简单操作

unity 做2D项目也很方便。  首先要调整camera的模式,camera 的检视面板参数如下:

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

 

Perspective类型

 

Orthographic

 

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

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

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

买代码我说个J8:

  1 using UnityEngine;
  2 using System.Collections;
  3 
  4 public class MoveTest : MonoBehaviour
  5 {
  6     private GameObject horses;
  7     private Object[] images;
  8     private float timer;
  9     public float fps=10f;//一秒10帧;
 10     private int currentFrame;
 11     // Use this for initialization
 12     void Start ()
 13     {
 14         GameObject plane = GameObject.Find("Plane");
 15         //得到面默认宽度
 16         float size_x = plane.GetComponent<MeshFilter>().mesh.bounds.size.x;
 17         //得到面宽度的缩放比例
 18         float scal_x = plane.transform.localScale.x;
 19         //得到面默认高度
 20         float size_z = plane.GetComponent<MeshFilter>().mesh.bounds.size.z;
 21         //得到面高度缩放比例
 22         float scal_z = plane.transform.localScale.z;
 23         
 24         //原始宽度乘以缩放比例计算出真实宽度
 25         float mapWidth = size_x * scal_x;
 26         float mapHeight = size_z * scal_z;
 27         
 28         Debug.Log("得到面的位置:"+plane.transform.position);
 29         Debug.Log("得到面的宽度:"+ mapWidth);
 30         Debug.Log("得到面的高度:"+ mapHeight);
 31 
 32         horses = GameObject.Find ("player");
 33         images = Resources.LoadAll ("xulieImages")as Object[];
 34     }
 35     
 36     // Update is called once per frame
 37     void Update ()
 38     {
 39         if (Input.GetKey (KeyCode.A)) {
 40             this.transform.Translate (-0.1f, 0f, 0f, Space.World);
 41         }
 42         if (Input.GetKey (KeyCode.D)) {
 43             this.transform.Translate (0.1f, 0f, 0f, Space.World);
 44         }
 45         if (Input.GetKey (KeyCode.W)) {
 46             this.transform.Translate (0f, 0.1f, 0f, Space.World);
 47         }
 48 
 49         if (Input.GetKey (KeyCode.S)) {
 50             this.transform.Translate (0f, -0.1f, 0f, Space.World);
 51         }
 52         //this is  horse's:
 53         if (Input.GetKey (KeyCode.J)) {
 54             horses.transform.Translate (-0.1f, 0f, 0f, Space.World);
 55             DrawImages(images);
 56         }
 57         else
 58         {
 59             horses.renderer.material.mainTexture=images[currentFrame]as Texture;
 60         }
 61         if (Input.GetKey (KeyCode.L)) {
 62             horses.transform.Translate (0.1f, 0f, 0f, Space.World);
 63             DrawImages(images);
 64         }
 65         else
 66         {
 67             horses.renderer.material.mainTexture=images[currentFrame]as Texture;
 68         }
 69         if (Input.GetKey (KeyCode.I)) {
 70             horses.transform.Translate (0f, 0.1f, 0f, Space.World);
 71             DrawImages(images);
 72         }
 73         else
 74         {
 75             horses.renderer.material.mainTexture=images[currentFrame]as Texture;
 76         }
 77         
 78         if (Input.GetKey (KeyCode.K)) {
 79             horses.transform.Translate (0f, -0.1f, 0f, Space.World);
 80             DrawImages(images);
 81         }
 82         else
 83         {
 84             horses.renderer.material.mainTexture=images[currentFrame]as Texture;
 85         }
 86 
 87     }
 88 
 89     public void DrawImages(Object[]useImages)
 90     {
 91         timer+=Time.deltaTime;
 92         //序列图切换函数
 93         if (timer>=1.0/fps) {
 94             currentFrame++;
 95             timer=0;
 96             //溢出归零;
 97             if (currentFrame>=useImages.Length) {
 98                 currentFrame=0;
 99             }
100         }
101         horses.renderer.material.mainTexture=useImages[currentFrame]as Texture;
102     }
103 
104 
105 
106 }

该脚本我把它挂在了 游戏对象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这里返回 [低,高)范围,例如,高值不包括在内。
 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 public class example : MonoBehaviour {
 5     void Start() {
 6         GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube);
 7         Object[] textures = Resources.LoadAll("Textures", typeof(Texture2D));
 8         Texture2D texture = textures[Random.Range(0, textures.Length)];
 9         go.renderer.material.mainTexture = texture;
10     }
11 }

 

转载于:https://www.cnblogs.com/devilWang/p/4670862.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值