unity3d 2D游戏开发

这篇博客介绍了在Unity3D中开发2D游戏的基本操作,包括音频处理、面板切换、模拟屏幕控制以及人物运动的实现。通过具体的代码示例展示了如何处理角色碰撞、屏幕点击事件以及角色在屏幕上的移动,为2D游戏开发提供了一个基础框架。
摘要由CSDN通过智能技术生成

//音频

using UnityEngine;

using System.Collections;

public class GameController:MonoBehaviour
{
    private AudioClip baoshi_sound;
    private AudioClip jump_sound;
    private AudioClip dead_sound;
    private AudioSource soundSource;
    private static GameController instance = null;
    public AudioSource soundMgr;

    public static GameController Instance
    {
        get
        {
            return instance;
        }
    }
    void Awake()
    {
        instance = this;
        soundSource = gameObject.GetComponent<AudioSource>();
        baoshi_sound = Resources.Load("Sounds/baoshi_eat") as AudioClip;
        dead_sound = Resources.Load("Sounds/dead") as AudioClip;
        jump_sound = Resources.Load("Sounds/jump") as AudioClip;
    }
    public void Playbaoshi_sound()
    {
        soundSource.clip = baoshi_sound;
        soundSource.Play();

    }
    public void Playdead_sound()
    {
        soundSource.clip = dead_sound;
        soundSource.Play();
    }
    public void Playjump_sound()
    {
        soundSource.clip = jump_sound;
        soundSource.Play();
    }

}

//panel切换

using UnityEngine;
using System.Collections;

public class Panel_start : MonoBehaviour
{
    public GameObject panel_start;
    public GameObject panel_main;
    void Start ()
    {

    }

    void Update ()
    {

    }
    public void OnClick()
    {
        panel_start.SetActive(false);
        
        panel_main.SetActive(true);
        Application.LoadLevel(0);
    }
}

//模拟屏幕控制

using UnityEngine;
using System.Collections;

public class HUDLayer : MonoBehaviour
{
    public Vector2 zhongli = new Vector2(0, -20);
    public Vector2 click = new Vector2(0, 12);
    public Vector2 tap = new Vector2(0, 20);
    public Rigidbody2D body;
    public bool start = false;
    public float speed = 0.09f;

    bool StartCountTime = false;
    float totalTime;
    int speed_y = 1;

    //public Animation Jump;
    public GameObject Role_Jump;

    public GameController jumpSource;
    public void OnPointerDown()
    {
        StartCountTime = true;

    }
    public void OnPointerUp()
    {
        StartCountTime = false;
        if (totalTime < 0.15&&speed_y==0)
        {
            body.velocity = click;
            //animation["Jump"].speed = 0;
            body.transform.Find("role_run").GetComponent<Animator>().speed = 0;
            jumpSource.Playjump_sound();
        }
        totalTime = 0;
    }
    void Start ()
    {
        jumpSource = GameObject.Find("SourceManger").GetComponent<GameController>();
    }

    void Update ()
    {
        
        float a=speed + PlayerPrefs.GetInt("floors_num")*0.0005f;
        if (PlayerPrefs.GetInt("stop") == 1)
        {
            a = 0;
            click = new Vector2(0, 0);
            tap = new Vector2(0, 0);
        }
        if (body.velocity.y < 0.2 && body.velocity.y > -0.2)
        {
            speed_y = 0;
        }
        else
        {
            speed_y = 1;
        }
        body.AddForce(zhongli);
        body.transform.Translate(a, 0, 0);
        if (StartCountTime)
        {
            totalTime += Time.deltaTime;
            if (0.2 > totalTime && totalTime > 0.15 &&speed_y=&

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值