2017-05-31 34 views
-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; 
    } 
} 

编辑的代码: -

​​

我用这个代码来更改立方体的颜色,如果我压一个按钮。但它不起作用。我已经将此代码添加到多维数据集,并在按钮中附加了此脚本和函数。如果我按下按钮,立方体的颜色不会改变。

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()

编辑再次: - 现在我可以改变3D模型的材料如:如果它是一张椅子,我能够通过点击按钮来改变椅子的材料。我怎样才能改变不同的模型?例如:在椅子上有不同的模型,如果用户点击一个按钮,它应该产生不同的模型如何做到这一点?

回答

2

的simpliest方式是创建一个函数,它的int索引和在那个函数变化撕裂的材料以材料[索引],然后创建具有三个按钮的UI帆布,每个按钮将触发该功能,但通不同的int。

+0

PLZ做检查上述编辑 –

+0

不能添加更新()函数来你的onClick监听器。你需要创建一个新的PUBLIC函数(即public void ChangeColor(int id)),然后用该脚本拖动游戏对象到你的按钮,选择函数并传递id。 – Fiffe

+0

thanku!这么多 –