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() as Renderer).material = mat;

     

你可能感兴趣的:(Unity开发)