Resources.Load返回null
在unity中Resources.Load从Assets下的任意Resources目录下读取资源,比如从Assets\Resources下读取Cube(预制体),当然也可以读取其他资源
代码为
GameObject prefab = Resources.Load<GameObject>("Cube");
Debug.Log(prefab);
问题:一个问题是我即使按照官方要求做,依旧返回null,代码如下
public class aircraftCenterMonitor: MonoBehaviour
{
public string prefabPath = "AircraftV2";
void Start()
{
GameObject prefab = Resources.Load<GameObject>(prefabPath);
Debug.Log(prefab);
Transform parentTransform = transform.parent;
Debug.Log("Parent Transform: " + parentTransform.name + ", Type: " + parentTransform.GetType());
}
void Update()
{
}
}
问题在于prefabPath, 可以如上述代码那样写,但是不建议。因为我即使把值设为“AircraftV2”,但是在Start函数第一行打印了下,发现值是我知道Resources.Load正确用法前设置的一个路径。因此将代码改为
public class aircraftCenterMonitor: MonoBehaviour
{
void Start()
{
GameObject prefab = Resources.Load<GameObject>("AircraftV2");
Debug.Log(prefab);
Transform parentTransform = transform.parent;
Debug.Log("Parent Transform: " + parentTransform.name + ", Type: " + parentTransform.GetType());
}
void Update()
{
}
}