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 /
This question was closed Nov 20, 2014 at 05:13 AM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
4
Question by LijuDeveloper · Mar 10, 2014 at 11:14 AM · c#imageavatarupload

How to upload an image from our system or device

How to upload an image using c# . In my game, need to upload Avatar picture . How to upload an image from our system or device.

Comment
Add comment · Show 3
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 Nick4 · Mar 10, 2014 at 11:17 AM 0
Share

http://docs.unity3d.com/Documentation/ScriptReference/WWW-texture.html

avatar image LijuDeveloper · Mar 11, 2014 at 03:55 AM 0
Share

Nick4 , i asking about uploading images, not about downloading

Anyone else know how to upload an image from our system or device

avatar image thornekey · Mar 11, 2014 at 10:39 AM 0
Share

Nick4 is right.. you can use that concept to technically 'download' from your own hard drive. ill post an example in a sec

2 Replies

  • Sort: 
avatar image
3
Best Answer

Answer by LijuDeveloper · Nov 20, 2014 at 05:10 AM

i've got the solution. I got a free plugin for file browser . C# File Browser in Asset Store.

1 .For File browser code please refer Accessing local system ( File Browser ) in Unity answers.

2.Code for uploading image

     using UnityEngine;
     using System.Collections;
     using System.Xml;
     using System.Linq;
     using System.IO;
     using System ;
     using System.Text;
     using System.Runtime.Serialization.Formatters.Binary;
     using System.Net;

     public class c_BrowserManager : MonoBehaviour {

         public GameObject fileBrowserObj;
         public GameObject filePhoto;
         public Texture defaultTexture;
         private Texture fileTexture;
         public   tk2dTextMesh fileNameText;

         private string  imgUploadUrl = "http://google.com/game/data/image_upload?";// here your url
         // Use this for initialization
         void Start () {
             c_global.fileFullName="";
             fileBrowserObj.SetActive (false);
             fileNameText.text ="Choose an image to upload";
         }
         
         // Update is called once per frame
         void Update () 
         {
             if(c_global.isFileBrowserShow == true)
             {
                 fileBrowserObj.SetActive (true);
                 c_global.isFileBrowserShow = false;
             }
             if(c_global.isFileBrowserHide == true)
             {
                 fileBrowserObj.SetActive (false);
                 c_global.isFileCheck = true;
                 c_global.isFileBrowserHide = false;
             }

             if(c_global.isFileCheck == true)
             {
                 if(c_global.fileFullName !="")
                 {
                     //print ("LLLLL");
                     fileNameText.text = c_global.fileFullName;
                     StartCoroutine (WaitForImageDisplay());
                 }
                 c_global.isFileCheck = false;
             }

             if(c_global.isFileUpdateOn == true)
             {
                 if(c_global.fileFullName !="")
                 {
                     //print ("LLLLL");
                     fileNameText.text = c_global.fileFullName;
                     StartCoroutine (WaitForImageUpload());
                 }
                 else
                 {
                     fileNameText.text ="Please select an image";
                 }
                 c_global.isFileUpdateOn = false;
             }

         
         }

         void PostImageInfo()
         {

         }
         IEnumerator WaitForImageDisplay()
         {
             WWW localFile = new WWW ("file://"+c_global.fileFullName);
             yield return localFile;
             //print ("Image Display Calling");
             if(localFile.error == null)
             {
                 fileTexture = localFile.texture;
                 filePhoto.renderer.material.mainTexture = fileTexture;
             }
             else
             {
                 filePhoto.renderer.material.mainTexture = defaultTexture;
                 //print ("Error :"+localFile.error);
             }
         }

         IEnumerator WaitForImageUpload()
         {
             WWW localFile = new WWW ("file://"+c_global.fileFullName);
             yield return localFile;
             //print ("Image Display Calling");
             if(localFile.error == null)
             {
                 fileTexture = localFile.texture;
                 filePhoto.renderer.material.mainTexture = fileTexture;
                 
             }
             else
             {
                 filePhoto.renderer.material.mainTexture = defaultTexture;
                 //print ("Error :"+localFile.error);
                 yield break;
             }
             WWWForm myForm  = new WWWForm();

         //    string result = System.Text.Encoding.UTF8.GetString(localFile.bytes);
         //    string result = System.Text.Encoding.UTF8.GetString(localFile.bytes);

             FileInfo file = new FileInfo(c_global.fileFullName);

             string fileExtension = file.Extension;
             string []tempArray = fileExtension.Split("."[0]);

     //        int leng = fileExtension.Length ;
     //        char [] temp = fileExtension.ToCharArray ();
     //        char [] temp2 = new char[leng-1];
     //
     //        for(int i=0,j=0;i<leng ;i++)
     //        {
     //            if(i>0)
     //            {
     //                temp2[j] = temp[i ] ;
     //                //print ("temp2["+j+"] = " +temp2[j] );
     //                j++;
     //            }
     //        }
     //        file
                 fileExtension = tempArray[1];

             string result =Convert.ToBase64String(localFile.bytes);

             //print ("Filetype :"+fileExtension );


             //myForm.AddBinaryData ("img",localFile.bytes);
             //myForm.AddField ("img",localFile.ToString());
             myForm.AddField ("id",c_global.userId);
             myForm.AddField ("type",fileExtension);
             myForm.AddField ("img",result);
             //print (" Image to string :"+result);
             c_global.isImgUploadWinHide = true;
             WWW upload = new WWW (imgUploadUrl ,myForm);

             StartCoroutine (WaitForImageUploadToServer(upload));
             /*

             WWW upload = new WWW (imgUploadUrl ,myForm);
             yield return upload;

             if(upload.error ==null)
             {
                 if( upload.text != "" )
                 {
                     XmlDocument l_doc = new XmlDocument ();
                     l_doc.LoadXml(upload.text);
                     
                     XmlNode  l_status = l_doc.SelectSingleNode("data/status");
                     //print(" Upload Status !"+l_status.InnerText);

                 }
                 else
                 {
                     //print(" Upload Error Null Text");
                 }
             }
             else
             {
                 //print(" Upload Error :"+upload.error);
             }*/
         }

         IEnumerator WaitForImageUploadToServer(WWW upload)
         {

             //WWW upload = new WWW (imgUploadUrl ,myForm);
             yield return upload;
             
             if(upload.error ==null)
             {
                 if( upload.text != "" )
                 {
                     XmlDocument l_doc = new XmlDocument ();
                     c_global.isFileUpdateCompleted = true;
                     c_global.isImageUploadError = true;
                     c_global.isImageUploadErrorMsg = "Image uploaded successfully !";
     //                try
     //                {
     //
     //                
     //                l_doc.LoadXml(upload.text);
     //                
     //                XmlNode  l_status = l_doc.SelectSingleNode("data/status");
     //                //print(" Upload Status !"+l_status.InnerText);
     //                }
     //                catch (Exception e)
     //                {
     //                    //print (" Exception in uploading :"+e);
     //                }
                     
                 }
                 else
                 {
                     //print(" Upload Error Null Text");
                 }
             }
             else
             {
                 //print(" Upload Error :"+upload.error);
                 c_global.isImageUploadError = true;
                 c_global.isImageUploadErrorMsg = "Image uploading fail !";
             }
         }



     }


