1 设为public方便其它脚本调用。
但这时 Inspector上对应的脚本点开,变量会全部展开。有时候直接影响工程的编写,不小心打开,电脑卡死等。
2 可以申请为 private变量,其它脚本里用,则写一个函数,相互传值。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Variables : MonoBehaviour {
// Use this for initialization
public GameObject[] gobs = new GameObject[100];
private GameObject[] priGobs;
void Start () {
priGobs = new GameObject[100];
}
// Update is called once per frame
void Update () {
}
void GiveValues(GameObject[] gob)
{
//do something
}
}
3, 像上面以对象数组为例,如何快速赋值
脚本如下:下面完成对 gobs对象数值的赋值。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Variables : MonoBehaviour {
// Use this for initialization
public GameObject[] gobs = new GameObject[20];
private GameObject[] priGobs;
void Start () {
priGobs = new GameObject[100];
foreach (var item in gobs) {
print (item.name);
}
}
// Update is called once per frame
void Update () {
}
void GiveValues(GameObject[] gob)
{
//do something
}
}
3.1 赋值
A 简单的做法是一个一个的托动赋值,20还可以接受,如果是100呢?当然可以通过脚本来赋值,这儿指通过如何快速实现托动对象数组赋值。
因为一次多个赋值时,对象选好时,脚本在 Inspector不显示,如下
B 如果选对象时,脚本能显示不就可以赋值了吗?
所以即锁定Inspector
C 选择对象,直接托动赋值。但会发现对象数组的空间会自动变为以前的两倍,前面的一部分空间没有赋值,后面的赋值了。
如下:
运行程序也会有错误
D 在Inspector上把数组空间大小先修改为0,再托动赋值,OK.
运行结果