-1
我創造了一個地板,通過凝視它每2秒的材料變化。 現在我想在場景中創建材質選擇器。如果用戶可以從材料的給定3個選項中進行選擇並點擊它,則所選材料應該適用於地板。怎麼做?如何在統一中創建材料選擇器選項?
用於凝視地板改變材料的源代碼: -
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;
}
}
編輯的代碼: -
我用這個代碼來更改立方體的顏色,如果我壓一個按鈕。但它不起作用。我已經將此代碼添加到多維數據集,並在按鈕中附加了此腳本和函數。如果我按下按鈕,立方體的顏色不會改變。
編輯再次: - 現在我可以改變3D模型的材料如:如果它是一張椅子,我能夠通過點擊按鈕來改變椅子的材料。我怎樣才能改變不同的模型?例如:在椅子上有不同的模型,如果用戶點擊一個按鈕,它應該產生不同的模型如何做到這一點?
PLZ做檢查上述編輯 –
不能添加更新()函數來你的onClick監聽器。你需要創建一個新的PUBLIC函數(即public void ChangeColor(int id)),然後用該腳本拖動遊戲對象到你的按鈕,選擇函數並傳遞id。 – Fiffe
thanku!這麼多 –