UNITY在加载场景后不能改变transform.position

时间:2018-03-09 16:19:39

标签: c# unity3d transform loading scene-manager

我的C#scirpt有些问题。所以,问题是加载场景后,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;
}
嗯...为什么它看起来像你的帖子我的代码? :d

1 个答案:

答案 0 :(得分: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;
}
相关问题