原文地址:http://blog.sina.com.cn/s/blog_7d9405e50100s061.html
今天在使用Unity3D的時候遇到了一個問題:_tesGameObject是在Project中的一個Prefab。
public GameObject _testGameObject;
void Awake()
{
Mesh mesh=_testGameObject.GetComponent<MeshFilter>().mesh;
Debug.Log(mesh.bounds.size);
}
這樣使用的時候會導致將Prefab的Mesh給去掉了。所以第一次使用的時候好使。如果再次執行的時候就會遇到Prefab中的Mesh為Null了。 不知道是Unity3D的一個Bug還是自己理解有誤。反正感覺就是Prefab的一些屬性不能直接讀取,需要實例化之后才能正常讀取
最后:解決方法是:
public GameObject _testGameObject;
void Awake()
{
GameObject gameInstance = (GameObject)Instantiate(_testGameObject);
gameInstance.transform.position = Vector3.zero;
gameInstance.name = _testGameObject.name;
Mesh mesh=gameInstance.GetComponent<MeshFilter>().mesh;
Debug.Log(mesh.name);
Debug.Log(mesh.bounds.size);
}