• 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
1
Question by ozg · Jul 10, 2012 at 01:51 PM · meshcubetransparent

How to do a glass cube?

I have a struct for a cube like this:

 public struct Cube{
  public float x;
  public float y;
  public float z;
  
  public float w;
  public float h;
  public float d;
 
  public Cube(float pX, float pY, float pZ, float pWidth, float pHeight, float pDepth)
  {
      x = pX;
      y = pY;
      z = pZ;
  
      w = pWidth;
      h = pHeight;
      d = pDepth;
  }
    

How can i add a transparent surface to this cube?

Comment
Add comment · Show 4
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 Tim-Michels · Jul 10, 2012 at 02:21 PM 1
Share

Well, this code looks familiar ;)

Your question isn't really clear, because the struct itsself doesn't represent a mesh or any graphical.

What are you exactly tring to achieve?

avatar image Yoerick · Jul 10, 2012 at 02:40 PM 0
Share

Tim's right, this struct doesn't do anything graphical as there is nothing that can be rendered. You'll need something like an Object or GameObject and add a renderer to it to be able to actually see your cube. Then, to add transparency to it, you'll need to change it's material shader to one that has transparency.

avatar image ozg · Jul 11, 2012 at 06:55 AM 0
Share

Yes Tim it's yours:) Actually i used this struct for particles.They are moving only in this cube.Now i will try to change it to game object then:) thanks for help

avatar image ozg · Jul 11, 2012 at 07:22 AM 0
Share

I mean particles' movements are bounding with this cube struct's coordinates. As a result there is a cube container for particles.

1 Reply

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

Answer by Yoerick · Jul 10, 2012 at 02:34 PM

Is there a reason you approach the making of a cube like this? There is an easy function CreatePrimitive which can create a cube with a single line.

Just like this:

 GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);

You can then adjust it's dimensions by modifying cube.transform.localScale:

 cube.transform.localScale = new Vector3 (x, y, z);

Why would I go this way? Cause you'll need an Object or GameObject to be able to put a material component onto it. The material's shader can then be set to "Transparent/Diffuse" like so:

 Renderer r = cube.addComponent("Renderer");
 r.material = new Material(Shader.Find("Transparent/Diffuse"));

Finally, to adjust the material's transparency, just modify it's Color:

 r.material.Color = new Color(1,0,0,0.5f) //0.5f is alpha value -> this is a red cube with 50% transparency

As I'm not home I don't have the Unity engine to test this atm, but I'm fairly sure it is correct. If not, it should already put you in the right direction.

Hope it helps ;)

Comment
Add comment · Show 4 · 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 ozg · Jul 11, 2012 at 07:20 AM 0
Share

Actually i wonder if there is a way like add a transparent mesh from cube struct's x coordinate to y coordinate?Or do i must create a game object for it?

avatar image Yoerick · Jul 13, 2012 at 11:44 AM 0
Share

You could create the primitive (with the CreatePrimitve function I described), starting from your struct, using the struct's values to adjust the localScale (as described). But yes, you need a gameObject to be able to use it in your scene / render it ;)

avatar image Ingen · Jul 13, 2012 at 02:19 PM 0
Share

sorry for intrusion

but use script to create primitive, is better than use menu > new Gameobject / $$anonymous$$aterials and add script

avatar image Yoerick · Jul 13, 2012 at 02:52 PM 0
Share

Which is what I said? In the code I provided, the gameObject is created through the script.

I think you wanna say it's better to create a gameObject through the menu? If that's what you wanna say I kind of disagree with you. Yes, in a way it's easier but on the other hand, your scene gets larger, as does your build size when you publish the game. If you create an object through code, it's loaded at runtime and doesn't affect the size of your build since it doesn't exist at that time. Besides, his question was specifically code based so that's where I build my solution on.

However, you could argue about overhead that comes with creating the object through code but then it just comes down to what you prefer, a smaller build size with a $$anonymous$$imal extra overhead or a slightly larger build size with no additional overhead.

Creating objects in the scene or at runtime is really a matter of preference in my opinion, both methods have their up and down sides. To be honest, I use both methods.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

Need Help > Why my 3D modelling became transparent 1 Answer

Cube shows image same on all sides? 2 Answers

Script rendering Mesh to Image with transparent background 1 Answer

How do I make a grey cube transparent? 1 Answer

Adjusting strange Vector3.Normalize behaviour 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