Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Jack_4mobile · Feb 21, 2013 at 02:47 PM · c#textureimageload

Loading user custom image.

I'm pretty desperated with this. I'm crafting simple openGL program, where user put his own image inside so i cant put that image into assets. I tried www class and also tried Texture.LoadImage(string path). www class loaded image, texture loaded as well. But the image never appeared at the program and unity just shown white material. This is how the code looks lately:

 string path = Application.dataPath;
         if (Image == "") {
             path = Path.Combine(path, "pink.png");
         } else {
             path = Path.Combine(path, Image);
         }
         
         
         var shaderText =
         "Shader \"Alpha Additive\" {" +
         "Properties { _MainText (" +
         "\"texture\""+
         ", 2D) = \"white\" {}}" +
         "SubShader {" +
         "    Tags { \"Queue\" = \"Transparent\" }" +
         "    Pass {" +
         "        Blend One One ZWrite Off ColorMask RGB" +
         "        Material { Diffuse [_Color] Ambient [_Color] }" +
         "        Lighting On" +
         "        SetTexture [_Dummy] { combine primary double, primary }" +
         "    }" +
         "}" +
         "}";
         
         Texture2D texture = new Texture2D(1,1);
         FileStream stream = new FileStream(path, System.IO.FileMode.Open);
         BinaryReader reader = new BinaryReader(stream);
         byte[] buffer = reader.ReadBytes(10000000);
         texture.LoadImage(buffer);
         
         var noteMaterial = new Material(shaderText);
         noteMaterial.mainTexture = texture;
         
         MeshRenderer[] NoteRenderer = note.gameObject.GetComponentsInChildren<MeshRenderer>();
         NoteRenderer[2].material = noteMaterial;

as i wrote i also tred www class. First thing - at monodevelop it says "yield www;" is not regular command and when i used "yield return www" the unity shouts it is not regular command. Only thing i beg is to have code which will take my picture from app folder and put it on some prefab and im really tired of finding only Resources.Load, and unsuccessfull www class and texture.loadimage(). Well is there anyone who was lucky with doing this? thx.

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
2

Answer by robertbu · Feb 21, 2013 at 04:22 PM

The syntax in C# for waiting for load using the WWW clase is "yield return www". Here is a few lines demonstrating a load of a texture using WWW:

 public class LoadTexture : MonoBehaviour {
 
     void Update () {
         if (Input.GetKeyDown (KeyCode.A))
             StartCoroutine (LoadATexture ("file://e://foo.jpg"));
     }
     
     IEnumerator LoadATexture(string st) {
         WWW www = new WWW(st);
         yield return www;
     
         if (www.error == null) {
             renderer.material.mainTexture = www.texture;
         }    
     }
 }
Comment
Add comment · 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 Jack_4mobile · Feb 21, 2013 at 06:13 PM 1
Share

Well this answer i found many times before and i tried it many times before.

For everyone whose stumbling on this: This answer works but one important thing is to add "file:\\" or "file://" on the beggining.

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

9 People are following this question.

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

Related Questions

Image gallery - load png from external folder (android) 2 Answers

Loading Images / Textures. Resources.Load() vs www.texture 5 Answers

How do I iterate over a large Texture2d quickly? 1 Answer

Change texture of RawImage 2 Answers

Multiple Cars not working 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