Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by aymeric · Jul 19, 2011 at 10:51 AM · materialeditor-scriptingassetdatabaseloadassetatpath

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".

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dbolt · Jul 19, 2011 at 09:00 PM 0
Share

I hate to ask this but can you verify that "blue.mat" exists within your Assets folder in the project.

avatar image aymeric · Jul 19, 2011 at 11:50 PM 0
Share

yes it does exist. doesn't matter if i put it in the root or in a subfolder. it just doesn't load it.

avatar image jahroy · Jul 20, 2011 at 05:57 AM 0
Share

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.

avatar image jahroy · Jul 20, 2011 at 06:02 AM 0
Share

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)

avatar image aymeric · Jul 20, 2011 at 12:15 PM 0
Share

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.

Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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());
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image aymeric · Jul 21, 2011 at 10:45 PM 0
Share

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.

avatar image
0

Answer by astracat111 · Feb 15, 2020 at 09:30 PM

Do you use?

 Material SRm = (Material)AssetDatabase.LoadAssetAtPath("Assets/MyMaterial.mat", typeof(Material));
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Could not load copied asset via AssetDatabase.LoadAssetAtPath 4 Answers

Load an asset right as it is imported? 0 Answers

Assign sharedMaterials from assets with editor script. 0 Answers

Checking Object Material against list of materials 2 Answers

How to identify an asset? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges