Unity的button脚本及MaterialPropertyBlock使用、C#数组转list
2017-06-21 17:26
477 查看
MaterialPropertyBlock使用
优点:设置material里shader的属性更直接,还能遍历整个场景的shader,设置其属性。代码
MaterialPropertyBlock props; private List<Renderer> myRenderer=new List<Renderer>(); props = new MaterialPropertyBlock(); Renderer[] rd =GameObject.FindObjectsOfType<Renderer>(); myRenderer =new List<Renderer>(rd); props.SetFloat("_ColliderForce", 0); foreach(Renderer render in myRenderer) render.SetPropertyBlock(props); 上面的脚本会把他所在场景所有其他物体属性都修改 当然,也可以每个物体挂一个脚本一个shader,只控制该物体本身的。 props.SetFloat("_ColliderForce", 0); foreach(Renderer render in myRenderer) render.SetPropertyBlock(props);
more
note:同一个物体上挂两个材质球也是好使的,两个材质球的属性都会被修改数组转list:new List(数组变量)
Gui值用法
button、label的使用
参考:http://blog.csdn.net/zuoyamin/article/details/17261361using UnityEngine; using System.Collections; public class Button1 : MonoBehaviour { public Texture img; private Texture img0; private string info;//显示的信息/ private int frameTime;//记录按下的时间/ MaterialPropertyBlock props; private Renderer myRenderer; void Start() { //初始化/ info = "请您点击按钮"; frameTime = 0; props = new MaterialPropertyBlock(); myRenderer = GetComponent<Renderer>(); } void OnGUI() { //标签/ //设置标签字体 GUIStyle style = GUI.skin.GetStyle("label"); style.fontSize = 40; GUI.Label(new Rect(50, 10, 200, 20), info); //普通按钮,点击后显示Hello World if (GUI.Button(new Rect(150, 100, 200, 20), "lyg设为0")) { //info = "Hello World"; props.SetFloat("_ColliderForce", 0); myRenderer.SetPropertyBlock(props); info = "您点击了图片按钮,shewei0"; Debug.Log("设置为0"); } //标签/ //GUI.Label(new Rect(280, 10, 200, 200), img0); //图片按钮,点击后显示图片/ //if (GUI.Button(new Rect(280, 250, 200, 200), img)) //{ // img0 = img; // props.SetFloat("_ColliderForce", 1); // myRenderer.SetPropertyBlock(props); // info = "您点击了图片按钮,shewei1"; //} //标签/ //GUI.Label(new Rect(500, 10, 200, 20), "持续按下的时间:" + frameTime); //连续按钮,点击后显示按下的时间/ if (GUI.Button(new Rect(380, 100, 200, 20), "lyg设为1")) { //frameTime++; props.SetFloat("_ColliderForce", 1); myRenderer.SetPropertyBlock(props); //info = "您点击了图片按钮,shewei1"; info = "您按下了连续按钮,shewei0"; Debug.Log("设置为1"); } //每当鼠标按下时将frameTime重置,一遍进行下次记录/ if (Input.GetMouseButtonDown(0)) { frameTime = 0; } } }
gui之button、label字体大小、颜色修改
参考:https://docs.unity3d.com/Manual/gui-Customization.html颜色修改
GUI.color = Color.red;
label字体
GUIStyle style = GUI.skin.GetStyle("label"); style.fontSize = 40; GUI.Label(new Rect(widthOffset + 100, 200, 600, 300), "now " + (Camera.main.allowMSAA ? "opened ................" : "not opened") + " MSAA"); GUI.Label(new Rect(widthOffset + 100, 300, 600, 300), "now " + (Camera.main.allowHDR ? "opened ................" : "not opened") + " HDR");
其他控件的字体
//toggle字体 GUIStyle style1 = GUI.skin.GetStyle("Toggle"); style1.fontSize = 40; m_toggle0 = GUI.Toggle(new Rect(widthOffset + 200, 400, 400, 200), Camera.main.allowMSAA, "Toggle MSAA !"); m_toggle1 = GUI.Toggle(new Rect(widthOffset + 250, 600, 400, 200), Camera.main.allowHDR, "Toggle HDR!"); //button字体 GUIStyle style1 = GUI.skin.GetStyle("Button"); style1.fontSize = 40;
toggle的使用
//成员变量 private bool m_toggle0 = false; private bool m_toggle1 = false; //OnGUI里 m_toggle0 = GUI.Toggle(new Rect(widthOffset + 200, 400, 400, 100), Camera.main.allowMSAA, "Toggle MSAA !"); m_toggle1 = GUI.Toggle(new Rect(widthOffset + 250, 600, 400, 100), Camera.main.allowHDR, "Toggle HDR!"); if (m_toggle0) { Camera.main.allowMSAA = true; QualitySettings.antiAliasing = 2; } else { Camera.main.allowMSAA = false; } if (m_toggle1) { Camera.main.allowHDR = true; } else { Camera.main.allowHDR = false; }
相关文章推荐
- 使用 NSPropertyListSerialization 持久化字典与数组
- 使用 NSPropertyListSerialization 持久化字典与数组
- 使用 NSPropertyListSerialization 持久化字典与数组
- 使用 NSPropertyListSerialization 持久化字典与数组
- Unity 使用备忘(四) 游戏物体不挂在脚本文件上直接实现Button(Script)脚本文件的Onclick方法
- 使用客户端脚本(RegisterClientScriptBlock ,RegisterStartupScript )
- 如何使用boost::property_tree表示JSON数组
- Unity Application Block-配置文件的使用-示例(附源码下载)
- 关于在.net1.1中使用RegisterClientScriptBlock()注册脚本的显示顺序的BUG
- 在ASP.NET 中使用 Unity Application Block – 示例(提供代码下载)
- 使用EL表达式分别获取 某个 web 域 中的对象,访问 javabean 的属性、访问 list 集合、访问 map 集合、访问数组
- 使用foreach,list来遍历数组
- Unity Application Block 1.0系列(5): 使用BuildUp让已存在对象实例也支持依赖注入
- Unity3.5的脚本编写(C#),请使用VS编写,然后用MonoDevelop调试!
- ogre 中material脚本的'texture_unit'部分使用的属性
- MAG中关于数组和COMBO,LIST的联合使用例子(MAGList和MAGInputList)
- MFC控件使用说明(combo box Button Check Box Radio Button List Control Edit Box Hot Key IP Address....)
- Unity Application Block-配置文件的使用-示例(附源码下载)
- Unity Application Block 1.0系列(3): 属性/设值方法注入(Property/Setter Injection)
- RegisterType和RegisterInstance的不同使用情况--Unity Application Block(1)