4. 双加步骤三中创建的脚本文件,使用VS编辑器打开或者使用默认的编辑器打开,编写代码如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestMaterial : MonoBehaviour {
public Material sphereMat;
private Material cubeMat;//使用代码动态创建
public GameObject sphere;
public GameObject cube;
// Use this for initialization
void Start () {
cubeMat = new Material(Shader.Find("Custom/DiffuseColor1"));
cubeMat.color = new Color(255, 228, 0, 1);
sphere.GetComponent().material = sphereMat;
cube.GetComponent().material = cubeMat;
}
// Update is called once per frame
void Update () {
}
}