Comment
Add comment · Show 3 · 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 mpdrakou · Jan 16, 2015 at 03:32 PM 0
Share

hello, what are the steps which you follow to do the upload?

avatar image hvatrex · Oct 02, 2015 at 06:09 PM 0
Share

hey thank for your great help but i have simple question what is c_global?

avatar image rooksFX · Oct 20, 2015 at 11:37 PM 0
Share

Will this work with Android Phones..?

avatar image
2

Answer by thornekey · Mar 11, 2014 at 10:44 AM

     public Texture texNotFound;
     private string filelocationinput = "";
  
     void OnGUI ()
     {
  
        filelocationinput = GUI.TextField (new Rect (10, 10, 128, 20), filelocationinput);
  
        if (GUI.Button (new Rect (2, 10, 128, 32), "Choose Avatar!")) {
          StartCoroutine(LoadTexture ());
  
        }
     }
  
     IEnumerator LoadTexture ()
     {
        WWW www = new WWW ("file://"+filelocationinput);;
  
        yield return www;
  
        Texture avatarImage = null;
  
        if (www.error == null && www.texture != null) {
          avatarImage = www.texture;
        }
        else {
          avatarImage = texNotFound;
          Debug.Log ("Texture not found");
        }
  
        renderer.material.mainTexture = avatarImage;
     }
 }


something like this work. attach a material to the object you want to have the avatar texture on.

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

Follow this Question

Answers Answers and Comments

26 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

Related Questions

Downsize/compress image on upload to Firebase? 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Download images from server and storing it in Resources. 1 Answer

Image does not display correctly after fulfilling the conditions 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