4

The error is:

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)

Line 27 is:

var mat = new Material(

And line 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) );
  }
}

4 Answers 4

6

The Material string constructor is now obsolete. You can use the Shader or Material constructor.

public Material(Material source);
public Material(Shader shader);

Put the shader code in a shader file the use Shader.Find to find it.

var mat = new Material(Shader.Find("Transparent/Diffuse"));
1

I believe there's no way to create a Material from shader strings in newer versions of Unity (which is arguably good thing).

You will need to create a Shader asset and put your code from CreateLineMaterial there. Then you can write something like:

    var mat = new Material(Shader.Find("Lines/Colored Blended"));

instead in the Material constructor.

1

This is a step by step solution for those that are not familiar with shaders.

1) Create a Shader File

To create a new Shader, use Assets > Create > Shader from the main menu or the Project View context menu. A shader is a text file similar to a C# script, and is written in a combination of Cg/HLSL and ShaderLab languages

2) Copy/Paste this Shader Code

The filename of the shader does not matter. Go ahead and copy/paste this shader code into your shader. (Delete any old code that existed when the shader file was created)

Shader "Lines/Colored Blended" {
            SubShader { Pass {
                Blend SrcAlpha OneMinusSrcAlpha
                ZWrite Off Cull Off Fog { Mode Off }
                BindChannels {
                  Bind "vertex", vertex Bind "color", color }
            } } }

3) Find the Shader in your C# code

Now you can write your function, how you like (using the Shader.Find() function):

static Material CreateLineMaterial()
{
    mat = new Material(Shader.Find("Lines/Colored Blended"));

    mat.hideFlags = HideFlags.HideAndDontSave;
    mat.shader.hideFlags = HideFlags.HideAndDontSave;

    return mat;
}

That's it! Your code should work now.

0

Creating materials from shader source string is no longer supported. Use Shader assets instead. https://docs.unity3d.com/Manual/class-Shader.html

Source: Unity Docs https://docs.unity3d.com/ScriptReference/Material-ctor.html

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.