在实际应用中,为了避免代码冗余,有的时候会用到 全局 静态 变量
本文 已 全局 静态数组为例 ,做出说明
----------------------------------------------------------------------
定义一个全局 静态 类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class ReferenceTable
{
public static string[] gv_str_array = { "hello", "hello_1", "hello_2", "hell123",
};
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test_List : MonoBehaviour {
private string[] str_array = ReferenceTable.gv_str_array;
// Use this for initialization
void Start () {
for (int i=0; i < str_array.Length;i++)
{
Debug.Log(" "+ str_array[i]);
}
}
}测试代码如上
结果如下所示

本文介绍了在Unity中创建和使用全局静态变量的方法,通过一个全局静态数组的例子,阐述了静态类和非静态类的区别。全局静态变量无法在调用的对象中改变原本的值,且静态类的成员变量必须是静态的。强调了全局静态变量的主要目的是减少代码冗余,优化代码。
最低0.47元/天 解锁文章
3065

被折叠的 条评论
为什么被折叠?



