Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

EditorGUILayout and Materials

Discussion in 'Scripting' started by kinkersnick, Oct 12, 2011.

  1. kinkersnick

    kinkersnick

    Joined:
    Aug 22, 2011
    Posts:
    35
    Hi,

    Does anyone know how to add a field in an editor script where people can drag and drop materials in the inspector?
    There doesn't seem to be anything about it in the documentation... Is there a method for this?
    EditorGUILayout.MaterialField doesn't exist, why not?

    Thanks!

    S
     
  2. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
  3. kinkersnick

    kinkersnick

    Joined:
    Aug 22, 2011
    Posts:
    35
    Doesn't work unfortunately. GameObject and Material are different types, and it won't let you drag a material on to the GameObject field in the inspector if you set it up like you're suggesting.

    Seems very strange you can't just set a MaterialField property in GUILayout.
     
  4. scarpelius

    scarpelius

    Joined:
    Aug 19, 2007
    Posts:
    966
    Code (csharp):
    1.  
    2.         GUILayout.BeginHorizontal();
    3.         npc.mat = (Material)EditorGUILayout.ObjectField("material", npc.mat, typeof(Material));
    4.         GUILayout.EndHorizontal();
    5.  
    C# and npc.mat is the field in my class
    public Material mat;
     
    feyyd likes this.
  5. kinkersnick

    kinkersnick

    Joined:
    Aug 22, 2011
    Posts:
    35
    Aha!
    Thank you muchly. Using JS, so had to twiddle the code to get it right:

    Code (csharp):
    1. GUILayout.BeginHorizontal();
    2.                 var allowSceneObjectsP : boolean = !EditorUtility.IsPersistent(target);
    3.                 target.elementArray[i].particleMaterial = EditorGUILayout.ObjectField("Particle Material", target.elementArray[i].particleMaterial, Material, allowSceneObjectsP);
    4.                 GUILayout.EndHorizontal();
    But that did the trick. Hadn't clocked that I could set the System.Type in there.
    Thanks!
     
  6. feyyd

    feyyd

    Joined:
    Apr 23, 2014
    Posts:
    8
    I tried soooo many versions of the ObjectField syntax before I found this post. This did exactly what I wanted, the casting makes it non-intuitive.