1. 程式人生 > >Unity動態建立帶有Shader的Material

Unity動態建立帶有Shader的Material

Unity5.x不在允許通過string的格式給材質新增shader,必須通過shaderAssets,否則警告:

UnityEngine.Material.Material(string) is obsolete:'Creating materials from shader source string is no longer supported. Use Shader assets instead.'

        具體操作方法:

1、新增一個Shader,放入工程中:

Shader "UI/Default Grey" {  
 Properties {  
  _MainTex ("Base (RGB)", 2D) = "white" {}  
  _Specular ("Specular", Range(1.0, 500.0)) = 250.0  
 }  
...
      2、建立一個材質
Material mat = new Material(Shader.Find("UI/Default Grey"));
(img.GetComponent<Renderer>() as Renderer).material = mat;