简体   繁体   English

在Unity(C#)中访问全局游戏数据

[英]access global game data in Unity (C#)

i'am completely new to Unity and C#. 我对Unity和C#完全陌生。 I try to use my OOP knowledge to build something that holding the data and "business logic" of my game. 我尝试使用自己的OOP知识来构建一些东西,以容纳游戏的数据和“业务逻辑”。

Game outline: Some cubes falling down, can be moved while falling and finally dropping on an pane. 游戏概述:一些立方体掉落,可以在掉落时移动,最后放到窗格上。

The cubes are prefab objects and must be tracked to count the score, detect un/wanted behaviour, etc. 多维数据集是预制的对象,必须进行跟踪以计算分数,检测不良行为等。

In the scene is: 在场景中是:

  • 1 empty GameObject = CubeSpanPoint + CubeSpawn Script 1个空GameObject = CubeSpanPoint + CubeSpawn脚本
  • 1 empty GameObject = GameMain + GameMain script (holding game settings and business logic) 1个空的GameObject = GameMain + GameMain脚本(保存游戏设置和业务逻辑)
  • 1 prefab Cube + MoveScript + FallingScript 1个预制立方体+ MoveScript + FallingScript

GameMain.cs GameMain.cs

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

[System.Serializable]
public class GameMain : MonoBehaviour
{
    private float CubeSpawnSpeed = 3.0f;  //seconds
    private int score = 0;

    // construtor
    public GameMain()
    {
    }

    // getter: is falling allowed for the cube
    public bool getFalling(){
        return true;
    }

    // getter cube spawn speed
    public float getCubeSpawnSpeed(){
        return this.CubeSpawnSpeed;
    }
}

The FallingScript must get the data from MainGame (getters), FallingSpeed and the falling permission. FallingScript必须从MainGame(获取程序),FallingSpeed和下降许可中获取数据。

CubeFalling.cs CubeFalling.cs

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

public class BoxFalling : MonoBehaviour
{
    public GameMain gameMain;

    void Start(){
        // an attempt to get the instance this way, but fail
        // GameMain main = GameObject.Find("GameMain").GetComponent<GameMain>();
    }

    void Update()
    {
        // Is still falling allowed?
        if (gameMain.getFalling()){
            transform.Translate(Vector3.down);
        }
    }
}

But this produces the error NullReferenceException: Object reference not set to an instance of an object BoxFalling.Update () 但这会产生错误NullReferenceException: Object reference not set to an instance of an object BoxFalling.Update ()

It is also not possible to use the inspector by pulling the GameMain GameObject onto gameMain slot in the CubeFalling Script to the same named public variable. 也不能通过将GameMain GameObject拉到CubeFalling脚本中的gameMain插槽到相同的命名公共变量来使用检查器。

What i've been expecting is that i've to pull the GameMain scipt to the GameMain GameObject, what is equal to an instantiation with new GameMain . 我一直期望的是,我必须将GameMain脚本拉到GameMain GameObject上,这等效于使用new GameMain进行实例化。 And to reuse this instance, that i've to pull the GameObject to the GameMain Variable in the falling script. 为了重用此实例,我必须在下降脚本中将GameObject拉到GameMain变量。 But this don't work. 但这不起作用。

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

[System.Serializable]
public class GameMain : MonoBehaviour
{   private static GameMain _gameMain;
    public static GameMain Instance {get {return _gameMain;}}
    void Awake()
    {
        _gameMain = this;
    }

    // getter: is falling allowed for the cube
    public bool getFalling(){
        return true;
    }
}

BoxFalling.cs BoxFalling.cs

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

public class BoxFalling : MonoBehaviour
{
    void Update()
    {
        // Is still falling allowed?
        // now you can got it
        if (GameMain.Instance.getFalling()){
            transform.Translate(Vector3.down);
        }
    }
}

You could use a Singleton to access GameMain . 您可以使用Singleton访问GameMain This would be handy when in future you are instantiating more cubes at runtime. 将来在运行时实例化更多多维数据集时,这将很方便。

But you also should be able to retrieve the GameMain using 但是您也应该能够使用以下方法检索GameMain

gameMain = GameObject.Find("GameMain").getComponent<GameMain>();

Perhaps you misspelled something? 也许您拼错了什么? Deactivated objects will not be found. 找不到停用的对象。

(Edit: This approach searches through all GameObjects in the hierarchy. At least cache the result. See more at Unity's documentation ) (编辑:此方法搜索层次结构中的所有GameObject。至少缓存结果。有关更多信息,请参见Unity文档

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

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