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
2
Question by frmusta · Jun 14, 2010 at 09:42 PM · updatetexture2dstringassign

Create Texture2D and assign image to it through a script

Im trying to develope a system where if i enter the name of a texture i have in my assets folder, i can create a texture2d object and apply the string name to that object and have it find and apply the image in my assets folder to it.

Where im getting stuck is, when im trying to define the Texture2D asset in my script, i cant just say something like this:

var showImage : boolean = true; var stringForNewImageToBeDisplayed : String = "myImage"; //this string will be changing through an XML script periodically var currentImage : Texture2D; //the Texture that im trying to update

function showImageOverlay() { if(showImage) { //where im trying to assign the name of an image i have in my asset folder currentImage = stringForNewImageToBeDisplayed; } }

function OnGUI() { //shows image on screen GUI.DrawTexture (Rect (Screen.width*.1, (Screen.height/2)-(((Screen.width*.8)/1.6)/2)-20, Screen.width*.8, (Screen.width*.8)/1.6), currentImage);
}

This seems like it should be pretty simple but i dont know my way around JavaScript well enough to figure it out. Am i close? Thanks very much for any tips you can give me here!

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

Answer by qJake · Jun 14, 2010 at 10:21 PM

In Unity you don't access textures by name, you access them through references. This not only speeds up game performance, but it minimizes the size of the compiled game because non-used assets are not included.

Typically what you would do is create a public variable in your script, and then assign its value through the Inspector. You can even have arrays of assets if you choose.

The only way to call an asset by name is to place it inside the special folder Resources/. Any asset inside this folder is automatically included in the game, no matter what (even if you don't use it), so using the Resources folder to load assets exclusively is somewhat dangerous.

Any asset you place inside the Resources folder can be accessed by its name, like this:

var myTexture = Resources.Load("myImage");

but remember that anything you want to call by name needs to be in the Resources/ folder. The preferred way of retrieving assets is by reference through the Inspector, but if this absolutely does not work for you, you can use Resources.Load();.

Read more about Resources.Load(); here:

http://unity3d.com/support/documentation/ScriptReference/Resources.html

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 frmusta · Jun 14, 2010 at 10:28 PM 0
Share

Thank you for your answer, this is exactly what i needed. I understand now that its not the ideal way to call objects, but im trying to make this part of my script as modular as possible where by changing out scripts it will adjust the file. using the inspector is how i was doing it before but has become too time consu$$anonymous$$g and tedious. This should work great, thanks!

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

No one has followed this question yet.

Related Questions

How Can I Change GetType() And GetField() And GetValue() ? 2 Answers

How to Fire Shells With Speech Recognition Input 3 Answers

Help with assigning an object with Instantiate 1 Answer

Can't assign a texture from WWW to Image.material.maintexture 1 Answer

How to Update a score count going up and down 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