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.
Try to add the image to a quad. It will automatically make a material. The quad can be made a prefab.
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
Setup the "Texture Type" of your image in the Import settings for your image to Sprite (2D UI).
Create an empty GameObject in the hierarchy. Give it a meaningful name.
Add a SpriteRenderer to the newly created GameObject.
Set the Sprite property value to your image.
Drag and drop the GameObject to somewhere in the Assets tab (usually under a folder named Prefabs).
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
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).
Now create a material (place this in a Materials folder) and give it a meaningful name.
You should not need to change the shader type of the material initially.
Set the Albedo image value to your image by selecting it from the dropdown or by dragging and dropping it.
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.
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.
Drag and drop the GameObject to somewhere in the Assets tab (usually under a folder named Prefabs).
Once the prefab is created you can delete it in the hierarchy.
@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!
@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!
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!
@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.
yes, sorry I didnt see your reply for some reason. I clicked Accept, is it O$$anonymous$$ now?
Thanks!
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.
Hi @tgfawad,
Here is a link to the package. The package has the following
Assets_Scenes\PrefabCreationDemo.unity - scene file
Assets\Scripts\Prefab$$anonymous$$anager.cs - Class to create prefabs
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.
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!
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?
Hi @stevenhadley,
I am sorry but because the script uses the following functions
AssetDatabase.CreateAsset()
PrefabUtility.CreatePrefab()
AssetDatabase.SaveAssets()
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.