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 unity_oEVKDotJO50MrQ · Jul 18, 2018 at 08:29 AM · objectsopacity

Unity - Change opacity of objects around selected one

I would like to change the opacity of the objects that are "around" another object that i have selected by script ( GameObject.Find("MySelectedObject")) to have a "better visibility" of the selected one.

For example i could have a model of a car, i could have selected the engine and i would like to see it even if the hood is closed.

There is a way to achieve this goal? Can you give me some hints or point me to some functions or snippets?

Thanks for your time and , hopefully, answers;)

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

Answer by JVene · Jul 18, 2018 at 06:22 PM

This is a change to the materials on those objects that should become translucent.

I'd start by creating "duplicate" materials of those in use, adjust the duplicates so they look translucent (which will require changing the rendering mode to transparent, then set the alpha channel of the material's color until it looks about right in an example.

If you have only a few materials you can prepare these in the editor and name them appropriately, so they are recognizable by name in code.

If you have lots of materials it may be better to initialize your code by reading through the materials of the model, create duplicate materials in code, and adjust them in code to be translucent versions based on the experiment I proposed above on one or two materials.

Then, at the moment you want objects to become translucent, you switch the material on that GameObject from the existing one to the translucent version (then back to the original when this state is ended).

To figure out how to do all of this, which is a bit more than a post should hold, start by reading the docs on MeshRenderer. There are example snippets regarding the material member. Note that if your meshes have multiple materials you'll need to use the materials member (plural) to get a list of all of them.

That last point is a per-object issue. Some objects may have only 1 material, while some might have multiple materials. Therefore, the script attached to the objects should perform this work, as the script attached to the object would adapt to the needs of each object.

Further, since this is a common behavior to multiple objects it would be best to consider creating a class that does this work and use it for all objects. How you implement this is up to you, but there are two basic approaches.

One is to "intercept" the classes Unity hands to you when you create or attach a script to an object. This means Unity hands you a class derived from MonoBehaviour. You will have created a class that is derived from MonoBehaviour with the code that manipulates and initializes translucent materials. You then edit the script Unity created for you, and change its base from MonoBehaviour to your "translucent material manager" class. The result is a class exactly like you had before, but the common behaviors that manage materials will be inherited by all classes you edit this way. Now you write code once for all objects that do this.

Another approach is to create a stand alone class that has the code for managing translucent materials, make a member of your GameObject that instantiates that for each GameObject, initialize this "translucent handler" so it "knows" the object it is attached to (the member will need some references to the meshes or other artwork that has materials to be changed), and have it do the work. Repeat for all objects. This is something like attaching a component to a GameObject.

To learn how to find material already in your project, look into Resources. You'll use Resources.Load to get a material from the resources, which presumably would be a translucent version, for use in switching materials on an object.

Also, you can study Material, a class you can use to create materials in code. You can read the material already on the GameObject (from Renderer, probably MeshRenderer if your model is a mesh), then use code to fashion translucent versions.

You'll also need to consider containers, like List, maybe Dictionary to help store these materials.

Your design might also create a stand alone "executive" class that coordinates all of this (something that is told what is the selected object, which ones should become translucent, and then how to undo all of this when it's over).

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

87 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Empty Project & Scene, showing up tons of loaded stuff in Unity 4.3.3f1 and not in 4.3.1f1? 0 Answers

How can i connect two objects to an animation? 2 Answers

Collision in between 2 objects moving away from each other? 1 Answer

Build just what's in the scene? 1 Answer

Problem with passing object to another script. 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