Unity的C#编程教程_54_静态类型StaticTypes详解及应⽤练习⽂章⽬录
C# Static Types
1.Working with Static Types
所谓的 static type,使⽤的是 static 关键字
⽐如可以有 static class,static method,或者 static variable
static type 存在于栈 stuck 中(内存),⽣命周期与程序运⾏⼀致
任务说明:
我们设计⼀个程序⽤于接收信息,⽐如⼀堆数字,然后⽤程序加总
假设每次按下空格键加 10
传统的做法是⾸先新建⼀个 Score 的类,⽤于存放累加后的结果:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Score : MonoBehaviour
{
public int num;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
这个脚本可以挂载到⼀个游戏对象 Score 上⾯
然后再⽤⼀个脚本进⾏累加操作:
{
public Score score; // 实例化⼀个 Score 的类
// Start is called before the first frame update
void Start()
{
score = GameObject.Find("Score").GetComponent<Score>(); // 初始化
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
score.num += 10; // 对于类下⾯的 num 变量加 10
}
}
}
这个脚本也挂载到⼀个游戏对象下⾯,运⾏游戏后,按下空格键就可以看到 Score ⾥⾯的 Num 每次会加 10然后我们尝试把 Score 类下⾯的 num 变量改为 static:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Score : MonoBehaviour
{
public static int num;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
计数程序就要做对应修改:
{
// public Score score; // 不再需要实例化
// Start is called before the first frame update
void Start()
{
//score = GameObject.Find("Score").GetComponent<Score>(); // 也不需要初始化
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
//score.num += 10;
Score.num += 10; // 直接对于类下⾯的 num 变量加 10
Debug.Log(Score.num); // Inspector 中⽆法显⽰,需要通过 debug 进⾏显⽰
}
}
}
静态变量直接存储在内存中,⽆需通过类的实例化来分配内存,所以可以直接对其进⾏操作。
Static 的东西没有办法直接在 unity 的 inspector 中看到,所以可以通过 debug 的⽅式查看数值变化。
在运⾏⼤型项⽬的时候,由于静态数据会占据内存,需要额外关注。
不仅仅是变量可以 static,类和⽅法也可以是 static 的。
当你使⽤ static class 的时候,这个类⽆法继承别的⽗类,所以⾃然 static class ⽆法继承 MonoBehaviour。
2.Practical Example of Working with Static Types
实际运⽤ static type
创建⼀个 enemy spawn manager
显⽰ enemy 的数量,使⽤ UI 展现数字
使⽤ cube 代表 enemy,设定为 prefab
⾸先创建⼀个 cube,命名为 Emeny,然后拖拽到 assets 中作为 prefab,原窗⼝中的 Enemy 可以删除,然后创建脚本SpawnManager:
public class SpawnManager : MonoBehaviour
{
public GameObject enemyPrefab;
public static int enemyCount; // 这⾥使⽤静态变量
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
Instantiate(enemyPrefab); // 每次按下空格键⽣成 enemy
}
}
}
另外创建⼀个 Enemy 脚本,挂载到 Enemy 的 Prefab 上,⽤于控制 Enemy 的激活和关闭:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
private UIManager _ui; // 实例化
public void OnEnable() // unity ⾃带的⽅法,⽣成游戏对象的时候⾃动调⽤
{
// 静态变量可以直接操作,⾮常⽅便!
_ui = GameObject.Find("UIManager").GetComponent<UIManager>(); // 初始化
_ui.UpdateEnemyNum(); // 调⽤⽅法更新数字
Delet(); // 调⽤销毁⽅法
}
public void OnDisable() // unity ⾃带的⽅法,销毁游戏对象的时候⾃动调⽤
{
// 静态变量可以直接操作,⾮常⽅便!
_ui.UpdateEnemyNum(); // 调⽤⽅法更新数字
}
void Delet() // 销毁⽅法
{
Destroy(this.gameObject, Random.Range(2.0f, 4.0f)); // 2~4秒后销毁⾃⼰
}
}
这⾥我们发现,把 enemyCount 设置为静态变量后,操作起来⾮常⽅便!不需要有实例化和初始化过程,直接可以对其进⾏操作!
将 Enemy 的 Prefab 拖拽到 SpawnManager 脚本上。
创建⼀个空的游戏对象,命名为 UIManager,并挂载同名脚本,⽤于显⽰现存的 Enemy 数量:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; // 直接 using UI 库⽅便使⽤
public class UIManager : MonoBehaviour
{
public Text enemyNumText; // 实例化⼀个 Text
public void UpdateEnemyNum()
{
< = "Enemy Number: " + Count;
}
unity 教程
}
在场景中新建⼀个 Text 游戏对象,按照喜好修改⼤⼩颜⾊等参数,然后将其拖拽到 UI Manger 的脚本中。
运⾏游戏,按下空格键就会⽣成 Enemy,UI会显⽰数字增加,当 Enemy ⾃动销毁后,数字会减⼩。
注意⼏点:
static 的⽅法⽆法使⽤⾮ static 的变量。
静态的即不变的,在某个类中只有⼀个,不会因实例化对象的不同⽽不同。
静态类不能实例化,不能使⽤ new 关键字创建静态类类型的变量。
Instance Members vs Static Members
区别 instance 和 static
instance:实例化以后才存在
static:程序运⾏开始到结束都会存在,如果 class 中有⼀个静态变量,那么这个变量为所有该类的实例化进⾏共享这个问题也是⾯试常见题⽬

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。