【问题标题】:Getting text to stay when changing scenes and then destroying when entering another更改场景时让文本保留,然后在进入另一个场景时销毁
【发布时间】:2016-12-23 23:04:10
【问题描述】:

我正在创建一款专为视力受损的游戏玩家设计的游戏。我的分数文本当前会在游戏过程中显示。一旦你丢失它会加载一个新场景(结束屏幕),但我希望在加载结束场景时保持分数,然后在再次加载游戏屏幕时将其重置为 0,然后在玩家决定返回时将其删除到主菜单。

这是加载下一个场景的内容。

public int amount;
public void ChangeScene(int changeTheScene)        

{
    SceneManager.LoadScene(changeTheScene);
}

void Start()
{

}
void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        amount += 1;
        Debug.Log(amount);
        if (amount == 10)      
        {
            SceneManager.LoadScene(2); 
        }
    }
}
}

这就是我跟踪分数的方式

void Start()
{
    count = 0;
    SetCountText();
    float x = Random.Range(325f, -600f);
    float y = Random.Range(250f, -450f);
    Debug.Log(x + "," + y);
    prefab.GetComponent<RectTransform>().anchoredPosition = new Vector2(x,    y);
    loseObject.GetComponent<Lose>().amount = 0;
}

public void move()
{
    float x = Random.Range(325f, -600f);
    float y = Random.Range(250f, -450f);
    Debug.Log(prefab.transform.position.x + "," +       prefab.transform.position.y);
    prefab.GetComponent<RectTransform>().anchoredPosition = new Vector2(x,  y);
    loseObject.GetComponent<Lose>().amount = 0;
    count = count + 1;
    SetCountText();
}


void SetCountText()
{
    countText.text = "Count: " + count.ToString();
}
}

【问题讨论】:

  • 请参考:How to Ask,请出示minimal reproducible example
  • 嘿,您必须制作一个在加载时不会破坏分数的对象。然后它将持续存在于场景中。 docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
  • 嗨,我只需要它进入一个场景然后消失,我该怎么办?加载时也不要破坏对附着在画布上的任何东西都不起作用,我需要一个文本对象来继续下一个场景而不是游戏对象。有什么帮助吗?干杯
  • 对文本使用 dontdestroyonload 函数,有某种指向该实例的指针,然后在您想删除文本时调用销毁或池化文本。
  • Alox,你介意给我写个例子吗?在编程方面,我有点新手。 (设计师 xD)

标签: c# unity3d


【解决方案1】:

我不完全确定这是否可行,但要回答您的问题,我猜这会满足您的需求。

using UnityEngine;

//This would be attached to said gameobject that displays the amount.
public class Amount : MonoBehaviour
{
    public static Amount instance = null;

    void Awake()
    {
            if (instance == null)
                instance = this;

            if (instance != this)
                Destroy(gameObject);

            DontDestroyOnLoad(gameObject);
    }
}

//Here would be what's needed to set the amount's parent.
public class SetThatAmountsParentToThisGameObject : MonoBehaviour
{
    void SetAmountParent()
    {
        Amount.instance.transform.SetParent(transform);
    }
}

//And here you would destroy said gameobject that displays the amount.
public class DestroyThatAmount : MonoBehaviour
{
    void DestroyIt()
    {
        Destroy(Amount.instance.gameObject);
    }
}

请注意,我尚未对此进行测试,因此不确定这是否真的有效。我将其写为答案,因为您要求提供示例,并且代码在答案中更清晰,更易于阅读。

【讨论】:

  • 非常感谢您抽出宝贵时间提供帮助。我会尽快试一试,看看效果如何。即使它不起作用,我真的很感激。
  • 是的,它仍然无法正常工作,因为我使用文本来显示分数并且整个画布都被破坏了。
  • 对,然后将dontdestroyonload的代码放在canvas游戏对象上,然后销毁。如果画布上还有其他游戏对象您想在场景之间保留,则使用一个变量来跟踪文本并在文本的游戏对象上调用销毁函数。
  • 好的!我们正在到达某个地方!它现在保持分数!但是第一个问题,它没有出现在游戏场景中,因为它从我的另一个场景中获取了画布并停留在上面,它需要出现在新场景画布上。有什么想法吗?
  • euh...然后回到你之前的状态,使用这个函数 Amount.instance.transform.setParent(transform);在您希望它所在的新画布中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-22
  • 2022-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-02
相关资源
最近更新 更多