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 /
  • Help Room /
avatar image
2
Question by JeffreyVentrella · Jun 16, 2016 at 09:25 AM · c#texturerenderingscript.quad

I need a Simple and Clear example for how to add a texture (image) to a quad in C#

Every answer to this question that I have found is different. Seems like it should have a straightforward answer. In simple terms... My question is:

I have created a quad entirely in a C# script. I have a .png file in my Resources folder, and I need the code to load it as a texture and show up on the quad. That's it!

What's the simplest way to do this? (with a code example) (no deprecated functions please :)

Thanks! -j

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 saud_ahmed020 · Jun 16, 2016 at 10:20 AM 0
Share

@JeffreyVentrella I changed the color just for testing.

 void Start()
 {
     Texture t = Resources.Load<Texture>("SpriteName") as Texture;
     GetComponent<$$anonymous$$eshRenderer>().material.SetTexture("_$$anonymous$$ainTex",t);
     GetComponent<$$anonymous$$eshRenderer>().material.color = new Color(1,0,0,1);
 }
avatar image JeffreyVentrella saud_ahmed020 · Jun 20, 2016 at 02:17 AM 0
Share

Thanks, but your example makes no reference to any particular gameObject. I created a quad using...

quad = GameObject.CreatePrimitive( PrimitiveType.Quad );

$$anonymous$$y question is: how do I attach the texture to THAT object? And do I need to add a renderer, material, etc. to that gameObject?

By the way, this is all happening inside of a script called $$anonymous$$ain.cs which I am using to create all my gameObjects. I need to be explicit about what gameObjects I am adding textures to.

Thanks! -j

avatar image Eudaimonium · Jun 17, 2016 at 12:23 PM 1
Share

Just a quick "make sure" - have you assigned valid UV coords to the quad vertices when you generate them?

avatar image JeffreyVentrella · Jun 21, 2016 at 09:23 PM 0
Share

Thanks @brijs

But...as I responded to the comment above...

I have a $$anonymous$$ain.cs which I am using to create all my gameObjects, including the quad, using...

quad = GameObject.CreatePrimitive( PrimitiveType.Quad );

I can see the quad. But now I need to add the necessary components (renderer? mesh? material?) and then add the texture. I need to be specific that I am adding the texture to THAT gameObject.

Hopefully this puts my question into the right context for the right answer :)

Thanks! -j

2 Replies

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

Answer by JeffreyVentrella · Jun 20, 2016 at 03:26 PM

OK folks, I came up with my answer by poking around the interwebs (the answers given were too sparse).

I HOPE THIS IS USEFUL for other people with the same question that I had:

  1. I created a Resources folder inside of the Assets folder.

  2. I Created a Material in the Resources folder which I renamed to "Whatever".

  3. I dragged a .png file called "my_image" into the Resources folder.

  4. I added the following code to the Start method:

    GameObject quad = GameObject.CreatePrimitive( PrimitiveType.Quad );

    Material quadMaterial = (Material)Resources.Load( "Whatever" );

    quad.GetComponent().material = quadMaterial;

    Texture2D myTexture = Resources.Load( "my_image" ) as Texture2D;

    quad.GetComponent().material.mainTexture = myTexture;

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
1

Answer by Brijs · Jun 16, 2016 at 10:33 AM

In Monobehaviour attached to quad

1) Load texture

 Texture texture = Resources.Load<Texture>("TextureName") as Texture;

2) Now wherever you want to add texture to quad write this

 GetComponent<Renderer>().material.mainTexture = texture;
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

6 People are following this question.

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

Related Questions

Changing a texture as a float or int drops 2 Answers

How can I reset the score counter? 0 Answers

I have problems with scripting movement, anyone can help me? 0 Answers

Player doesn't turn 1 Answer

How can i make my spaceship to land automatic on the Base ? 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