例項化UGUI中GameObject 並設定它的位置
阿新 • • 發佈:2019-02-12
</pre><pre name="code" class="csharp"><pre name="code" class="csharp">using UnityEngine; using UnityEngine.UI; using System.Collections; public class ItemInfo : MonoBehaviour { public GameObject prefab;//預設體 GameObject pre; void Start() { pre = (GameObject)Instantiate(prefab); pre.transform.SetParent(GameObject.Find("Panel").transform); pre.GetComponent<RectTransform>().localPosition = new Vector3(50, 0, 0);//設定該物體的RectTransform元件的POS X、POS Y、POS Z pre.GetComponent<RectTransform>().sizeDelta = new Vector2(160, 211);//設定該物體的RectTransform元件的Width、Height<span style="font-family: Arial, Helvetica, sans-serif;"> </span> } }