如何统一创建材料选择器选项?

时间:2017-05-31 10:01:03

标签: c# unity3d get unity5 color-picker

我已经创建了一个地板,每隔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;
    }
}

编辑代码: -

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

再次编辑: - 现在我可以改变3d模型的材质,例如:如果它是椅子,我可以通过点击按钮来改变椅子的材料。如何更改不同的型号?例如:在椅子上有不同的模型,如果用户点击按钮它应该生成不同的模型怎么做?

1 个答案:

答案 0 :(得分:2)

最简单的方法是创建一个带有int索引的函数,并在该函数中将Rend的材质更改为材质[index],然后创建一个带有三个按钮的UI画布,每个按钮都会触发该函数但会传递不同的中间体