В моей игре есть разные уровни Я сделал этот уровень в отдельных сценах Каждая сцена имеет игрока в начальной точке и когда она пересекает точку выхода на следующем уровне (следующая сцена) загружен, но здоровье и жизнь сбрасываются в качестве отправной точки в игре, и не останется того, что игрок в настоящее время имеет, когда он вышел на уровень уровеньИнтеграция жизни игрока на каждом уровне в единстве
Выход сценария: -
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();
}
}
}
}
Поскольку вы просматриваете детали игрока в методе 'Start' для обоих классов, показанных здесь, сохраните детали жизни в' Player'. Таким образом, он будет сохраняться в разных сценах. –