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 michel83 · Feb 07, 2017 at 05:38 PM · textureprefabinstanceclonesubmesh

Instantiate a prefab and access sub-mesh material

Hélo,

Unity noob here, I may have started with the wrong project :)

I'm trying to create a small lighting simulation app. My projector is made of several meshes. I made a "Projector" prefab that I can instanciate with an Empty Object containing the following script :

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class projector_instancer : MonoBehaviour {
 
     public GameObject proj_prefab;
     private GameObject proj_array;
 
     // Use this for initialization
     void Start ()
     {
         for (float x = 0; x < 10; x++) 
         {
             for (float y = 0; y < 10; y++) 
             {
                 // Create the clones
                 proj_array = Instantiate (proj_prefab, new Vector3 (x * (float)1.5, y * (float)1.5, 0), Quaternion.Euler(90,0,0));
 
                 // Rename the clones
                 proj_array.name = "Projectorl";
             }
         }
     }
 }

"proj_prefab" is supplied via Inspector, and contains the meshes making my projector.

Ok, now the tricky part. I would like each of the instantiated prefab to receive two textures, each one on one of the mesh they are made of (these projectors are actully LED matrices on a moving head). Later they also have to be stretched/translated so that each projector displays the part of the texture it's supposed to display.

Thing is : I have no idea how I should provide those textures to my prefab. I guess I'm lacking some Unity knowledge...

  • should I create a Material public var in my Instancer script, and assign/offset/scale the texture in the same loop that instantiates ?

  • should I assign my material to my prefab and do the offset/stretch thingy in another script ?

I'm totaly lost here, any help would be greeeeeaaatly appreciated :)

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 hexagonius · Feb 07, 2017 at 10:44 PM 0
Share

the only variance you seem to be seeking is a different part of the same texture for all instances depending on their position.
this means the prefab can have the material assigned already.
only code you need then is: in the loop, get the meshrenderer of the current instance and change tiling and offset according to coordinates.
a more performance friendly and a little more advanced solution would be using $$anonymous$$aterialPropertyBlocks once you have that.

0 Replies

· Add your reply
  • Sort: 

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

114 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

Related Questions

Logic issue with clone's materials 0 Answers

Reference all instances of a prefab? 1 Answer

Add prefab to list based on object touched? 0 Answers

Simple Prefab Instantiate "(Clone)" Question 3 Answers

Locate _MainTex from a public shader and assign to instantiated prefab for GUI 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