0

我的 C# 脚本有一些问题。所以,问题是加载场景后,obj 正在移动到场景的起始位置,而不是我设置的位置。有人可以帮我解决这个超级难题。请。

仍然无法正常工作,我尝试制作 IEnumerator 但这仍然无法正常工作,有什么新想法吗?:<

public void Load()
    {
        if (File.Exists (Application.dataPath + "/save.txt")) 
        {
            BinaryFormatter bf = new BinaryFormatter ();
            FileStream file = File.Open (Application.dataPath + /save.txt",FileMode.Open);
            PlayerData data = (PlayerData)bf.Deserialize (file);
            file.Close ();

            levelName = data.levelName;
            blueWaffle = data.blueWaffle;
            posX = data.posX;
            posY = data.posY;
            posZ = data.posZ;




            SceneManager.LoadScene (levelName);

            if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName(levelName)) 
            {
                //Vector3 loadedPosition = new Vector3 (posX, posY, posZ);


                Debug.Log ("I changed my position" + posX + posY + posZ);
                ScoreManager.Reset ();
                ScoreManager.AddPoints (blueWaffle);
                //player.GetComponent<Transform> ().position = loadedPosition;
                if (loaded (posX, posY, posZ)) {
                    Debug.Log ("I changed my position");
                } 
                else 
                {
                    Debug.Log ("I didint change my position");
                }
                Time.timeScale = 1f;
            }

        }
    }

    private Boolean loaded(float posX, float posY, float posZ)
    {
        Vector3 loadedPosition = new Vector3 (posX, posY, posZ);
        player.GetComponent<Transform> ().position = loadedPosition;
        return true;
    }
        -------------next version - still not work---------------
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class SaveAndLoad : MonoBehaviour {

    public static SaveAndLoad control;
    public int blueWaffle;
    public float posX;
    public float posY;
    public float posZ;
    public PlayerController player;
    public string levelName;
    public ScoreManager scores = new ScoreManager();

    void Start()
    {
        player = FindObjectOfType<PlayerController> ();
    }
    void Awake(){
        if (control == null) 
        {
            DontDestroyOnLoad (gameObject);
            control = this;
        } 
        else if(control != this)
        {
            Destroy (gameObject);
        }
    }

    public void Save()
    {
        BinaryFormatter bf = new BinaryFormatter ();
        FileStream file = File.Create(Application.dataPath + "/save.txt");

        PlayerData data = new PlayerData ();
        data.blueWaffle = blueWaffle;
        data.posX = posX;
        data.posY = posY;
        data.posZ = posZ;
        data.levelName = levelName;
        bf.Serialize(file, data);
        file.Close ();
    }

    public void Load()
    {
        if (File.Exists (Application.dataPath + "/save.txt")) 
        {
            BinaryFormatter bf = new BinaryFormatter ();
            FileStream file = File.Open (Application.dataPath + "/save.txt",FileMode.Open);
            PlayerData data = (PlayerData)bf.Deserialize (file);
            file.Close ();

            levelName = data.levelName;
            blueWaffle = data.blueWaffle;
            posX = data.posX;
            posY = data.posY;
            posZ = data.posZ;

            StartCoroutine ("LoadLevel");


            /*
            SceneManager.LoadScene (levelName);

            if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName(levelName)) 
            {
                //Vector3 loadedPosition = new Vector3 (posX, posY, posZ);


                Debug.Log ("I changed my position" + posX + posY + posZ);
                ScoreManager.Reset ();
                ScoreManager.AddPoints (blueWaffle);
                //player.GetComponent<Transform> ().position = loadedPosition;
                if (loaded (posX, posY, posZ)) {
                    Debug.Log ("I changed my position");
                } 
                else 
                {
                    Debug.Log ("I didint change my position");
                }
                Time.timeScale = 1f;
            }*/

        }
    }
    AsyncOperation asyncLoadLevel;
    IEnumerator LoadLevel()
    {
        asyncLoadLevel = SceneManager.LoadSceneAsync (levelName, LoadSceneMode.Single);
        while (!asyncLoadLevel.isDone) 
        {
            Debug.Log ("prepare");
            yield return null;
        }
        Debug.Log ("I changed my position" + posX + posY + posZ);
        ScoreManager.Reset ();
        ScoreManager.AddPoints (blueWaffle);

        Vector3 loadedPosition = new Vector3 (posX, posY, posZ);
        player.GetComponent<Transform> ().position = loadedPosition;
        /*
        if (loaded (posX, posY, posZ)) 
        {
            Debug.Log ("I changed my position");
        } 
        else 
        {
            Debug.Log ("I didint change my position");
        }*/
        Time.timeScale = 1f;
    }

/*  private Boolean loaded(float posX, float posY, float posZ)
    {
        Vector3 loadedPosition = new Vector3 (posX, posY, posZ);
        player.GetComponent<Transform> ().position = loadedPosition;
        return true;
    }
    */  


}
[Serializable]
class PlayerData
{
    public int blueWaffle;
    public float posX;
    public float posY;
    public float posZ;
    public string levelName;
}

嗯...为什么它看起来像你的帖子我ostly 代码?:D

4

1 回答 1

1

您正尝试在加载场景的同一帧上访问场景中的数据。

看看等到场景完成后再设置对象的位置。

AsyncOperation asyncLoadLevel;

IEnumerator LoadLevel()
{
    asyncLoadLevel = SceneManager.LoadSceneAsync(levelName, LoadSceneMode.Single);
    while (!asyncLoadLevel.isDone)
    {
        print("Loading the Scene");
        yield return null;
    }
    //the game has finished loading
    ScoreManager.Reset();
    ScoreManager.AddPoints(blueWaffle);
    //player.GetComponent<Transform> ().position = loadedPosition;
    if (loaded(posX, posY, posZ))
    {
        Debug.Log("I changed my position");
    }
    else
    {
        Debug.Log("I didint change my position");
    }
    Time.timeScale = 1f;
}
于 2018-03-09T16:32:55.710 回答