将玩家在每个关卡中的生命整合在一起

本文关键字:生命 在一起 玩家 | 更新日期: 2023-09-27 17:49:45

在我的游戏中有各种各样的关卡我在不同的场景中制作了这些关卡每个场景都有一个处于起点的玩家,当它穿过退出点时,下一个关卡(下一个场景)将被加载,但生命值和生命值将被重置为游戏的起点,而不是玩家退出关卡时的当前值

退出级别脚本:-

using UnityEngine;
using System.Collections;
public class ExitLevel : MonoBehaviour {
    public string scene;
    private Player player;

    // Use this for initialization
    void Start () {
        player = GameObject.Find ("Player").GetComponent<Player> ();
    }

    // Update is called once per frame
    void Update () {
    }
    void OnTriggerEnter2D(Collider2D target){
                        if (target.gameObject.tag == "Player") {
            if (player.ArtifactCount == 1) {
                player.ArtifactCount = 0;
                                Destroy (target.gameObject);
                                Application.LoadLevel (scene);
                        }
                }
    }
}

LifeMeter脚本:-

using UnityEngine;
using System.Collections;
public class LifeMeter : MonoBehaviour {
    public float Life = 100;
    public float maxLife = 100;
    public float LifeBurnRate = 1f;
    public Texture2D bgTexture;
    public Texture2D LifeBarTexture;
    public Texture2D HeartTexture;
    public int iconWidth = 32;
    public Vector2 LifeOffset = new Vector2(255, 10);
    public int HeartCount = 3;
    public Vector2 HeartOffset = new Vector2(455, 6.5f);
    public int HeartDistance = 0;
    public int HeartDistanceAddup = 40;
    public int HeartDisplayWidth = 40;
    public int HeartDisplayHeight = 40;

    private Player player;
    private ExitLevel exitLevel;

    // Use this for initialization
    void Start () {
        player = GameObject.FindObjectOfType<Player> ();
    }
    public void OnGUI(){
        var percent = Mathf.Clamp01 (Life / maxLife);
        if (!player) 
                percent = 0;
        if(Life == 0 && HeartCount !=0 ){
            HeartCount = HeartCount - 1;
            Life = 100;
        }
        HeartDistance = 0;
        DrawMeter (LifeOffset.x, LifeOffset.y, LifeBarTexture, bgTexture, percent);
        for (int i = 0; HeartCount > i; i++) {
            HeartDistance = HeartDistance + HeartDistanceAddup;
            GUI.DrawTexture (new Rect (HeartOffset.x + HeartDistance, HeartOffset.y, HeartDisplayWidth, HeartDisplayHeight), HeartTexture);
                }
    }
    void DrawMeter(float x, float y, Texture2D texture, Texture2D background, float percent){
        var bgW = background.width;
        var bgH = background.height;
        GUI.DrawTexture (new Rect (x, y, bgW, bgH), background);
        var nW = ((bgW - iconWidth) * percent) + iconWidth;
        GUI.BeginGroup (new Rect (x, y, nW, bgH));
        GUI.DrawTexture (new Rect (0, 0, bgW, bgH), texture);
        GUI.EndGroup ();

    }
    // Update is called once per frame
     void Update () {
        }

    public void life(){
    Life = Life - LifeBurnRate;
        if (HeartCount == 0) {
            {   Explode script = player.GetComponent<Explode> ();
        script.OnExplode ();
            }
        }
    }
}

将玩家在每个关卡中的生命整合在一起

你可以使用PlayerPrefs(在游戏会话之间存储和访问玩家偏好)来保存玩家的当前状态。参考此链接http://docs.unity3d.com/ScriptReference/PlayerPrefs.html

在上面的情况下有两种方法:

  • 你可以添加DontDestroyOnload(this);到你的播放器对象。这在新关卡加载时不会破坏游戏对象。和在每一个关卡的开始,你都可以刷出你的关卡起点。这让玩家的数据在整个游戏中保持一致。

  • 你可以使用一个静态类来获取玩家的生命值等所需的数据。在静态职业中保存玩家的生命值退出关卡并将数值应用于下一关卡的玩家