How to modify RectTransform properties in script [Unity 4.6 beta]

后端 未结 7 934
面向向阳花
面向向阳花 2020-12-24 15:03

\"enter

Hello, I\'m using the new UI system from Unity 4.6 beta...

Tried diffe

7条回答
  •  醉话见心
    2020-12-24 15:42

    You normally don't want to modify directly those values, even more if they come from a prefab you have already setup with the correct coords, if you just want to add it correctly under canvas (or other) but it "changes position" do this:

    GameObject instance;
    
    void Start () {
        GameObject canvas = GameObject.Find ("Canvas");
        if (canvas != null) {
            GameObject go = Resources.Load("MyPrefab");
            if(go != null){
                instance = Instantiate(go);
                instance.GetComponent().SetParent(canvas.GetComponent(), false);
            }
        }
    }
    

    Passing false on set parent will avoid the rect transform being changed in weird forms when instantiating the prefab.

提交回复
热议问题