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 /
  • Help Room /
avatar image
0
Question by orenko · Jul 28, 2016 at 02:59 PM · imageprefabs

Creating prefab from image files

Hello, I want to make prefabs from some images (.png, .jpeg files) that I would. Meaning, I want to add an object that will look like my images, so the player can pass through it or pick up. How can I do it in unity? I couldn't find anyway to create prefab from image.

Thanks.

Comment
Add comment · Show 1
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 andrei2699 · Jul 28, 2016 at 05:01 PM 0
Share

Try to add the image to a quad. It will automatically make a material. The quad can be made a prefab.

3 Replies

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

Answer by TBruce · Jul 28, 2016 at 05:47 PM

A prefab s a copy of a GameObject converted into a reusable asset -- it shows up in the Project folder and is serialized as a file on disk.

That said you have not mentioned if this is 2D or 3D.

If 2D (this can also be done 3D but may or may not have the desired effect) do this

  1. Setup the "Texture Type" of your image in the Import settings for your image to Sprite (2D UI).

  2. Create an empty GameObject in the hierarchy. Give it a meaningful name.

  3. Add a SpriteRenderer to the newly created GameObject.

  4. Set the Sprite property value to your image.

  5. Drag and drop the GameObject to somewhere in the Assets tab (usually under a folder named Prefabs).

  6. Once the prefab is created you can delete it in the hierarchy.

If 3D and you are not going the method described above you can do it this way

  1. Setup the "Texture Type" of your image in the Import settings for your image to Texture (you can leave this as a sprite like step 1 above if desired).

  2. Now create a material (place this in a Materials folder) and give it a meaningful name.

  3. You should not need to change the shader type of the material initially.

  4. Set the Albedo image value to your image by selecting it from the dropdown or by dragging and dropping it.

  5. Now here instead of creating an empty GameObject you need to create a 3D mesh of some kind. You will need to decide which type of mesh per your needs. For the sake of this answer we will use a Quad but use the one that works for your needs.

  6. Now you need to set Element 0 of the Materials property in the Mesh Renderer to your new material. This can be done by either selecting it from the dropdown or by dragging and dropping it.

  7. Drag and drop the GameObject to somewhere in the Assets tab (usually under a folder named Prefabs).

  8. Once the prefab is created you can delete it in the hierarchy.

Comment
Add comment · Show 11 · 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 TBruce · Jul 29, 2016 at 05:23 PM 0
Share

@orenko Would you be so kind as to click the tick to accept the answer if your question was answered to your satisfaction. Thank you!

avatar image orenko · Aug 01, 2016 at 02:09 PM 0
Share

Got it.

Thanks!

avatar image TBruce orenko · Aug 01, 2016 at 06:02 PM 0
Share

@orenko Does that mean that your question was answered to your satisfaction? If so could you click the Accept box to accept the answer ? Thank you!

avatar image orenko · Aug 02, 2016 at 02:30 PM 0
Share

Hi, If I want to do the described above (in 3D) in code, how can I do it?

I have a library of 60 pics, and I want to create quad prefab for each of the pics. So, I think it better to do it in the code rather then manually. Is there a way to do it?

Thank you!

avatar image TBruce orenko · Aug 02, 2016 at 06:42 PM 0
Share

@orenko I can give you a script that can do what you want. I only ask that you first accept this answer by clicking the Accept box above. Thank you.

avatar image TBruce orenko · Aug 09, 2016 at 05:52 PM 0
Share

@orenko Are you interested in this script?

avatar image orenko TBruce · Aug 10, 2016 at 07:53 PM 0
Share

yes, sorry I didnt see your reply for some reason. I clicked Accept, is it O$$anonymous$$ now?

Thanks!

Show more comments
avatar image tgfawad · Jan 10, 2017 at 11:57 AM 0
Share

Hi @TBruce, can you kindly share the script with me too.. I am looking for the same thing but the link you pasted here is dead now. Thank you.

avatar image TBruce tgfawad · Jan 10, 2017 at 08:45 PM 1
Share

Hi @tgfawad,

Here is a link to the package. The package has the following

  1. Assets_Scenes\PrefabCreationDemo.unity - scene file

  2. Assets\Scripts\Prefab$$anonymous$$anager.cs - Class to create prefabs

  3. Assets\Scripts\Editor\Prefab$$anonymous$$anagerCustomEditor.cs - custom editor file

The scripts that you are asking about are Prefab$$anonymous$$anager.cs and Prefab$$anonymous$$anagerCustomEditor.cs. At a $$anonymous$$imum you will need to add Prefab$$anonymous$$anager.cs to a game object. If you add the custom editor to the scene the button "Create Prefabs" will be added to the inspector.

When the button is pressed, or if the public function CreatePrefabs() is called, prefabs will be made out of all textures in the Image Path which defaults to "Assets\Resources\Textures". Because the script uses the Resources.LoadAll() function, all textures need to be somewhere under the Assets\Resources folder. There is a public path called Image Path that gets tacked on to Assets\Resources which defaults to Textures resulting in Assets\Resources\Textures as stated above.

avatar image tgfawad TBruce · Jan 11, 2017 at 12:31 PM 0
Share

@TBruce. Thank You. :)

avatar image
0

Answer by orenko · Aug 24, 2016 at 12:30 PM

Hi @Mavina, I have a question regarding your script: Is there a reason for naming it : PrefabManager : MonoBehaviour is it called automatically somewhere throughout the game? I have a specific place I want to call the scripts and get the list of prefabs:

         private void AddToPool ()
         {
             
             // Select a random prefab.
             int randomIndex = Random.Range (0, m_Prefabs.Length);
 
             // Instantiate the prefab.
             GameObject instance = Instantiate(m_Prefabs[randomIndex]);
 
             // Make the instance a child of this pool and turn it off.
             instance.transform.parent = transform;
             instance.SetActive (false);
 
             // Add the instance to the pool for later use.
             m_Pool.Add (instance);
         }

Here, instead of the line :

 GameObject instance = Instantiate(m_Prefabs[randomIndex]);

I want to insert:

 GameObject instance = Instantiate(list_generated_using_your_script[randomIndex]);

Can I do it?

Thanks!

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 orenko · Aug 31, 2016 at 03:36 PM 0
Share

Hi, Does anyone have an answer? @$$anonymous$$avina?

avatar image
0

Answer by stevenhadley · Jan 06, 2017 at 06:59 AM

Is there a way to automate this? Sort of like a batch script in windows?

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 TBruce · Jan 10, 2017 at 08:54 PM 0
Share

Hi @stevenhadley,

I am sorry but because the script uses the following functions

  1. AssetDatabase.CreateAsset()

  2. PrefabUtility.CreatePrefab()

  3. AssetDatabase.SaveAssets()

  4. AssetDatabase.CreateFolder() - this is only used to create folders under the Assets directory if they do not exist

this will only work in the editor.

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

67 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 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 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

Prefab UI Slider misses the fill and background items... 0 Answers

How do you change offset Image UGUI? 0 Answers

Images as buttons with GUI skins 1 Answer

Activate/Deactivate image with GetKeyDown? 0 Answers

Is it possible to make a script that pushes this button? 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