I have created a floor where by gazing on it the material changes for every 2 seconds. Now I wanted to create a material picker on the scene. Where the user can choose from the given 3 options of the materials and by clicking on it the selected material should apply to the floor. How to do it?
The source code for gazing floor to change 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;
}
}
Edited code:-
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 used this code to change the color of a cube if i pressed a button. But it's not working. I have added this code to cube and in button I have attached this script and function. If I pressed the button the color of cube does not change.
Edited again:- Now I can change the materials of the 3d model for eg:if it is a chair I am able to change the materials of chair by clicking on a button. How can I change the different models? eg:in chair there are different models if a user clicked on a button it should produce different models how to do it?
The simpliest way would be to create a function that takes an int index and in that function change Rend's material to materials[index], then create an UI canvas with three buttons, each of the buttons would trigger that function but pass different int.