您的位置:首页 > 其它

HTC Vive VR房产项目开发五(通过材质列表切换地板材质)

2017-08-01 01:18 651 查看
一、搭建界面

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值

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: