【问题标题】:Canvas wont enable it after got loaded in new sceneCanvas 在新场景中加载后不会启用它
【发布时间】:2017-08-20 19:28:34
【问题描述】:

所以我有一个游戏对象,可以在必要时加载新场景。附加到游戏对象的脚本有一个变量,它保存对画布元素的引用(画布元素有一个进度条)。问题是,在我加载一个新场景并且我想在该加载的场景中加载一个新场景后,我失去了对画布对象的引用,因为我无法将他设置为 DontDestroyOnLoad。

我所做的是我将画布元素(参见 start function() 中的注释行)作为不会被破坏的游戏对象的子元素。在我完成之后,我没有收到警告我丢失了对象引用,但它不会在 setNewNameAndLoadScene() 函数中启用画布。

我的意思是启用该 gui 不会出现在屏幕上,但它在层次结构中。我检查了它是否为空,但它不是。程序通过代码行运行没有问题,但画布没有出现在游戏屏幕上。

第二次编辑:我刚刚检查了(在运行时)如果我将我的画布(它是我的 gameobject 的一个孩子)放在新加载场景的画布中会怎样。发生的事情是它显示了我的画布。但是我问我为什么将它放入场景画布时会显示它。在我加载我的第一个场景之前,我的画布不在场景画布中,但仍然在游戏屏幕中显示了进度条

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

public class SceneLoaderGameObject : MonoBehaviour {


private SceneLoader sl;
public Canvas cav; //loses reference when loading new scene

void Start () {
    sl = new SceneLoader ();
    cav.GetComponent<Canvas> ().enabled = false;
    DontDestroyOnLoad (this.gameObject);
    DontDestroyOnLoad (this);
    DontDestroyOnLoad (this.gameObject.transform.GetChild(0).GetComponent<Canvas>()); 
}

public void setNewNameAndLoadScene(string name){

    if(name.Length ==0)
        throw new UnityException("length 0");

    if (cav != null) {
        Slider slid = cav.transform.GetChild (1).GetComponent<Slider> ();
        Text tx = cav.transform.GetChild (2).GetComponent<Text> ();
        Button bttn = cav.transform.GetChild (3).GetComponent<Button> ();

        sl.setNewScene (name);
        sl.setSliderAndTextToChange (slid, tx);
        bttn.onClick.AddListener (() => activateNewScene ());
        cav.GetComponent<Canvas> ().enabled = true;
        StartCoroutine (sl.LoadAsynchron (name, bttn));

    }

}

public void  activateNewScene(){
    sl.AsgetOP().allowSceneActivation=true;

 }
}

【问题讨论】:

  • 请在您的问题中详细说明。 “不会启用”是什么意思?使用Debug.Log 看看到底发生了什么。检查画布变量是否为空。检查编辑器并告诉我们画布对象是否仍然存在于层次结构中。将这些添加到您的问题中。帮助我们帮助您。
  • @Programmer 希望有帮助
  • @Programmer 或者是否可以不丢失对我的画布的引用。所以我不必让它成为一个孩子。所以我可以始终像使用已保存的变量一样使用它,因为它是一个画布,所以我启用/禁用它

标签: c# unity3d canvas object-reference


【解决方案1】:

只需使用下面的代码制作单例并获取滑块的公共 GameObject 引用或在场景加载时想要激活-停用的任何内容。例如,如果您有控制画布组件的游戏管理器,则将单例对象和引用对象相互关联。代码源是here

public class SceneLoaderSingleton : MonoBehaviour
{

    //Static instance of SceneLoaderSingleton which allows it to be accessed by any other script.
    public static SceneLoaderGameObject instance = null;    

    //Awake is always called before any Start functions
    void Awake()
    {
        //Check if instance already exists
        if (instance == null)

            //if not, set instance to this
            instance = this;

        //If instance already exists and it's not this:
        else if (instance != this)

            //Then destroy this. This enforces singleton pattern, meaning there can only ever be one instance of a SceneLoaderSingleton.
            Destroy(gameObject);    

        //Sets this to not be destroyed when reloading scene
        DontDestroyOnLoad(gameObject);            
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-12
    • 1970-01-01
    • 2020-05-18
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    相关资源
    最近更新 更多