Unity - 给游戏对象添加材质

3

我正在尝试创建一个脚本,连续生成物体并将一个脚本附加到所有生成的物体上。我希望在3秒后附加的脚本可以更改物体上的材料并添加一个盒子碰撞器。我的问题在于材料。由于物体是随机生成的,我不知道如何设置材料变量。这是我的Spawner:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Spawner : MonoBehaviour {
    //Variables
    public float x_max;
    public float x_min;
    public float y_max;
    public float y_min;
    private float Size;
    public float s_max;
    public float s_min;
    public float w_max;
    public float w_min;
    private float ProceduralCacheSize;
    private GameObject sphere;
    private float waitTime = 5f;
    private int fix;

    // Use this for initialization
    void Start () {
        Spawn ();
        Spawn ();
        Spawn ();
        StartCoroutine("MyCoroutine");
    }

    // Update is called once per frame
    void Update () {

    }
    void Spawn(){
            Size = Random.Range (s_min, s_max);
            sphere = GameObject.CreatePrimitive (PrimitiveType.Sphere);
            sphere.transform.position = new Vector3 (Random.Range (x_min, x_max), Random.Range (y_min, y_max), 0);
            sphere.transform.localScale = new Vector3 (Size, Size, Size);
            var fbs = sphere.AddComponent<Astroid> ();
        }

    IEnumerator MyCoroutine(){
        StartCoroutine("Change");
        waitTime = Random.Range (w_min, w_max);
        yield return new WaitForSeconds(waitTime);
        StartCoroutine("MyCoroutine");
        StartCoroutine("Change");
        Spawn ();
    }
}

这是附加的脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Astroid : MonoBehaviour {
    //I need to be able to set this variable to a material
    public Material mat;
    // Use this for initialization
    void Start () {
        this.GetComponent<SphereCollider>().enabled = false;
        StartCoroutine("Change");
    }
    // Update is called once per frame
    void Update () {

    }
    IEnumerator Change(){
        yield return new WaitForSeconds (3);
        this.GetComponent<SphereCollider>().enabled = true;
        this.GetComponent<Renderer> ().material = mat;
    }
}
2个回答

1
你可以这样创建一个公共的静态整型。 你的生成器:

public Material mat;
public static Material mat2;
void Start(){
mat2 = mat;
}

并且在你的小行星脚本中:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Astroid : MonoBehaviour {
 //I need to be able to set this variable to a material
 //public Material mat;
 // Use this for initialization
 void Start () {
  this.GetComponent<SphereCollider>().enabled = false;
  StartCoroutine("Change");
 }
 // Update is called once per frame
 void Update () {
  
 }
 IEnumerator Change(){
  yield return new WaitForSeconds (3);
  this.GetComponent<SphereCollider>().enabled = true;
  this.GetComponent<Renderer> ().material = Spawner.mat2;
 }
}


0
你可以直接从资源中加载它:
mat = Resources.Load("myMaterial.mat", typeof(Material)) as Material;

这个答案是不正确的。根据Unity文档,必须省略扩展名。 - Dávid Florek

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接