首页 / UNITY / unity 之2D游戏简单操作
unity 之2D游戏简单操作
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了unity 之2D游戏简单操作,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3893字,纯文字阅读大概需要6分钟。
内容图文
![unity 之2D游戏简单操作](/upload/InfoBanner/zyjiaocheng/1216/f9fa73967bb941a4b34f529f9cb5c1fb.jpg)
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帧; 10privateint currentFrame; 11// Use this for initialization 12void Start () 13 { 14 GameObject plane = GameObject.Find("Plane"); 15//得到面默认宽度 16float size_x = plane.GetComponent<MeshFilter>().mesh.bounds.size.x; 17//得到面宽度的缩放比例 18float scal_x = plane.transform.localScale.x; 19//得到面默认高度 20float size_z = plane.GetComponent<MeshFilter>().mesh.bounds.size.z; 21//得到面高度缩放比例 22float scal_z = plane.transform.localScale.z; 23 24//原始宽度乘以缩放比例计算出真实宽度 25float mapWidth = size_x * scal_x; 26float 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 37void Update () 38 { 39if (Input.GetKey (KeyCode.A)) { 40this.transform.Translate (-0.1f, 0f, 0f, Space.World); 41 } 42if (Input.GetKey (KeyCode.D)) { 43this.transform.Translate (0.1f, 0f, 0f, Space.World); 44 } 45if (Input.GetKey (KeyCode.W)) { 46this.transform.Translate (0f, 0.1f, 0f, Space.World); 47 } 48 49if (Input.GetKey (KeyCode.S)) { 50this.transform.Translate (0f, -0.1f, 0f, Space.World); 51 } 52//this is horse‘s: 53if (Input.GetKey (KeyCode.J)) { 54 horses.transform.Translate (-0.1f, 0f, 0f, Space.World); 55 DrawImages(images); 56 } 57else 58 { 59 horses.renderer.material.mainTexture=images[currentFrame]as Texture; 60 } 61if (Input.GetKey (KeyCode.L)) { 62 horses.transform.Translate (0.1f, 0f, 0f, Space.World); 63 DrawImages(images); 64 } 65else 66 { 67 horses.renderer.material.mainTexture=images[currentFrame]as Texture; 68 } 69if (Input.GetKey (KeyCode.I)) { 70 horses.transform.Translate (0f, 0.1f, 0f, Space.World); 71 DrawImages(images); 72 } 73else 74 { 75 horses.renderer.material.mainTexture=images[currentFrame]as Texture; 76 } 77 78if (Input.GetKey (KeyCode.K)) { 79 horses.transform.Translate (0f, -0.1f, 0f, Space.World); 80 DrawImages(images); 81 } 82else 83 { 84 horses.renderer.material.mainTexture=images[currentFrame]as Texture; 85 } 86 87 } 88 89publicvoid DrawImages(Object[]useImages) 90 { 91 timer+=Time.deltaTime; 92//序列图切换函数 93if (timer>=1.0/fps) { 94 currentFrame++; 95 timer=0; 96//溢出归零; 97if (currentFrame>=useImages.Length) { 98 currentFrame=0; 99 } 100 } 101 horses.renderer.material.mainTexture=useImages[currentFrame]as Texture; 102 } 103104105106 }
该脚本我把它挂在了 游戏对象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 }
原文:http://www.cnblogs.com/devilWang/p/4670862.html
内容总结
以上是互联网集市为您收集整理的unity 之2D游戏简单操作全部内容,希望文章能够帮你解决unity 之2D游戏简单操作所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。