- Home /
How can I create variables for each materials to show in inspector (Using Editor) ?
Hi, I have been trying to find my solution by making various searches on google first but couldn't get the answer I was looking for.
My problem is that I need a custom script that will show public variable that are added to a C# script via the editor.
To clarify: When adding the script 'CoreMats' to an object I want it to create new variables for each materials attached to this gameobject.
I do know how to get the amount of materials on the object and make the loop out of it But I still need to learn to create variables through the editor.
Right now: my Editor script create a button on the "CoreMats" script in the editor. This button calls a function in the CoreMats script that iterate through each materials attached. Like so:
 using UnityEngine;
 using System.Collections;
 using UnityEditor;
 
 [CustomEditor(typeof(CoreMats))]
 public class CoreMatsEditor: Editor 
 {
     public override void OnInspectorGUI()
     {
         DrawDefaultInspector();
         
         CoreMats myScript = (CoreMats)target;
         if(GUILayout.Button("Generate"))
         {
             myScript.CreateVariables();
         }
     }    
 }
This is the actual code I need to create new variables on.
 using UnityEngine;
 using System.Collections;
 using System;
 
 [Serializable]//I think this is required.
 
 public class CoreMats : MonoBehaviour {
 
     //Create variables in the inspector on this script if possible.
     public void CreateVariables () 
     {
         Material[] myMats = GetComponent<MeshRenderer>().materials;
         if (myMats[0] != null)//just making sure the user doesnt break anything...
         {
             for (int i = 0 ; i < myMats.Length; i++)// for each materials attached to this object
             {
                 //Here I need to create custom variables...
                 //The variables I want to create should appear in the inspector as public.
             }
         }
         else
         {
             Debug.LogError("No materials were found on '" + this.name + "' thus it created nothing");
         }
     }
 }
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                