First describe the project I'm doing, it's a parkour-like audio tour.
Then the running cool class sound travel in the drawing runway, must consider the different brick display question. Let's say I have a rhythm list, so how do we put different stickers on different bricks?
It took me hours to figure out what was going on inside and listen to me slowly.
First we create a map, create a new empty Object on it, and divide all the road bricks into this group.
As follows:
We got some precast blocks, and then we're going to dye them.
The first question is, how do we traverse these bricks? Very simple, with tag. We'll make all the bricks a special tag, "Road".
Next we look for a gameobject and add what we need in its start function. The object that satisfies the requirement can be an ambient light, or it can be any empty project.
I created a new C # script that texture render, and then we use this function to get all the bricks:
var roads = Gameobject.findgameobjectswithtag ("road");
The next roads variable is a list whose members are all the gameobject of the tag road. And, more coincidentally, it is in order, so that we can easily traverse all the bricks sequentially from beginning to end.
For a question about the various variants functions of find, you can look at the document.
The next step is to find out which attribute texture is in. I don't know at the beginning what the map should be, and finally come to the conclusion that it should be assigned to this property:
Roads[i]. Getcomponent<renderer> (). material.maintexture
The old version of unity may be used directly
Renderer.material.mainTexture
But this usage is out of date.
The third step is how we correctly load the stickers in.
I looked through a lot of information and finally found that the Resource.load function could do the job.
First we put the required resource files into the Assets/resources folder in the root directory. If not, you need to create a new folder. Why is it? Because the path of the load () function is a relative address, it is found from the resource directory. Be sure to remember this pit.
Now I put some material in it, and then we call it that way.
Resources.load ("rocka" as texture2d;
As plays a role in type conversion. We can also write this.
(texture2d) Resources.load ("rocka");
Next, the loaded resource is assigned to the property of object.
Roads[i]. Getcomponent<renderer> (). Material.maintexture = Resources.load ("rocka"as texture2d;
Next, we'll write a traversal on it.
var roads = Gameobject.findgameobjectswithtag ("road"); for (int0; i < roads. Length; i++) { roads[i]. Getcomponent<Renderer> (). Material.maintexture = Resources.load ("grassb" as texture2d; }
Only one material is used here, and the final rendering is the result:
So we can do it. Dynamically loading the map.
As I mentioned before, this is a music game. I store the rhythm in list form through my own rhythm algorithm.
Then I just need to write a conversion algorithm, the corresponding bricks for special bricks, so that it is easy to load a variety of music.
P.S. Debug.Log () is really good, in unity, this feature basically replaces the breakpoint function and variable monitoring.
Unity uses scripts for bulk dynamic load mapping