2013-10-31 55 views
0

遊戲我想和普通物理學的Minecraft類遊戲(冰塊滑落在地板上,當你把他們舉例)在C#中使用Unity期間對象的任何時間。負載的.FBX使用Unity

現在我試圖在被擊中時摧毀一個玻璃立方體。 我已經有了.fbx(用Blender創建的多維數據集爆炸動畫),當它在場景中拖動並播放時,它可以正常工作,但我只想在特定時間播放它。 這不只是一個動畫。它有.fbx內的對象我的玻璃立方體是一個遊戲對象。我有一個Class GenMap(MonoBehaviour),我讀了一個配置文件來了解立方體的位置和立方體的類型(污垢/冰/玻璃/石頭)。

如果它是一個玻璃立方體我添加了一個組件,它是在另一個的.cs易碎類。

在這個類中我有一個OnCollisionEnter功能破壞我的立方體和播放動畫。

我的問題是,我不知道如何讓我的.fbx在這個時刻。

我嘗試了公共變換對象,把我.FBX到Unity但因爲我用addComponent(「易碎」)我的公共變量爲空我的物體上時,一直產生它。 我明白,當我addcomponent我附上的.cs但不是公共變量我手動分配

void AttachTextureToObject(GameObject obj, int type) //Assign a texture to an object 
{ 
    _meshrenderer = obj.GetComponent("MeshRenderer") as MeshRenderer; 
    switch (type) 
    { 
     case 1: 
      _meshrenderer.material.mainTexture = textureG; //textureGrass i initialised in a special function 
      break; 
     case 2: 
      _meshrenderer.material.mainTexture = textureD; 
      break; 
     case 3: 
      _meshrenderer.material.mainTexture = textureS; 
      break; 
     case 4: 
      _meshrenderer.material.mainTexture = textureW; 
      break; 
     case 5 : 
      _meshrenderer.material.mainTexture = textureI; 
      obj.collider.sharedMaterial = materialI; 
      break; 
     case 10: //glass type 
      obj.AddComponent("SurfaceReflection"); //add a shader to get glass "texture" 
      obj.AddComponent("Breakable"); //add my class breakable 
      obj.renderer.material.shader = shaderG; 
      obj.collider.sharedMaterial = materialI; 
      break; 
    } 
} 

//不同的.cs

public class Breakable : MonoBehaviour 
{ 

    public Transform breaking_cube; //when i hit play the .sc attached to my gameobject is null and if i drag my .fbx manually and hit my cube i see the animation 
    Transform current_cube; 

    bool broke = false; 

    void Start() //when i addComponent i initialize the current cube to itself 
    { 
     current_cube = this.transform; 
     breaking_cube = Resources.Load("Blender/broken_cube") as Transform; //this doesn't work 
    } 

    void OnCollisionEnter(Collision other) 
    { 
     if (other.gameObject.GetComponent<Rigidbody>()) 
     { 
      broke = true; 
      Destroy(current_cube); 
      Transform broken_cube = Instantiate(breaking_cube, transform.position, Quaternion.identity) as Transform; 
      current_cube = broken_cube; 
      if (broke) 
      { 
       if (!current_cube.transform.animation.isPlaying) 
       { 
        //i will destroy my gameobject when i finish 
       } 
      } 
     } 
    } 
} 

我試圖addcomponent動畫,並添加我的.FBX但由於它不僅僅是一個動畫,它也不起作用。

我最近開始使用Unity,所以我可能錯過或誤用了一個函數。 我不知道我是否足夠清楚,但如果你需要截圖或其他東西,讓我知道。

回答

0

創建Cube預製件。

(一旦在現場,拖動遊戲物體回項目的層次結構,它會成爲一個預製)。現在

,你需要給你任何時候都可以實例化預製的副本:

public Transform CubePrefab; 

void Start() 
{ 
    GameObject cube = (GameObject)Instantiate(CubePrefab, new Vector3(1, 1, 0), Quaternion.identity); 
    AttachTextureToObject(cube, 10/*glass*/); 
} 

參考Instantiating Prefabs at runtime

+0

感謝您的時間 我想你說的幾型動物的方法,但我一直有同樣的問題。 當我創建我的GameObject,如果它是一個玻璃立方體,我做 obj.addComponent(「breakable」)//我將腳本附加到我的對象 當我這樣做時,我的公共變量與我的預製初始化空值。 – Tsuneo62

+0

我在上面添加了你的AttachTextureToObject()調用。你是怎麼稱呼它的? – peterept

+0

號 我有一個GenMap類上的emptyObject 當我打玩 附上我在pos加載文件 0 0 0 10 //玻璃立方體x = 0時Y = 0 z = 0的 ... 我創建了一個遊戲對象一個論文的位置,並呼籲AttachTextureToObject(我創建的遊戲對象,10) 當我在10的情況下我 到達addComponent(「易碎」)至極是的.cs 我聯繫我的breakable.cs腳本 如果你魔杖我可以鏈接我的genmap.cs – Tsuneo62