Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by dozhwal · Aug 20, 2014 at 12:33 PM · spritematerialduplicate

Duplicate a material used only once easily

hi,

I have a "sprite" prefab with a simple double triangle (its not the unity thing, its a 3dsmax fbx).

i make levels where a "sprite" is often used only one time (background things). I want to place it in the scene and change the material (texture offset from one atlas).

i find boring to duplicate the material in the asset manager, apply it to the new "sprite" every time.

i made a gui thing that do :

  if(GUILayout.Button("Duplicate Mat")) {
  
      Selection.activeGameObject.renderer.material =
      new Material(Selection.activeGameObject.renderer.material);
  
  }

So yes, it make a new material "materialXX (instance)" in the scene but not in the asset manager. so unity cry saying there is a leaked material, but is it a big problem?

one solution i thought is to make a script for the sprite with a material variable that it is applied at the start of the game. but i can't see it in the scene view so it is difficult to make levels.

Another one is a gui script that duplicate material in the asset manager, rename it, and assign it to the selection. duplicate and rename was ok but i dont know why, script dont want to assign it to the selection (script is below). it seemed an easy and fun solution.

do you have a better solution?

 newmatname = EditorGUILayout.TextField(newmatname);
                 if(GUILayout.Button("Duplicate Material")) {
                     
 //Take the path, add the new name, add .mat    

             newpath=AssetDatabase.GetAssetPath(Selection.activeGameObject.renderer.sharedMaterial).Substring(0,AssetDatabase.GetAssetPath(Selection.activeGameObject.renderer.sharedMaterial).LastIndexOf("/"))   +"/"+newmatname+".mat";
 // Duplicate with the new name    


     AssetDatabase.CopyAsset(AssetDatabase.GetAssetPath(Selection.activeGameObject.renderer.sharedMaterial), newpath);
                     
 //Try to assign the new material asset to my fuck**g Selection but dont work. asset path was good though. but material assign give a null material


 Selection.activeGameObject.renderer.sharedMaterial=AssetDatabase.LoadAssetAtPath(newpath, typeof(Material));
                     //other try :
                     //mat = EditorGUIUtility.Load(newpath, (typeof(Material))) as Material;
                     
                 }









Comment
Add comment
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

1 Reply

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

Answer by dozhwal · Aug 21, 2014 at 11:43 AM

Ok, i reply myself for the second question i resolved.

I forgot to set an Refresh() to the asset database! So the material is duplicate and assigned to the selection without problem.

it's resolved but if someone can tell me if leaked material is a real problem or not, i would thank him.

 if(GUILayout.Button("Duplicate Material")) {
                     
                     newpath=AssetDatabase.GetAssetPath(Selection.activeGameObject.renderer.sharedMaterial).Substring(0,AssetDatabase.GetAssetPath(Selection.activeGameObject.renderer.sharedMaterial).LastIndexOf("/"))   +"/"+newmatname+".mat";
                     AssetDatabase.CopyAsset(AssetDatabase.GetAssetPath(Selection.activeGameObject.renderer.sharedMaterial), newpath);
                     AssetDatabase.Refresh();
                         Selection.activeGameObject.renderer.sharedMaterial = AssetDatabase.LoadAssetAtPath(newpath, typeof(Material)) as 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

2 People are following this question.

avatar image avatar image

Related Questions

How can you duplicat folders and not share old dependencies? 1 Answer

Pixel snap for material made with Unlit Shader Graph? 1 Answer

Changing button texture by setting a texture offset of a sprite sheet?(SOLVED) 1 Answer

Duplicating Gameobject and changing it without affecting original Gameobject 1 Answer

Sprite Renderer Missing Material after running 1 Answer


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