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
0
Question by Ghartard · Jan 11, 2016 at 10:05 AM · texturesobjectsreferences

Is it possible to find all objects that reference a specific texture during run time?

I have a project with multiple objects using the same texture. For example purposes let say "Big_Car_Red" is the texture name. In a C# script, I want to find all the objects that reference that specific texture. The reason I need this is to change all the objects that use that texture to another similar texture, like "Big_Car_Blue." Is there a way I can find all the objects that referenced "Big_Car_Red" during run time? I'd prefer to do this without looping through every object in the scene.

Comment
Add comment · Show 3
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 saschandroid · Jan 11, 2016 at 11:47 AM 0
Share

If the objects all have the same material on them you could change the textures by something like

 $$anonymous$$aterial m_material = oneOfYourCars.GetComponent().shared$$anonymous$$aterial;
 m_material.mainTexture = newTextureForAllCars;




avatar image Ghartard saschandroid · Jan 12, 2016 at 10:23 PM 0
Share

I get that, but I think you missed my question. I'm looking for all Objects my current texture is being used by, so I can change them all to my new texture.

avatar image saschandroid Ghartard · Jan 13, 2016 at 06:25 AM 0
Share

Changing the shared material does this: it changes the texture of all objects which have this material at once.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by UsmanAbbasi · Jan 11, 2016 at 01:21 PM

Find all objects in scene which have "Image" component and then iterate all of them to check which objects have image named "Big_Car_Red" and replace it with "Big_Car_Blue".

 Image[] textures = FindObjectsOfType(typeof(Image)) as Image[];
 for( int i = 0 ; i < textures.Length ; i++)
 {
     if(textures[i].sprite.name == "Big_Car_Red")
     {
         textures[i].sprite =  Resources.Load<Sprite>("Big_Car_Blue");
     }
 }

P.S: Make sure to include "using UnityEngine.UI" in file where you write this code as "Image" class is placed in "UnityEngine.UI" namespace. Also place image named "Big_Car_Blue" in "Resources" folder and set its import type to "Sprite".

Comment
Add comment · Show 2 · 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 Ghartard · Jan 11, 2016 at 07:15 PM 0
Share

This is close to what I looking for. I was looking for something similar to AssetDatabase.GetDependencies(...) to call during run time. The problem with your answer, It's a big game. Looping through all objects that have an "Image" component may take too much time.

avatar image UsmanAbbasi · Jan 12, 2016 at 06:32 AM 0
Share

Yes if your scene is big then it will take time. I am not sure about AssetDatabase.GetDependencies() thing. Never used it.

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

Ref to Texture Missing when Creating Sprite by Script during Asset Postprocessing 1 Answer

Unity3D: objeec 1 Answer

Unity Trees Material/Texture changed 0 Answers

Changing texture on prefab at runtime (c#) 1 Answer

Lightmaps/objects and textures 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