在Unity3D中使用全局变量

在Unity3D中使用全局变量要注意以下几点

1.定义一个Global类,将静态变量写在里面

2.Global中不引用MonoBehaviour对象,除非这个对象被执行过DontDestroyOnLoad否则在切换场景时会变成null


using UnityEngine;
using System.Collections;

public class GlobalMono : MonoBehaviour
{
    public static int a = 1985;
    public static TestStatic b = new TestStatic();
}


public class Global
{
    public static int a = 1983;
    public static TestStatic b = new TestStatic();
    public static cgame c = null;
}

public class TestStatic
{
    int a = 111;
}


猜你喜欢

转载自blog.csdn.net/qq_22822335/article/details/51024444