A easy and straightforward tool for assigning materials to an object in C# for Unity
Will assign you one material to multiple (Or hundrades+) objects without dragging and dropping the material to one by one of those objects.
Make sure you're in the github page of this asset https://github.com/uotsab/assign-material-unity
1.
Download Zip file.
(You can also clone by the way. You'd need to have "Git" installed in your machine on that case. You'll copy the link like shown below:)
2.
Extract with WinRar software
3.
New folder has been created. That is the place WinRar extracted the project. The name of the folder is - assign-material-unity-main. Now, open it.
4.
Inside from the folder, drag and drop MaterialAssigner.cs in Unity Project Manager.
Now, Unity will automatically function the Script in the Editor strip menu.
This is how you'll open material assigner inside unity:
- At top, open the Unity's strip menu item "Window"
- Hover mouse "Uotsab Windows"
- Lastly select "Material Assigner" window
Now if things are alright, You would see the material assigner window for Unity
Make sure "Material Assigner" window is open. If not, so follow the avove parragraph to open it.
This assigned Green Material to all the objects that were selected. And the selected objects are colored green by the material. So now you saw multiple objects got the material assigned without of Dragging and Dropping one after one of the objects. Bravo!!!!!!
The project is kept as ssimple as possible for any programmers to understand. But here's yet a quick explaination of the Script:
The asset contains only and only one script file that does the job. It is "Material-Assigner.cs".
static void ShowWindow() { }
This Method creates the window
[MenuItem("Window/Uotsab Windows/Material Assigner")] -
Will set a path to Unity's strip menu to open the Material Assigner
GetWindow<MaterialAssigner>("Assign Material");
Makes Window of classs "MaterialAssigner"
if(GUILayout.Button("Material To Assign"))
{
foreach (Object obj in Selection.objects)
{
if(obj.GetType() == typeof(Material))
{
materialToAssign = (Material)obj;
}
}
}
Creates Button to assign material in materialToAssign variable
GUILayout.Label("2. Select objects to asssign material on.");
if(GUILayout.Button("Objects to assign on"))
{
objects.Clear();
foreach (Object obj in Selection.objects)
{
objects.Add(obj);
}
}
Creates Button to assign objects in "objects" List() variable
if(GUILayout.Button("Assign Material"))
{
if(objects == null)
{
if(Selection.objects == null)
{
Debug.LogError("There are no objects assigned and nothing is selected");
}
else
{
foreach(GameObject obj in Selection.objects)
{
objects.Add(obj);
}
}
}
foreach(GameObject obj in objects)
{
obj.GetComponent<Renderer>().material = materialToAssign;
}
}
GUILayout.Label("Selected material : " + materialToAssign.name);
Finally creates a button to assign "materialToAssign" material to all the objects in "objects" List<> variable.