uotsab / assign-material-unity

A easy and straightforward tool for assigning materials to an object in C# for Unity

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Introduction:

A easy and straightforward tool for assigning materials to an object in C# for Unity

image

Will assign you one material to multiple (Or hundrades+) objects without dragging and dropping the material to one by one of those objects.

Getting it to Unity:

Make sure you're in the github page of this asset https://github.com/uotsab/assign-material-unity

1.

image

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:)

image

2.

image

Extract with WinRar software

3.

image

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.

image

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.

Opening the window:

This is how you'll open material assigner inside unity:

  1. At top, open the Unity's strip menu item "Window"
  2. Hover mouse "Uotsab Windows"
  3. Lastly select "Material Assigner" window image

Now if things are alright, You would see the material assigner window for Unity

Example: 01 Assign green materials:

Make sure "Material Assigner" window is open. If not, so follow the avove parragraph to open it.

1. Select your desired Material. Green Material in case of the example here...

image

2. Click On "Material To Assign" in "Assign Material" window

image

3. Select your GameObject (Or Prefabs too!). Press "Objects to assign on"

image

4. Lastly magic, just click "Assign Material" and it should do!!!

image

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

Structure:

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.

image

About

A easy and straightforward tool for assigning materials to an object in C# for Unity

License:MIT License


Languages

Language:C# 100.0%