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 kayeejoe · Jul 27, 2018 at 01:22 AM · prefabmaterialtexture2dresources.loadsharedmaterial

When generating prefab, i cant load images from either resource or assetbundle

I want to generate prefabs for later use, because I have a lot of images to be used as textures. I tried almost every method (useful or not) online but still cannot figure it out. I have tried Resources.Load<Material> or Resources.Load() as Material, but all testing result returns null from Resources.Load. I am sure that the path to the Resource folder is correct, and so is the filename. The code is as below:

     GameObject quads_Axial = new GameObject();
     string localPath2 = "Assets/Prefabs/Axial/brain-Axial.prefab";   //2
     for (int i = 0; i < fCount2; ++i)
     {
         Material mat = Resources.Load("Textures/Axial/brain-Axial-" + (i + 1).ToString()) as Material;
         if (mat == null) Debug.Log( (i + 1).ToString());
         //mat.SetTexture("_MainTex", Resources.Load("Textures/Axial/brain-Axial-" + (i + 1).ToString()) as Texture2D);
         GameObject quad = GameObject.CreatePrimitive(PrimitiveType.Quad);
         quad.transform.position = new Vector3(0, half_scale - (float)i * 0.002f, half_scale);
         quad.transform.Rotate(Vector3.right, 90.0f);
         quad.transform.localScale = new Vector3(scale_size, scale_size, scale_size);
         //quad.GetComponent<Renderer>().material.color = Color.red;  //
         quad.GetComponent<Renderer>().sharedMaterial = mat;
         quad.transform.parent = quads_Axial.transform;
     }
     if (AssetDatabase.LoadAssetAtPath(localPath2, typeof(GameObject)))
     {
         if (EditorUtility.DisplayDialog("Are you sure?",
                 "The prefab already exists. Do you want to overwrite it?",
                 "Yes",
                 "No"))
         {
             CreateNew(quads_Axial, localPath2);
         }
     }
     else
     {
         Debug.Log(quads_Axial.name + " is not a prefab, will convert");
         CreateNew(quads_Axial, localPath2);
     }






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 kayeejoe · Jul 31, 2018 at 10:50 PM 0
Share

Hello, can anybody help me with this problem? Or some code that can create prefab programmatically? Anything is appreciated.

2 Replies

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

Answer by kayeejoe · Aug 02, 2018 at 01:38 AM

I figured it out in another way. My purpose was to load images at runtime, AND can run in as more UWP environment (such as Hololens Emulator or Hololens) as possible. But the system default folders are not same when the hardware is not same. Here let me introduce another way.


To be clearer, the Resouce.load function will never return a file by using filename as its argument in the Editor mode. In fact, Resouce.load method is somehow zigzag comparing with a batch 'dragging' method, which is an operation that allows you to drag multiple images (textures) to a script, in where you can use an array of textures to feed your gameObjects. If you do not understand what I am talking, please see this link: link text. Remember you need to lock the inspector where you will drag the images into!

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
avatar image
0

Answer by Zhiyong_Chen · Jul 27, 2018 at 02:58 AM

Should localPath2 add .prefab?

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 kayeejoe · Jul 27, 2018 at 01:54 PM 0
Share

Actually the prefab file was generated by this code, just without correct materials (sometimes pink or black). So I guess the problem would be I did not load the images correctly.

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

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

assign texture to asset material via script 1 Answer

How to share material between meshes 1 Answer

How can you duplicat folders and not share old dependencies? 1 Answer

sharedMaterial not working after instantiate 0 Answers

Will using Resources.LoadAsync() to load Prefabs reduce loading times in comparison to just referencing Prefabs in the Inspector? 2 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