将玩家的生活整合在一起

时间:2015-05-20 05:30:41

标签: c# android unity3d

在我的游戏中有各种级别 我在不同的场景中制作了这些关卡 每个场景都有一个起始点的玩家,当它越过出口点时,下一个等级(下一个场景)被加载,但是健康和生命被重置为游戏中的起始点,并且不会保留玩家当前退出时的状态。水平

退出级别脚本: -

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 ();
            }
        }

    }
}

2 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:2)

在上述情况下,有两种方法可以继续:

  • 您可以将DontDestroyOnload(this);添加到播放器对象中。这个 当新级别加载时,不会销毁该游戏对象。并且 每个级别的开始你都可以产生你的等级起点。这个 在整个游戏过程中,你的玩家的统计数据将保持一致。

  • 您可以使用静态类来让玩家健康和其他 要求的统计数据。在静态课程中保存玩家健康状况 退出关卡并将值应用于下一级玩家 下一级加载时的对象。