繁体   English   中英

从另一个场景加载场景时加载的内容与在编辑器中首先选择该场景时加载不同(Unity,c#)

[英]Scene loads differently when loaded from another scene than selecting that scene first, in the editor (Unity, c#)

真的很简单的问题,但在搜索互联网后,我很难找到答案。

问题

场景没有完全加载,一些部分工作,一些不工作,从另一个场景(通过Application.LoadLevel()),而直接打开场景然后播放它工作正常。

我试过的

  • 等待一段时间,看它是否只是花时间加载(1分钟+),与它自己加载的十秒相比。
  • 构建它并看看问题是否重复,允许我决定它是否是由编辑器引起的,就像你切换场景和灯光变暗一样。 但是,问题重演了。
  • 将我的Unity升级到版本5.3.4f1,然后使用SceneManager.LoadScene()而不是Application.LoadLevel()。 根本没有变化。

我认为可能导致它

  • 阅读很多网页我明白Unity的场景加载和场景系统可能很烦人

感谢您阅读本文,我希望这是一个简单的问题,但对于一个相当缺乏经验的Unity用户(ME)来说,这是非常令人困惑的。 如果需要任何确切的细节,我会添加它们,但对我来说,它们是不必要的(并且也需要付出很多努力)。

更多细节(如果您需要)

视频显示直接从另一个场景加载的场景( HERE

脚本有趣的调试结果 -

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ObstacleController : MonoBehaviour {
    ButtonCreator ButtonCreator;
    List<GameObject> LineGameObjects;
    List<LineClass.Line> LineInfos;
    List<GameObject> Positions;
    List<GameObject> Obstacles;
    List<GameObject> PossiblePositions;
    public List<Vector3> Targets;
    List<Vector3> PossiblePos;
    List<LineClass.Line> ConnectedLines;
    GameObject Ball;
    public GameObject ball;
    public int numberOfBalls;
    public float speed;
    Vector3 noGoPoint;
    List<float> Speeds;
    public List<Vector3> PastReachedPoints;
    bool Targeted;
    public GameObject cross;
    int i99;
    // Use this for initialization
    void Start() {
        i99 = 0;
        Speeds = new List<float>();
        ConnectedLines = new List<LineClass.Line>();
        ButtonCreator = GetComponent<ButtonCreator>();
        LineGameObjects = new List<GameObject>();
        LineInfos = new List<LineClass.Line>();
        Positions = new List<GameObject>();
        PossiblePositions = new List<GameObject>();
        Obstacles = new List<GameObject>();
        Targets = new List<Vector3>();
        PossiblePos = new List<Vector3>();
        Targeted = true;
        PastReachedPoints = new List<Vector3>();
    }

    void LateUpdate()
    {
        GameObject[] gameObjects = GameObject.FindGameObjectsWithTag("cross");
        foreach (GameObject target in gameObjects)
        {
            GameObject.Destroy(target);
        }
        Instantiate(cross, noGoPoint, Quaternion.identity);
    }
    // Update is called once per frame
    void Update() {
        Debug.Log("HEY"); // LOOK HERE <- <- <- <- <- 
        if (Time.frameCount == 1)
        {
            Debug.Log("2HEY"); // LOOK HERE <- <- <- <- <- 
            // connect up lists
            LineGameObjects = ButtonCreator.LineGameObjects;
            Positions = ButtonCreator.Positions;
            LineInfos = ButtonCreator.LineInfos;
            //intialise possible positions
            PossiblePositions.Clear();
            int i = 0;
            while (i < Positions.Count)
            {
                PossiblePositions.Add(Positions[i]);
                i++;
            }
            // set number of balls
            numberOfBalls = 11;
            // create balls
            i = 0;
            while (i < numberOfBalls)
            {
                GameObject Ball = Instantiate(ball) as GameObject; // create the ball
                int i2 = Random.Range(0, PossiblePositions.Count); // randomise the index
                Ball.transform.position = PossiblePositions[i2].transform.position; // set the positions
                Ball.transform.position = new Vector3(Ball.transform.position.x, Ball.transform.position.y, -0.1f);// set the z to -1
                Obstacles.Add(Ball); // add to list
                Debug.Log("3HEY"); // LOOK HERE <- <- <- <- <- 
                i++;
            }
            PossiblePositions.Clear();
            i = 0;
            while (i < Positions.Count)
            {
                PossiblePositions.Add(Positions[i]);
                i++;
            }
            // reset possible positions

            setNoGo();
            setTarget();
        }

        bool TargetsReached = false;
        bool TargetsNotReached = false;
        int i5 = 0;
        while (i5 < Obstacles.Count)
        {
            if (Obstacles[i5].transform.position == new Vector3(Targets[i5].x, Targets[i5].y, -1))
            {
                TargetsReached = true;
            }
            else
            {
                TargetsNotReached = true;
            }
            i5++;
        }

        if (TargetsReached == true && TargetsNotReached == false)
        {
            Targeted = false;
        }
        if (Targeted == false)
        {
            SetupTarget();
        }
        if (Targeted == true)
        {
            int i3 = 0;
            while (i3 < numberOfBalls)
            {
                Vector3 current = Obstacles[i3].transform.position;
                Vector3 target = new Vector3(Targets[i3].x, Targets[i3].y, -1);
                float step = Speeds[i3] * Time.deltaTime;
                Obstacles[i3].transform.position = Vector3.MoveTowards(current, target, step);
                i3++;
            }
        }

    }
    void SetupTarget ()
    {
        if (i99 == 2)
        {
            setNoGo();
            i99 = 0;
        }
        setTarget();
        i99++;
        Targeted = true;
    }
    void setNoGo() // if targeted = false as targets have reached
    {
        Vector3 previous = noGoPoint;
        PossiblePositions.Clear();

        int i = 0;
        while (i < Positions.Count)
        {
            PossiblePositions.Add(Positions[i]);
            i++;
        }
        i = 0;
        while (i < PossiblePositions.Count)
        {
            bool isThereBlue; // will return true if at least one of the connected lines of this point is blue
            isThereBlue = false;

            // for each possible position find all the connected lines
            ConnectedLines.Clear();
            int i2 = 0;
            while (i2 < LineInfos.Count)
            {
                if (LineInfos[i2].endpos == PossiblePositions[i].transform.position || LineInfos[i2].startpos == PossiblePositions[i].transform.position)
                {
                    ConnectedLines.Add(LineInfos[i2]);
                }
                i2++;
            }

            // checks if there is a connected blue line
            i++;
            int i3 = 0;
            while (i3 < ConnectedLines.Count)
            {
                if (ConnectedLines[i3].colour == Color.blue)
                {
                    isThereBlue = true;

                }
                i3++;
            }

                // if there are no connected blue lines remove from the selection for no go zone
                if (isThereBlue == false)
                {
                    PossiblePositions.RemoveAt(i);
                    if (i != 0)
                    {
                        i--;
                    }
                }
            i++;

        }

        i = 0;
        while(i < PossiblePositions.Count)
        {
            if (PossiblePositions[i].transform.position == previous)
            {
                PossiblePositions.RemoveAt(i);
                if (i != 0)
                {
                    i--;
                }
            }
            i++;
        }
        // for each obstacle
        // when targeted = false
        // choose a target, which is not - 
        // where it is now
        // or in the no go area
        i = Random.Range(0, PossiblePositions.Count);
        noGoPoint = PossiblePositions[i].transform.position;
        }


    void setTarget()
    {
        PastReachedPoints.Clear();
        Targets.Clear();
        Speeds.Clear();
        int i3 = 0;
        while (i3 < numberOfBalls)
        {
            Vector3 point = Obstacles[i3].transform.position;
            //if (i3 == 1)
            //{
            //    Instantiate(cross, point, Quaternion.identity);
            //}
            point.z = 0;
            PossiblePos.Clear();
            // find all the possible positions to move towards
            int i = 0;
            while (i < LineInfos.Count)
            {
                if (point == LineInfos[i].startpos)
                {
                    PossiblePos.Add(LineInfos[i].endpos);
                }

                else if (point == LineInfos[i].endpos)
                {
                    PossiblePos.Add(LineInfos[i].startpos);
                }
                i++;
            }
            //i = 0;
            //while (i < PossiblePos.Count)
            //{
            //    if (i3 == 1)
            //    {
            //        Instantiate(ocross, PossiblePos[i], Quaternion.identity);
            //    }
            //    i++;
            //}
            PossiblePos.Remove(point);

            // remove any from the no go list
            i = 0;
            while (i < PossiblePos.Count)
            {
                    if (new Vector2(PossiblePos[i].x, PossiblePos[i].y) == new Vector2(noGoPoint.x, noGoPoint.y))
                    {
                        PossiblePos.RemoveAt(i);
                        if (i != 0)
                        {
                            i--;
                        } 
                    }   
                i++;
            }
            //choose the random target
            //i = 0;
            //while(i < PossiblePos.Count)
            //{
            //
            //    if (i3 == 1)
            //    {
            //        Instantiate(rmarker, PossiblePos[i], Quaternion.identity);
            //    }
            //    i++;
            //}
            PastReachedPoints.Add(point);
            Targets.Add(PossiblePos[Random.Range(0, PossiblePos.Count)]);
            float time = 0.3f;
            float distanceInBetween = Vector3.Distance(Obstacles[i3].transform.position, Targets[i3]);
            float speed = distanceInBetween / time;
            Speeds.Add(speed);
            i3++;
        }

    }
}

控制台消息

直接加载场景时

SceneDirect

但有趣的是,当它从另一个场景加载时,它没有“2HEY”或“3HEY”的任何调试。

Time.frameCount从游戏开始计算帧数。 因此,如果从另一个场景加载场景, Time.frameCount会大于0。

此外,调试游戏的速度更快,而不是使用日志来查看正在发生的事情。 只需按下MonoDevelop中的Play并(对于Unity 4.3及更低版本)选择Unity Debugger,将断点放在需要停止和检查变量值的位置,然后点击Unity中的Play

祝好运

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM