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 Theacesofspades · Feb 21, 2013 at 02:38 AM · javascriptguitexturearray

How can i show the texture in the array?

I have this script and i am wondering how i can display the texture that is in this array. The script i have is:

 var personArray : Array = [myPerson];
 var myTexture : Texture;
 
 
 class myPerson
 {
     var name = "Brandon";
     var texture : Texture;
 }
 
 function Start ()
 {
     texture = myTexture;
 }
 
 function OnGUI () 
 {
     GUI.Button(Rect(0, 0, 100, 100), personArray[0]);
 }
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 flamy · Feb 21, 2013 at 04:56 AM

well the above answer solves the problem of handling texture with arrays, but it is not a good practice to link textures to scripts from editor, since they will stay in memmory as long as the gameObject is in the scene... For an instance 20 of 4mb texture will hold 80 mb in RAM for nothing.

Better practice is to use string arrays of path to texture and use Resources.Load() function to load them when needed. It will be automatically unloaded by garbage collector or you can use this function to do that manually.

This suggestion becomes completely useless if you have to use all of the textures at once. But just remember that objects(not only texture) that are attached to editor will stay in memory until the gameobject exists.

Comment
Add comment · Show 4 · 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 sona.viswam · Feb 21, 2013 at 05:08 AM 1
Share

@vijayakumar (flamy) . Thanks for sharing your knowledge. Its really helpful to me as a beginner. If u dont $$anonymous$$d can you explain with an example. i tried child.renderer.material.mainTexture = Resources.Load("halowin_pumkin-off") as Texture; but no texture showing.

avatar image flamy · Feb 21, 2013 at 05:21 AM 1
Share

$$anonymous$$ake a folder called Resources in your project and place the texture inside that folder. if it is inside folder of heiraricy Resources/UI/mytexture you have to pass UI/mytexture, I hope this is clear.

avatar image sona.viswam · Feb 21, 2013 at 05:28 AM 1
Share

@flamy: cool.its work. while clicking on each object, same texture will load. is it make memory lose ? how can i unload?

avatar image flamy · Feb 21, 2013 at 05:49 AM 1
Share

one good thing about Resources.Load is, if it is in memory already it wont load again, it will just give a reference to it. To unload you can you use Resourcs.UnloadAsset(); or use Resources.UnloadUnusedAsstes();

 #pragma strict
 
     var personArray : String[];
     var $$anonymous$$yTexture : Texture;
     var imageIndex :int;
     
     function Start()
     {
       imageIndex=0;
       $$anonymous$$yTexture= Resources.Load(personArray[0]) as Texture;
     }
     
     function OnGUI()
     {
        if(GUILayout.Button($$anonymous$$yTexture))  //changes texture
        {
           Resources.UnloadAsset($$anonymous$$yTexture);
     
           imageIndex++;
           if(imageIndex>=personArray.Length)
           imageIndex=0;
     
           $$anonymous$$yTexture = Resources.Load(personArray[imageIndex]) as Texture;
     
           
        }
     
     }

avatar image
2

Answer by sona.viswam · Feb 21, 2013 at 04:07 AM

Declare array of textures.

 var personArray : Texture[];

From scene you can give array length and just drag and drop your textures to the elements of array (public parameters).

Then you call that texture as

 function OnGUI () 
 {
     GUI.Button(Rect(0, 0, 100, 100), personArray[0]);
 }

HTH..

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 Theacesofspades · Feb 21, 2013 at 05:16 AM 0
Share

But is there a way to do it with the array that i already created? Sorry im new to arrays.

avatar image Theacesofspades · Feb 22, 2013 at 02:35 AM 0
Share

Can i just use the array that i have and pull out that texture?

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

11 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

Related Questions

Applying different textures to spawned objects. 2 Answers

Aspect ratio keeping guitexture scaling 1 Answer

Menu material error 1 Answer

drawing GUI or Instantiate an object to highlight multiple object 2 Answers

What type of Array should I use? 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