- Home /
Change material with EditorScript
I try to change the material of a gameobject in the editor by a EditorScript. I use this code:
if(boolean == true)
{
var newMat = AssetDatabase.LoadAssetAtPath("Assets/blue.mat", typeof(Material));
if(newMat != null)
Debug.Log("Asset loaded");
else
Debug.Log("cant find asset");
target.renderer.material = newMat;
}
when i click the boolean in the EditorScript my debug log prints "Cant find asset".
I hate to ask this but can you verify that "blue.mat" exists within your Assets folder in the project.
yes it does exist. doesn't matter if i put it in the root or in a subfolder. it just doesn't load it.
Can I ask what you mean by "click the boolean" in your editor script?
I'll assume you're using editor scripts in a way that I haven't explored, but I'll ask silly questions just in case they help one of us...
I typically trigger my editor scripts from the menus using the @$$anonymous$$enuItem attribute. Does "click the boolean" mean you're using an EditorWindow or wizard of some kind? I haven't ever seen the properties of my editor scripts in the Inspector.
I guess it would help to see more of your code, too.
Also (I don't use C#) but can you really use the word 'boolean' as a variable name?
(or maybe that's just an example name)
it's a javascript script. the name boolean is a example because i have a dutch name there. The boolean is linked to a Toggle element and that works fine for me. The only thing is that i want is to alter the gameobjects material when i click that toggle or immediately when i add the script to it.
Answer by aymeric · Jul 22, 2011 at 08:04 AM
It works now! i used the code like this: Make sure you begin with "Assets" in the path.
@CustomEditor (scriptname)
class NewBehaviourScript extends Editor{
function OnInspectorGUI() { var thePath = "Assets/ExampleMaterial.mat"; var loadedMaterial = AssetDatabase.LoadAssetAtPath(thePath, Material);
Debug.Log("Just loaded this material: " + loadedMaterial.name);
target.renderer.material = loadedMaterial;
} }
Thanks Jahroy
Answer by jahroy · Jul 21, 2011 at 04:13 AM
Sorry, I assumed you were using C# because of the unfamiliar code I saw as the second parameter of your call to LoadAssetPath().
When I use LoadAssetAtPath() I use it like this:
var newMat = AssetDatabase.LoadAssetAtPath("Assets/blue.mat", Material);
I'm not sure if that's the problem or not. I would give it a try both ways. I don't have a machine with Unity handy, but I'm curious to see the output of:
Debug.Log(typeof(Material);
Debug.Log(AssetDatabase.GenerateUniqueAssetPath());
i removed the answer/comment and thanks!
I tried your code (down my comment) and it works in a new unity project. tomorrow (in 8 hours) i will test it at my other project. I will post here if it did work.
///////////////////////// (6 hours ago)jahroy Yes, that code works.
If I place the following script in a folder named Editor it works fine. To execute the code you need to choose "Load an Asset" from the menu at the top of the screen named "Unity Answers Test".
Of course you'll need to modify the path to the $$anonymous$$aterial asset.
@$$anonymous$$enuItem ( "Unity Answers Test/Load an Asset", false, 300 ) static function testLoadAsset () { var thePath = "Assets/Example$$anonymous$$aterial.mat"; var loaded$$anonymous$$aterial = AssetDatabase.LoadAssetAtPath(thePath, $$anonymous$$aterial);
Debug.Log("Just loaded this material: " + loaded$$anonymous$$aterial.name);
} ////////////////////////
Thanks again.
Answer by astracat111 · Feb 15, 2020 at 09:30 PM
Do you use?
Material SRm = (Material)AssetDatabase.LoadAssetAtPath("Assets/MyMaterial.mat", typeof(Material));