• 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
Question by GlennNZ · Oct 12, 2013 at 10:42 PM · textureruntimeresourcesdynamic

Texturing a mesh created at runtime

I'm trying to dynamically apply a texture to a dynamically created mesh at runtime. In my script, which is a Billboarding script I use to have a simple quad always face the camera, the mesh is created and the texture applied in the Start() method. I've then drag dropped this script onto a GameObject in the scene.

This works fine in the Unity editor, but unfortunately on iOS and Android all I get is the pink mesh/billboard, which (to my understanding) is usually a symptom of a shader not having loaded or not being supported by the hardware.

I use the following code in the Start() method of my script (The GameObject the script is attached to also has a Character Controller script and some A* pathfinding scripts added to it).

 gameObject.AddComponent("MeshFilter");
 gameObject.AddComponent("MeshRenderer");
 MeshFilter filter = GetComponent<MeshFilter>();
 Mesh mesh = filter.mesh;
 Renderer renderer = GetComponent<MeshRenderer>().renderer;
         
 mesh.Clear();
 mesh.vertices = vertices;
 mesh.uv = uvs;
 mesh.triangles = triangles;
 mesh.RecalculateNormals();
         
 renderer.material.mainTexture = Resources.Load("tank_icon") as Texture;
 var shader = Shader.Find ("Particles/Alpha Blended");
 gameObject.renderer.material.shader = shader;

Where tank_icon is really tank_icon.png, which is a 32bit RGBA .png and is located in the Assets/Resources folder.

Now, the weird part is that, let's say I have a default plane GameObject added to the scene and I drag onto it that very same tank_icon.png and apply the "Particles/Alpha Blended" shader to it, then both the default plane and my billboarded quad (which was originally pink), renders correctly in both the Editor and Devices.

As soon as I remove the tank_icon material from the the Default plane GameObject, then my Billboarding GameObject is pink again (on device)

Could someone please share some insight as to what is going on and what I have to get this to work on the devices.

Thanks.

  • apologise if this thread appears twice on the site but my previous attempt at this question got stuck in moderation and didn't appear in my post history.*

Comment

People who like this

0 Show 0
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
Best Answer

Answer by robertbu · Oct 13, 2013 at 07:19 AM

Unity only pulls in things that you reference. If you don't reference a certain material or texture or script, Unity does not include it in the build. An attempt to dynamically load a shader using a string does not count as a reference. So if something in your build pulls in this shader, then the Shader.Find() will find it. If you don't reference it anywhere, then it is not pulled in, and the Shader.Find() will fail.

You could create a material with this shader and assign it to the component through drag and drop in the editor...then assign the material instead of just the shader. My guess is that you could also dynamically load the material from Resources.

Comment
DylanW

People who like this

1 Show 1 · 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 GlennNZ · Oct 15, 2013 at 09:12 AM 0
Share

Thanks. It took me a little while to trace down everything that was going on.

Yes, it seems that creating a material that uses the .png and the shader (in my case Mobile/Particles/Alpha Blended) was required in order to dynamically apply a texture and shader from inside a script. From there, I had a choice of either applying the material with...

 renderer.material = Resources.Load ("tank_material") as Material;

or applying the texture and shader separately with...

 renderer.material.mainTexture = Resources.Load("tank_icon_red") as Texture;
 renderer.material.shader = Shader.Find ("Mobile/Particles/Alpha Blended");

I'm assuming all those assets have to live in the Resource folder too.

Thanks for the help.

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

15 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

Related Questions

Dynamicaly Creating A Plane With A JPG Material ... 2 Answers

Changing shared texture at runtime without affecting project settings? 1 Answer

dynamic images for textures ar runtime 2 Answers

Dynamically load AudioClip during runtime 1 Answer

Load texture and shaders at runtime on iPhone 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