unity 之2D游戏简单操作
2015-07-23 16:37
573 查看
unity 做2D项目也很方便。 首先要调整camera的模式,camera 的检视面板参数如下:
![](http://images0.cnblogs.com/blog2015/788574/201507/231613265219008.png)
perspective 模式就是平时用的 模式。摄像机到游戏物体是有角度的张开, 而 orthographic 模式则没有,两者的区别从侧面看就一目了然了:
Perspective类型
![](http://images0.cnblogs.com/blog2015/788574/201507/231617313032828.png)
Orthographic
![](http://images0.cnblogs.com/blog2015/788574/201507/231617554287423.png)
这两张图是截取雨松前辈的图,这是侧视图,相信大家一眼就看出了区别,不再过多解释;
下面是移动篇 ,按下WASD 控制 摄像机的上下左右移动。 按下 IJKL 控制 小马驹的移动(按下时小马驹序列图播放,松开时停留在最后一帧)。
图片是JPG格式的,由于切图的时候的失误没有保存为 PNG 格式,就用了JPG 格式。所以 shader的选择上选什么shader 也不重要了我就用了Transparent/Diffuse 这个自带的shader;
买代码我说个J8:
该脚本我把它挂在了 游戏对象Main Camera 上。 player 是一个plan 对象:
![](http://images0.cnblogs.com/blog2015/788574/201507/231630367716127.png)
动态改变的是 play 这个面片的材质的主贴图;上面的脚本用到了 Resources.LoadAll 函数,下面解释一下 这个函数的使用方法 以及一个官方的案例:
加载Resources文件夹中的path文件夹或者文件中的所有资源。
如果path是一个文件夹,文件中的所有资源都将被返回。如果path为一个文件,只有这个资源将被返回。只有type类型的物体将被返回。Path相对于Resources文件夹。Resources文件夹可以在Assets文件夹中的任何位置。
![](http://images0.cnblogs.com/blog2015/788574/201507/231613265219008.png)
perspective 模式就是平时用的 模式。摄像机到游戏物体是有角度的张开, 而 orthographic 模式则没有,两者的区别从侧面看就一目了然了:
Perspective类型
![](http://images0.cnblogs.com/blog2015/788574/201507/231617313032828.png)
Orthographic
![](http://images0.cnblogs.com/blog2015/788574/201507/231617554287423.png)
这两张图是截取雨松前辈的图,这是侧视图,相信大家一眼就看出了区别,不再过多解释;
下面是移动篇 ,按下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 对象:
![](http://images0.cnblogs.com/blog2015/788574/201507/231630367716127.png)
动态改变的是 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; } }
相关文章推荐
- Unity3D动画贴图、滚动纹理、滚动贴图脚本
- Unity3D-client +MinaServer仿微信聊天系统
- [Unity3D]Unity3D游戏开发3D选择场景中的对象,并显示轮廓效果强化版
- Unity多线程(Thread)和主线程(MainThread)交互使用类——Loom工具分享
- UNITY3D聖殿-自由的遊戲設計天堂
- 概要:Unity Cg 编程(关于顶点与片段着色器)
- [Unity3D]关于为什么保存Transform等引用效率会更高
- Unity5.0状态机
- 《Unity中的Path对应各平台中的Path》
- Unity笔记 XML读写 防篡改MD5签名
- Unity3D研究院之Android二次加密.so二次加密DLL(八十二)
- Unity笔记 UGUI RichText 文字表情图片
- Unity3D项目
- Unity3D逻辑热更新,第二代舒爽解决方案,L#使用简介
- [Unity3D]脚本中Start()和Awake()的区别
- 所有游戏开发者需要用心领悟的15个指标
- Unity3D之文件读写文件
- unity3d-carwaypoints插件优点
- 关于在Unity中启用外部的exe
- CellularAutomation(细胞自动机)