U3d学习-使用Unity3D开发2D游戏(上)

时间:2023-02-07 17:15:10



  1.创建一个项目
  2.将Camera设置为正交投影
  游戏的输出画面是通过摄像机所观察的场景来实现的,将场景呈现到2D的计算机屏幕上具有两种不同的投影方式:透视投影和正交投影,默认状态下是透视投影.
  透视投影
  
U3d学习-使用Unity3D开发2D游戏(上)
  正交投影
  
U3d学习-使用Unity3D开发2D游戏(上)
  3.添加相关对象模型
  移动摄像机物体,灯光到如下效果
  
U3d学习-使用Unity3D开发2D游戏(上)
  添加游戏物体移动超出边框的控制
  using UnityEngine;
  using System.Collections;
  public class Player:MonoBehaviour {
  public float playerSpeed;
  // Use this for initialization
  void Start () {
  }
  // Update is called once per frame
  void Update () {
  float amtToMove = Input.GetAxis("Horizontal")*Time.deltaTime*playerSpeed;
  gameObject.transform.Translate(Vector3.right*amtToMove);
  if(transform.position.x-7.5){
  //如果方块移动超出游戏窗体右边,方框将从左边窗口进入
  transform.position = new Vector3(5.25f,transform.position.y,transform.position.z);
  }
  if(transform.position.x5.25){
  //如果方块移动超出游戏窗体右边,方框将从左边窗口进入
  transform.position = new Vector3(-7.5f,transform.position.y,transform.position.z);
  }
  }
  }
  4.创建炮弹[create-Capsule]
  
U3d学习-使用Unity3D开发2D游戏(上)

  根据需要调整炮弹的大小


  选择炮弹模型,单机Component菜单,选择Physics-RigidBody,将炮弹设置为刚体,以便后面实现碰撞检测.
  为炮弹添加移动操作脚本
  using UnityEngine;
  using System.Collections;
  public class bulletTile : MonoBehaviour {
  public float bulletSpeed ;
  private Transform mytransform;
  // Use this for initialization
  void Start () {
  mytransform = gameObject.transform;
  }
  // Update is called once per frame
  void Update () {
  //定义炮弹移动速度
  float amtToMove = bulletSpeed * Time.deltaTime;
  //让炮弹垂直向上移动
  mytransform.Translate(Vector3.up * amtToMove);
  //如果炮弹移动超出游戏场景则销毁炮弹
  if(mytransform.position.y5.15){
  Destroy(this.gameObject);    //销毁当前对象
  }
  }
  }
  对象重用
  在Project中创建一个Prefab对象,然后将要重用的对象模型拖动到该Prefab对象上,即可实现模型对象的重用.
  发射子弹的实现
  在Player脚本中创建Prefab可重用对象实例
  using UnityEngine;
  using System.Collections;
  public class Player:MonoBehaviour {
  public float playerSpeed;
  public GameObject bulletPrefab;
  // Use this for initialization
  void Start () {
  }
  // Update is called once per frame
  void Update () {
  float amtToMove = Input.GetAxis("Horizontal")*Time.deltaTime*playerSpeed;
  gameObject.transform.Translate(Vector3.right*amtToMove);
  if(transform.position.x-7.5){
  //如果方块移动超出游戏窗体右边,方框将从左边窗口进入
  transform.position = new Vector3(5.25f,transform.position.y,transform.position.z);
  }
  if(transform.position.x5.25){
  //如果方块移动超出游戏窗体右边,方框将从左边窗口进入
  transform.position = new Vector3(-7.5f,transform.position.y,transform.position.z);
  }
  //获取发射器位置 发射器的正上方
  Vector3 position = new Vector3(transform.position.x,transform.position.y+transform.localScale.y/2.0f,transform.position.z);
  //按下空格键发射子弹
  if(Input.GetKeyDown("space")){
  //实例化一个炮弹对象
  Instantiate(bulletPrefab,position,Quaternion.identity);
  }
  }
  }
  
U3d学习-使用Unity3D开发2D游戏(上)
  拖动表示将属性实例化,因为我们脚本中定义的是public属性,在这里我们可以手动为public属性赋值,此时运行程序,用左右键控制发射器的移动,用space发射子弹.
  
U3d学习-使用Unity3D开发2D游戏(上)
  应用实例:
  游戏中声音的添加与控制
  支持的声音文件:*.aiff,*.wav,*.mp3,*.ogg
  .AIFF
  转换为无压缩音频导入,最适合短音效果。可以在编辑器中按需求压缩。
  .WAV
  转换为无压缩音频导入,最适合短音效果。可以在编辑器中按需求压缩
  .MP3
  转换成Ogg格式导入,最适合较长的音乐曲目。
  .OGG
  压缩音频格式(与iPhone设备和某些Android设备不兼容),最适合较长的音乐曲目。
  添加声音控制按钮,
  void OnGUI(){
  GUI.Button (new Rect (10,10,100,35), "播放音乐");
  GUI.Button (new Rect (10,60,100,35), "暂停播放");
  GUI.Button (new Rect (10,110,100,35), "停止音乐");
  }
  
U3d学习-使用Unity3D开发2D游戏(上)
  为按钮添加事件
  void OnGUI(){
  if(GUI.Button (new Rect (10,10,100,35), "播放音乐")){
  gameObject.audio.Play();
  }
  if(GUI.Button (new Rect (10,60,100,35), "暂停播放")){
  gameObject.audio.Pause().
  }
  if(GUI.Button (new Rect (10,110,100,35), "停止音乐")){
  gameObject.audio.Stop();
  }
  }
  5.为发射炮弹添加声音
  选中bulletPrefab,单机窗体中的Component-Audio-AudionSource
  
U3d学习-使用Unity3D开发2D游戏(上)