如何在Unity中的场景之间传递数据?

时间:2019-01-03 22:23:19

标签: c# unity3d

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

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


        }
    }
}

1 个答案:

答案 0 :(得分: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