關於Unity 獲得和使用GetComponent ().mesh時的心得


原文地址: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);
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM