最近在做的東西大部分都要用到instantiate, 實例化某個prefab物體,實例化的物體若沒有指定父物體,就會自動生成到根目錄下。
這是出現了一個問題,當實例化物體后,更改parent值,這時,實例化物體的scale值會產生相應的改變
有兩種解決辦法
1、instantiate本身可以有父物體參數 Instantiate<T>(T original, Vector3 position, Quaternion rotation, Transform parent),
這樣實例化出來的物體不會出現scale中的改變(因為沒有在外部更改父物體,一次性成品,安全)
1 Instantiate(twoDPreb, twoDPreb.transform.position, twoDPreb.transform.rotation, this.transform.Find("Panel").transform);
2、如果是實例化后,更改父物體導致scale值更改,也可以在下面更改實例化物體的localScale的值來更改其scale值
1 GameObject obj = Instantiate(twoDPreb, twoDPreb.transform.position, twoDPreb.transform.rotation); 2 obj.transform.SetParent(this.transform.Find("Panel").transform); 3 obj.transform.localScale = new Vector3(1, 1, 1);