HTC Vive VR房产项目开发五(通过材质列表切换地板材质)
来源:互联网 发布:keepalive mysql 主 编辑:程序博客网 时间:2024/04/28 18:55
一、搭建界面
1、在地板Floor1下创建一个canvas,重命名为FloorMatList,并添加脚本VRTK_UI Canvas,用于与UI进行交互
2、创建一个Panel,并且重命名为MatUIContainer,添加组件Grid Layout Group,设置内容距离上下左右的间距Padding,设置layout中内容的大小Cell Size,设置内容间的间距Spacing,设置内容为固定行数Constraint,并设置行数Constraint Count
3、在FloorMatList下创建一个Button并且重命名为ShowMatButton,设置大小和位置,设置source image并设置Highlighted Color
将text的内容置为空
4、创建一个Button,设置大小和位置,并设置Highlighted Color,将其拖动到Project视图做成Prefeb
二、创建脚本
1、
FloorMatList的脚本
using System.Collections;using System.Collections.Generic;using UnityEngine;using DG.Tweening;using UnityEngine.UI;[System.Serializable]public class FloorMatList : MonoBehaviour { public GameObject floor; //地板 private bool isOpen = false; //列表是否打开 public Transform matUIContainer; //材质列表容器 public FloorMatData[] matDataList; //材质列表 public GameObject matButtonPrefeb; //材质按钮原型 private int curMatIndex = 0;void Start () { init();}void Update () {} private void init() { matUIContainer.localScale = new Vector3(0,1,1); //初始化材质列表容器 foreach (FloorMatData data in matDataList) { GameObject matButton = Instantiate(matButtonPrefeb); //动态生成承装材质的button matButton.transform.parent = matUIContainer; //将button作为matUIContainer的子物体,放置在容器里面 RectTransform rect = matButton.GetComponent<RectTransform>(); rect.localPosition = Vector3.zero; //重置位置 rect.localRotation = Quaternion.Euler(Vector3.zero); //重置选择 rect.localScale = Vector3.one; //重置缩放值 matButton.GetComponent<Image>().sprite = data.matThumb; //设置按钮的缩略图 matButton.GetComponent<Button>().onClick.AddListener(delegate() { //设置按钮点击事件 onMatButtonClick(matButton); }); } } private void onMatButtonClick(GameObject button) { int i = 0; //所有动态生成的按钮是放在UIContainer中的,遍历所有的元素,找到传递过来的按钮进行设置 foreach (Transform child in matUIContainer) { if(button.transform == child && curMatIndex != i) { floor.GetComponent<Renderer>().material = matDataList[i].floorMat; //更改指定元素的材质 curMatIndex = i; } i++; } } public void showMatList() { Debug.LogError("showMatList........................"); //如果打开 if (isOpen) { //关闭提示框 matUIContainer.DOScale(new Vector3(0,1,3),0.3f); }//如果关闭 else { //打开提示框 matUIContainer.DOScale(Vector3.one, 0.3f); } //重置标记位 isOpen = !isOpen; }}
FloorMatData的脚本
using System.Collections;using System.Collections.Generic;using UnityEngine;[System.Serializable]public class FloorMatData { public Material floorMat; //地板材质 public Sprite matThumb; //材质缩略图}
2、将脚本FloorMatList挂载到物体FloorMatList之上,并将MatUIContainer赋值给脚本的变量MatUIContainer及其它变量的值
设置集合元素的个数并给其元素赋值
3、给按钮添加点击事件,点击On Click的“+”,将FloorMatList对象拖动过来,并且选择FloorMatList中的方法showMatList
三、运行后
初始化时默认的场景
按下touch pad键将指针指向button,如果容器没有展开,按下trigger键,容器展开
当选择对应的材质的button地板切换成对应的材质后
如果容器展开,点击按钮容器关闭
注:1、参考资料:http://edu.manew.com/course/344/learn#lesson/5755
2、材质列表导入进来后不能直接使用,因为使用了The lab renderer,需要修改shader为Valve/vr_standard和Tiling值
- HTC Vive VR房产项目开发五(通过材质列表切换地板材质)
- HTC Vive VR房产项目开发四(切换家具样式)
- HTC Vive VR房产项目开发二(开关门)
- HTC Vive VR房产项目开发一(手柄提示+户型图)
- HTC Vive VR房产项目开发三(添加物体提示)
- HTC Vive 扔球换材质的效果
- VR开发--(HTC Vive)设备一览
- HTC VIVE开发教程(五)
- htc vive And Unity 开发VR
- HTC Vive VR Unity 开发入门
- 基于htc vive设备的UE4 VR项目开发需要修改源码的地方
- Unity集成PerceptionNeuron(动捕)与htc vive VR开发
- 【VR开发】htc vive 和 oculus 初体验
- 【VR开发】htc vive+unity 3D 简单保龄球游戏
- Unity VR HTC VIVE 初识
- **Unity--Htc VIVE VR 引言**
- 材质
- 材质
- 关于C++二分查找
- 单例模式
- golang channel 学习
- python 函数式编程
- 微信小程序跳坑(一)---Post请求
- HTC Vive VR房产项目开发五(通过材质列表切换地板材质)
- SVM算法原理
- 第五章:格式
- JavaScript基本知识
- Javaweb从新建项目开始
- 【安卓基础】04 广播机制实现强制下线
- 哈希函数
- gsoap C++工程搭建
- 微信企业号加密机制