动态添加Ugui组件
2017-09-12 17:08
447 查看
动态添加Ugui组件
动态添加是指:在场景中有一个canvas,使用代码,动态添加一个没有canvas的prefab,但是这个prefab需要挂载到canvas下,就是canvans下的ugui组件
如果anchors 的min和max 不是 (0.5, 0.5)的时候就会有问题,添加的位置是不对的,
具体什么原因不是很懂, 与锚点有关系
一下代码就可以避免这种情况
1 GameObject obj = Resources.Load<GameObject>("btn_info_test"); // 要动态添加的物体 2 GameObject child = GameObject.Instantiate(obj); // 实例化GameObject 3 child.transform.parent = tran; // 设置父物体 4 //child.GetComponent<RectTransform>().pivot = obj.GetComponent<RectTransform>().pivot; 5 // 设置anchors min的偏移量 ,就是anchors min 到 控件左下角的偏移量 6 child.GetComponent<RectTransform>().offsetMin = obj.GetComponent<RectTransform>().offsetMin; 7 // 设置anchors min的偏移量 , 就是anchors max 到 控件右上角的偏移量 8 child.GetComponent<RectTransform>().offsetMax = obj.GetComponent<RectTransform>().offsetMax;
相关文章推荐
- android中动态添加组件
- ExtJs 动态添加组件
- VB动态添加WebBrowser控件,并拦截弹出窗口(不用引用任何组件)
- 【Android】动态添加组件
- WeX5向data组件动态添加数据格式
- UGUI Text组件上动态显示Emoji
- C#动态组件添加以及预定义事件处理
- Android RelativeLayout 动态添加组件
- 给UGUI的Button动态添加OnClick监听
- Android 利用addView 动态给Activity添加View组件
- JQuery中动态添加easyui组件 动态渲染 parser具体怎么用
- Android笔记(六十一)动态添加组件
- 动态添加组件
- android 动态添加组件,layout的宽高的正确显示
- Flex动态添加组件例子 Flex如何动态添加组件
- Flex4 动态添加删除组件
- 动态添加组件或是客户端控件以及事件(Mondify By LiFuyun)
- Android使用addView动态添加组件的方法
- Ext如何动态添加一行组件
- JS动态添加文件上传组件 struts2