Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Resolved Editor Script Freeze on CreateAsset with a new Material

Discussion in 'Scripting' started by cnlohr, Jun 4, 2021.

  1. cnlohr

    cnlohr

    Joined:
    Nov 25, 2020
    Posts:
    10
    I am having a bear of a time with Unity tonight. I'm up to over two hours and I still can't figure out how to save an material as an asset in an editor script that I have attached to an object, called on OnValidate(). If I create a material and use it, then save it, the editor freezes. All the time. Every time. If I break the script and open the project back up, the saved asset says "This is the default asset. Use assets-> Reimport...."
    Code (CSharp):
    1.  
    2. Material UseMaterial = new Material(Shader.Find("AudioLinkSandbox/ApplySmoothText"));
    3. GetComponent<MeshRenderer>().sharedMaterial = UseMaterial;
    4. AssetDatabase.CreateAsset(UseMaterial, $"Assets/Compiled/cmat_{gid}.asset");
    5.  

    I've tried:
    (1) Checking to see if I can load the asset: No. Opening the asset as material returns null.
    (2) Seeing if I can search for the asset: Yes. Asset appears in AssetDatabase.FindAssets
    (3) Tried guarding it with AssetDatabase.StopAssetEditing(); and AssetDatabase.StartAssetEditing();: No effect. Among many other things. And I'm out of ideas. Not exactly sure where to post about questions that are editor-script-related. P.S. This is called from within OnValidate()

    (This is 2018.4.20f1)
     
  2. cnlohr

    cnlohr

    Joined:
    Nov 25, 2020
    Posts:
    10
    This is even stranger. I start a brand new project. I create this script. I apply it to a cube. Unity Freezes.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor;
    5.  
    6.  
    7. public class test : MonoBehaviour
    8. {
    9.     public int i;
    10.    
    11.     void Start()
    12.     {
    13.        
    14.     }
    15.    
    16.     public void OnValidate()
    17.     {
    18.         Material UseMaterial = new Material(Shader.Find("Standard"));
    19.         UseMaterial.name = "cmat_HelloWorld";
    20.         AssetDatabase.CreateAsset(UseMaterial, $"Assets/HelloWorld.mat");
    21.     }
    22. }
    23.  
     
  3. cnlohr

    cnlohr

    Joined:
    Nov 25, 2020
    Posts:
    10
    Solution found: Never, ever, save a material from inside OnValidate. I had to add [ExecuteInEditMode], then set a flag and did it in Update() all is well.
     
    paleblueit and Lundralion like this.