Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by DougRichardson · Oct 23, 2017 at 07:51 AM · guicustom-editor

How to display texture field with label on top like the "Add Terrain Texture" window?

How do I display a texture with label on top like in the "Add Terrain Texture" window?

I've tried various combinations of EditorGUILayout.ObjectField in vertical and horizontal layout groups but the result is either the small texture picture (like in the material editor) or the large picker but with the label to the left rather than on top.

Here's what I want:

alt Add Terrain Texture Selection

capture.png (10.1 kB)
Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dan_wipf · Oct 23, 2017 at 09:14 AM 0
Share
 public Texture2D backgroundImage;
 
 OnGUI()
 { 
 backgroundImage = (Texture2D) EditorGUILayout.ObjectField("Image", backgroundImage, typeof (Texture2D), false); 
 }



maybe this works?

avatar image DougRichardson dan_wipf · Oct 23, 2017 at 09:23 AM 0
Share

That displays the image, but with the label to the left. I want the label on top like the add terrain texture window.

avatar image dan_wipf dan_wipf · Oct 24, 2017 at 12:15 PM 1
Share

you want this in the inspector as a component or for a custom Editor?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEditor;
 
 [CustomEditor(typeof(YourScript))]
 [CanEdit$$anonymous$$ultipleObjects]
 
 public class CustomInspectorScript : Editor {
 
 
     private YourScript yourimage;
     private GUIStyle m_box;
 
 
 
     void Awake()
     {
         yourimage = (YourScript)target;
     }
 
     public override void OnInspectorGUI()
     {
 
 
 
         GUILayout.BeginHorizontal ();
         GUILayout.Space (5);
         yourimage.yourscript.objectfieldclass = (Texture2D) EditorGUILayout.ObjectField (yourimage.yourscript.objectfieldclass,typeof(Texture2D), false, GUILayout.Width(70), GUILayout.Height(70));
         GUILayout.EndHorizontal ();
 
 
         GUILayout.BeginVertical ();
         GUILayout.Space (5);
         GUILayout.BeginHorizontal ();
         GUILayout.Space (5);
         EditorGUILayout.LabelField ("Your Image");
         GUILayout.EndHorizontal();
         GUILayout.Space (5);
         GUILayout.EndVertical ();
 
         if (GUI.changed) {
             EditorUtility.SetDirty (yourimage);
         }
     }
 }
 
avatar image DougRichardson dan_wipf · Oct 24, 2017 at 04:04 PM 0
Share

Thanks Dan. That wasn't quite it, but the GUILayout.Width and GUILayout.Height calls you mentioned pointed me in the right direction.

avatar image Bonfire-Boy · Oct 24, 2017 at 08:59 AM 0
Share

I've not done anything with Terrains but it looks like you're making an EditorWindow here (n which case you can set the title when you call GetWindow() to create it).

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by DougRichardson · Oct 24, 2017 at 04:22 PM

Based on the answer and comments from @dan_wipf, I was able to get the look I wanted. The TextureField function below renders an individual texture field that has a label on top.


 private static Texture2D TextureField(string name, Texture2D texture)
 {
     GUILayout.BeginVertical();
     var style = new GUIStyle(GUI.skin.label);
     style.alignment = TextAnchor.UpperCenter;
     style.fixedWidth = 70;
     GUILayout.Label(name, style);
     var result = (Texture2D)EditorGUILayout.ObjectField(texture, typeof(Texture2D), false, GUILayout.Width(70), GUILayout.Height(70));
     GUILayout.EndVertical();
     return result;
 }


To display a set of 4 textures horizontally:


 EditorGUILayout.BeginHorizontal();
 TextureField("Texture 1", texture1);
 TextureField("Texture 2", texture2);
 TextureField("Texture 3", texture3);
 TextureField("Texture 4", texture4);
 EditorGUILayout.EndHorizontal();


alt text

One of the problems I was running into is that I was previously using EditorGUILayout.LabelField, but that caused too much space between each of the textures. The reason for this is that EditorGUILayout.LabelField actually renders 2 labels (that's it's purpose in life). I didn't realize this and what I actually needed with GUILayout.Label, which only renders 1 label.


capture.png (7.4 kB)
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AdmiralThrawn · Oct 28, 2017 at 05:25 PM 0
Share

Thanks for sharing your solution, mate!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

126 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

I have a custom editor for a class which work's fine, but how do i refrence it in another class and get it ti appear? 1 Answer

EditorGUI and method create 0 Answers

Error with GUI.ObjectFeild() 0 Answers

Custom Window array inside custom class 1 Answer

Projecting 3D positions on Screen? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges