如何统一创建 material 选择器选项?
How to create a material picker option in unity?
我创建了一个地板,通过注视它,material 每 2 秒变化一次。 现在我想在场景中创建一个 material 选择器。 用户可以从给定的 material 和 3 个选项中进行选择单击它,选定的 material 应应用于地板。怎么做到的?
gazing floor源码改material:-
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//Make sure to change the class name (CCSphere) to whatever you called your
//script.
public class tochangematerial : MonoBehaviour
{
public float gazeTime = 2f;
private float timer;
private bool gazedAt;
public Material[] materials;//Allows input of material colors in a set size of array;
public Renderer Rend; //What are we rendering? Input object(Sphere,Cylinder,...) to render.
private int index = 1;//Initialize at 1, otherwise you have to press the ball twice to change colors at first.
// Use this for initialization
void Start()
{
Rend = GetComponent<Renderer>();//Gives functionality for the renderer
Rend.enabled = true;//Makes the rendered 3d object visable if enabled;
}
void Update()
{
if (gazedAt)
{
timer += Time.deltaTime;
if (timer >= gazeTime)
{
if (materials.Length == 0)//If there are no materials nothing happens.
return;
index += 1;//When mouse is pressed down we increment up to the next index location
if (index == materials.Length + 1)//When it reaches the end of the materials it starts over.
index = 1;
print(index);//used for debugging
Rend.sharedMaterial = materials[index - 1]; //This sets the material color values inside the index
timer = 0f;
}
}
}
public void pointerenter()
{
//Debug.Log("pointer enter");
gazedAt = true;
}
public void pointerexit()
{
//Debug.Log("pointer exit");
gazedAt = false;
}
}
编辑代码:-
using UnityEngine;
using System.Collections;
// Change renderer's material each changeInterval
// seconds from the material array defined in the inspector.
public class color2 : MonoBehaviour
{
public Material[] materials;
public float changeInterval = 0.33F;
public Renderer rend;
void Start()
{
rend = GetComponent<Renderer>();
rend.enabled = true;
}
public void Update()
{
if (materials.Length == 0)
return;
// we want this material index now
int index = Mathf.FloorToInt(Time.time / changeInterval);
// take a modulo with materials count so that animation repeats
index = index % materials.Length;
// assign it to the renderer
rend.sharedMaterial = materials[index];
}
}
我使用此代码在按下按钮时更改立方体的颜色。但它不起作用。我已将此代码添加到多维数据集,并在按钮中附加了此脚本和函数。如果我按下按钮,立方体的颜色不会改变。
I have added this script to cube and then in button I have dragged and dropped the gameobject(cube) and triggered the function void update()
再次编辑:-
现在我可以更改 eg:if 的 3d 模型的 materials 它是一把椅子 我可以通过单击按钮更改椅子的 materials。我怎样才能改变不同的模型? eg:in 椅子有不同的型号如果用户点击一个按钮应该产生不同的型号怎么办?
最简单的方法是创建一个采用 int 索引的函数,并在该函数中将 Rend 的 material 更改为 materials[index],然后创建一个 UI canvas 使用三个按钮,每个按钮都会触发该功能但传递不同的 int。
我创建了一个地板,通过注视它,material 每 2 秒变化一次。 现在我想在场景中创建一个 material 选择器。 用户可以从给定的 material 和 3 个选项中进行选择单击它,选定的 material 应应用于地板。怎么做到的?
gazing floor源码改material:-
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//Make sure to change the class name (CCSphere) to whatever you called your
//script.
public class tochangematerial : MonoBehaviour
{
public float gazeTime = 2f;
private float timer;
private bool gazedAt;
public Material[] materials;//Allows input of material colors in a set size of array;
public Renderer Rend; //What are we rendering? Input object(Sphere,Cylinder,...) to render.
private int index = 1;//Initialize at 1, otherwise you have to press the ball twice to change colors at first.
// Use this for initialization
void Start()
{
Rend = GetComponent<Renderer>();//Gives functionality for the renderer
Rend.enabled = true;//Makes the rendered 3d object visable if enabled;
}
void Update()
{
if (gazedAt)
{
timer += Time.deltaTime;
if (timer >= gazeTime)
{
if (materials.Length == 0)//If there are no materials nothing happens.
return;
index += 1;//When mouse is pressed down we increment up to the next index location
if (index == materials.Length + 1)//When it reaches the end of the materials it starts over.
index = 1;
print(index);//used for debugging
Rend.sharedMaterial = materials[index - 1]; //This sets the material color values inside the index
timer = 0f;
}
}
}
public void pointerenter()
{
//Debug.Log("pointer enter");
gazedAt = true;
}
public void pointerexit()
{
//Debug.Log("pointer exit");
gazedAt = false;
}
}
编辑代码:-
using UnityEngine;
using System.Collections;
// Change renderer's material each changeInterval
// seconds from the material array defined in the inspector.
public class color2 : MonoBehaviour
{
public Material[] materials;
public float changeInterval = 0.33F;
public Renderer rend;
void Start()
{
rend = GetComponent<Renderer>();
rend.enabled = true;
}
public void Update()
{
if (materials.Length == 0)
return;
// we want this material index now
int index = Mathf.FloorToInt(Time.time / changeInterval);
// take a modulo with materials count so that animation repeats
index = index % materials.Length;
// assign it to the renderer
rend.sharedMaterial = materials[index];
}
}
我使用此代码在按下按钮时更改立方体的颜色。但它不起作用。我已将此代码添加到多维数据集,并在按钮中附加了此脚本和函数。如果我按下按钮,立方体的颜色不会改变。
I have added this script to cube and then in button I have dragged and dropped the gameobject(cube) and triggered the function void update()
再次编辑:- 现在我可以更改 eg:if 的 3d 模型的 materials 它是一把椅子 我可以通过单击按钮更改椅子的 materials。我怎样才能改变不同的模型? eg:in 椅子有不同的型号如果用户点击一个按钮应该产生不同的型号怎么办?
最简单的方法是创建一个采用 int 索引的函数,并在该函数中将 Rend 的 material 更改为 materials[index],然后创建一个 UI canvas 使用三个按钮,每个按钮都会触发该功能但传递不同的 int。