【问题标题】:How do I carry over data between scenes in Unity?如何在 Unity 中的场景之间传递数据?
【发布时间】:2019-01-04 07:53:35
【问题描述】:

对于我正在开发的游戏,如果用户离敌人太近,它会将场景切换到战斗场景。但是我不知道如何将该敌人加载到战斗屏幕中(假设用户可以与许多不同的敌人作战)。下面是我目前对敌人的鳕鱼。我想知道我是否可以将它的名字带入下一个场景或其他什么。当场景改变时,我只想让我的敌人从一个屏幕转到另一个屏幕。代码将不胜感激谢谢

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

public class FolllowAndLoad : MonoBehaviour
{
public Transform target;
public Animator anim;
public Rigidbody2D myRigidBody;
public string levelToLoad;
private static string keyname; // value I want to carry over
public float MoveSpeed;
private bool checkTrigger;
public Rigidbody2D targetRigidBody;



void Start()
{
    target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();//getting the position of our player

    anim = GetComponent<Animator>();
    myRigidBody = GetComponent<Rigidbody2D>(); //getting my components

    targetRigidBody = GameObject.FindGameObjectWithTag("Player").GetComponent<Rigidbody2D>();

}

void Update()
{
    float distance = Vector2.Distance(target.position, myRigidBody.transform.position); //getting the distance between our player and our enemy
    if (distance < 5)
    {
        transform.position = Vector2.MoveTowards(transform.position, target.position, MoveSpeed * Time.deltaTime); //moving our enemy towards our player
        anim.SetBool("checkTrigger", true);
        anim.SetFloat("MoveX", moveXvalue()); //updating the animations for our enemy
        anim.SetFloat("MoveY", moveYvalue());

    }
    else if (distance > 5)  //if out of range stop walking
    {
        anim.SetBool("checkTrigger", false);

    }
}

int moveXvalue()
{
    int value;

    if (myRigidBody.transform.position.x < target.transform.position.x && Mathf.Abs(target.position.y - myRigidBody.position.y) < Mathf.Abs(target.position.x - myRigidBody.position.x)) //these are saying if the enemy is closer in x than in y use x animations and vice versa
        value = 1;
    else if (myRigidBody.transform.position.x > target.transform.position.x && Mathf.Abs(target.position.y - myRigidBody.position.y) < Mathf.Abs(target.position.x - myRigidBody.position.x))
        value = -1;
    else
        value = 0;
    return value;
}

int moveYvalue()
{
    int value;

    if (myRigidBody.transform.position.y < target.transform.position.y && Mathf.Abs(target.position.y - myRigidBody.position.y) > Mathf.Abs(target.position.x - myRigidBody.position.x))
        value = 1;
    else if (myRigidBody.transform.position.x > target.transform.position.x && Mathf.Abs(target.position.y - myRigidBody.position.y) > Mathf.Abs(target.position.x - myRigidBody.position.x))
        value = -1;
    else
        value = 0;
    return value;
}

public void OnTriggerEnter2D(Collider2D other)
{
    if (other.gameObject.name == "Player")
    {
        Debug.Log(gameObject.name);

        anim.SetBool("checkInContact", true);

        Application.LoadLevel (levelToLoad); //loading our level


        }
    }
}

【问题讨论】:

  • Unity - pass data between scenes 的可能重复项。无需任何工作即可找到此副本。这是我点击关闭时第一个建议的问题。
  • @Draco18s 不是重复的,因为他想加载文本(分数值)和我一个字符
  • 同样的原则适用于两者,请记住我们不是代码编写服务,答案不应该被剪切和粘贴。您可以使用列出的相同方法来移动分数,就像移动玩家对象一样,请先尝试一些建议的答案。
  • 附言。在链接的问题中,答案 2 应该对您有用,祝您好运!

标签: c# unity3d


【解决方案1】:

有很多方法可以做到这一点,但在您对 Unity 更加熟悉之前,最简单的方法是在您的项目中使用一个简单的静态类,您可以从任何场景中的任何脚本访问它.

因此,如果您现在要在项目中创建一个名为 SharedResources.cs 的新脚本,然后将其粘贴到脚本中并保存......

public static class SharedResources
{
    public const int kSceneIs_TitleScene = 0;
    public const int kSceneIs_ActualGameScene = 1;
    public const int kSceneIs_HighScoreScene = 2;

    public static int highScore = 0;
    public static int enemyID = 0;

    public static void sampleFunction()
    {
        //this is a sample method you can call from any other script
    }

}

您现在可以在一个场景中的脚本中执行此操作

SharedResources.highScore=SharedResources.highScore+20;
SharedResources.enemyID=5;

然后您可以打开一个新场景,并且该场景中的脚本可以访问高分

Debug.Log(SharedResources.highScore)
Debug.Log(SharedResources.enemyID)

您还可以访问常量并运行静态类中的子例程,如上所示。

执行此操作的正确方法有待商榷,实际上取决于您的最终目标是什么。我将参考另一个链接到一个更详细的帖子....

https://gamedev.stackexchange.com/questions/110958/unity-5-what-is-the-proper-way-to-handle-data-between-scenes

理想情况下,您应该阅读并理解使用简单静态类与派生自 MonoBehavior 的静态类之间的区别,以及静态类和 Singleton 之间的区别,后者在许多方面更强大(但也可能导致如果您没有正确编码会出现问题)

最后但同样重要的是,不要忘记您还可以使用 Unity 中内置的 PlayerPrefs 功能来存储分数和其他需要在游戏启动之间保留的设置....

https://answers.unity.com/questions/1325056/how-to-use-playerprefs-2.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-14
    • 2012-07-27
    • 1970-01-01
    相关资源
    最近更新 更多