错误是:
Trying to create a material from string - this is no longer supported.
UnityEngine.Material:.ctor(String)
Drawer:CreateLineMaterial() (at Assets/Flying Birds/Scripts/Drawer.cs:27)
Drawer:Awake() (at Assets/Flying Birds/Scripts/Drawer.cs:46)
第 27 行是:
var mat = new Material(
第 46 行:
lineMaterial = CreateLineMaterial();
using System;
using UnityEngine;
using System.Collections.Generic;
public class Drawer: MonoBehaviour
{
public Material lineMaterial;
struct Line
{
public Vector3 from;
public Vector3 to;
public Color color;
public Line( Vector3 from, Vector3 to, Color color )
{
this.from = from;
this.to = to;
this.color = color;
}
}
static List<Line> lines = new List<Line>();
static Material CreateLineMaterial()
{
var mat = new Material(
@"Shader ""Lines/Colored Blended"" {
SubShader { Pass {
Blend SrcAlpha OneMinusSrcAlpha
ZWrite Off Cull Off Fog { Mode Off }
BindChannels {
Bind ""vertex"", vertex Bind ""color"", color }
}}}"
);
mat.hideFlags = HideFlags.HideAndDontSave;
mat.shader.hideFlags = HideFlags.HideAndDontSave;
return mat;
}
void Awake()
{
if( lineMaterial == null )
lineMaterial = CreateLineMaterial();
}
void OnPostRender()
{
lineMaterial.SetPass( 0 );
GL.Begin( GL.LINES );
foreach( var l in lines )
{
GL.Color( l.color );
GL.Vertex3( l.from.x, l.from.y, l.from.z );
GL.Vertex3( l.to.x, l.to.y, l.to.z );
}
GL.End();
}
void FixedUpdate()
{
lines.Clear();
}
public static void DrawLine( Vector3 from, Vector3 to, Color color )
{
lines.Add( new Line(from, to, color) );
}
public static void DrawRay( Vector3 from, Vector3 to, Color color )
{
lines.Add( new Line(from, from + to, color) );
}
}
最佳答案
Material 字符串构造函数现已过时。您可以使用着色器或 Material 构造函数。
public Material(Material source);
public Material(Shader shader);
将着色器代码放入着色器文件中,然后使用 Shader.Find
找到它。
var mat = new Material(Shader.Find("Transparent/Diffuse"));
关于c# - 尝试从字符串创建 Material - 这不再受支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39709867/