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 /
avatar image
0
Question by LK84 · Aug 04, 2016 at 06:16 PM · texture2dimagereferencefile-ioraw

Loading Textures during runtime and applying to Raw Images

Hi, for my mobile game I wanna give the user the option to choose from different camera views. Therefore he can make screenshot and they are later displayed in the menue (as child of a scroll rect, s. picture). The screenshots are saved in Application.persistentDataPath as .png. So far so good. Saving the screenshot as .png works fine. Only when I try to load them during runtime I run into some issues.

In the script attached to an empty Game Object in the scene with the empty Raw Images I am trying now to assign the textures to each Raw Image. The Array ImageHolder corresponds to each GameObject holding the Raw Image component.

 using UnityEngine;
 using System.Collections;
 using System.IO;
 using System.Collections.Generic;
 using  UnityEngine.UI;
 using System;
 
 public class test : MonoBehaviour
 {
 
     Texture2D thisTexture;
     byte[] bytes;
     string fileName;
     public GameObject[] ImageHolder = new GameObject[5];
 
     void Start()
     {
         thisTexture = new Texture2D(100, 100);
         string[] ImageNames = { "Image1", "Image2", "Image3", "Image4", "Image5" };
         for (int i = 0; i < ImageNames.Length; i++)
         {
             fileName = ImageNames[i];
             bytes = File.ReadAllBytes(Path.Combine(Application.persistentDataPath, fileName + ".png"));
             thisTexture.LoadImage(bytes);
             thisTexture.name = fileName;
             ImageHolder[i].GetComponent<RawImage>().texture = thisTexture;
 
         }
 
     }
 }

I simply can't understand why I get this alt text instead of this alt text

For some reason the last texture from Image5 is written in all Raw Images. Why???

images-wrong.png (19.3 kB)
images.png (10.5 kB)
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

2 Replies

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

Answer by LK84 · Aug 05, 2016 at 01:51 PM

Ok, figured it out myself. I just had to create a new instance of Texture2d within the for loop, not before:

  public class test : MonoBehaviour
  {
  
      Texture2D thisTexture;
      byte[] bytes;
      string fileName;
      public GameObject[] ImageHolder = new GameObject[5];
  
      void Start()
      {
          
          string[] ImageNames = { "Image1", "Image2", "Image3", "Image4", "Image5" };
          for (int i = 0; i < ImageNames.Length; i++)
          {
              thisTexture = new Texture2D(100, 100); //NOW INSIDE THE FOR LOOP
              fileName = ImageNames[i];
              bytes = File.ReadAllBytes(Path.Combine(Application.persistentDataPath, fileName + ".png"));
              thisTexture.LoadImage(bytes);
              thisTexture.name = fileName;
              ImageHolder[i].GetComponent<RawImage>().texture = thisTexture;
  
          }
  
      }
  }

To be honest, I can't fully comprehend why that is the solution. So ImageHolder[i].GetComponent<RawImage>().texture=thisTexture; apparently sets thisTextureas a reference to ImageHolder[i].GetComponent<RawImage>().texture.

But isn't that in contrast to the follwing simple example:

 public class rbReference : MonoBehaviour {
 
     float bodyMass;
 
     void Start()
     {
         bodyMass = 5.0f;
         GetComponent<Rigidbody>().mass = bodyMass;
         bodyMass = 10.0f;
     }
 
 }

Here the Rigidbody mass remains 5 (as I expected) and not 10 --> bodyMass is NOT a reference to the RigidBody Component Mass. I'm really confused now and to me it seems kind of arbitrary when Unity sets references and when not. Or am I missing something completly here?

Comment
Add comment · Show 2 · 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 Landern · Aug 05, 2016 at 02:00 PM 1
Share

When objects are instantiated(created) and they are reference types then when you create a Texture2D object and assign it the object is referencing a pointer in memory. If you have an array or collection of this type of object and you use the same one, stuff it into that collection/array, you're stuffing the reference to the object. So when you created the outer object and you updated the properties to use a different image it was updating all of them since all the items in the collection point to the exact same object. When you're using the new keyword you're allocating new memory and new references to this object so when you change the properties they are unique, but wasn't unique when you used the same object over and over in the loop.

Example of the outer Texture2D instantiation:

 thisTexture = new Texture2D(100, 100); // made up address A0000000

Each time you added to your collection:

 ImageHolder[i].GetComponent<RawImage>().texture = thisTexture; 

thisTexture was just adding a pointer to the object in slot A0000000(again made up address). So regardless of what index you where assocating with using I$$anonymous$$ageHolder the pointer pointed to the same reference or object in memory.

When you added the ability to instantiate per texture since it has different images associated with it, it created a new memory location for each item as you looped through the for loop.

avatar image LK84 Landern · Aug 05, 2016 at 02:09 PM 0
Share

Ok, makes sense. Thank you

avatar image
0

Answer by adityainfy · Oct 23, 2020 at 01:26 AM

public class test : MonoBehaviour {

   Texture2D thisTexture;
   byte[] bytes;
   string fileName;
   public GameObject[] ImageHolder = new GameObject[5];
  
   void Start()
   {
       
       string[] ImageNames = { "Image1", "Image2", "Image3", "Image4", "Image5" };
       for (int i = 0; i < ImageNames.Length; i++)
       {
           thisTexture = new Texture2D(100, 100); //NOW INSIDE THE FOR LOOP
           fileName = ImageNames[i];
           bytes = File.ReadAllBytes(Path.Combine(Application.persistentDataPath, fileName + ".png"));
           thisTexture.LoadImage(bytes);
           thisTexture.name = fileName;
           ImageHolder[i].GetComponent<RawImage>().texture = thisTexture;
  
       }
  
   }

}

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity 5.6.4 Canvas RawImage hides all objects 0 Answers

How do I shrink a cube with scripts? 1 Answer

Texture2D creating blurry textures 1 Answer

[SOLVED] Bad PKCS7 While encrypting a file 1 Answer

!texture.texture error 0 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