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 /
  • Help Room /
avatar image
0
Question by Daniil-Besedin · Oct 08, 2016 at 12:53 PM · c#editorlistcustom-inspectorcustom class

Custom Inspector for a list of custom class members.

I am trying to make an in-editor content creator for my strategy game. I thought it was the easiest way to do it rather then type XML or hardcode class instances; output it as XML and save. But unfortunately I ran into inability to edit list members' appearance and limit values to an extent I need.

My goal is to prevent MaxElevation and MaxHumidity from having lower values then MinElevation and MinHumidity respectively in each member. (From there I can draw analogy to apply to more tricky parts of other content generators)

My MonoBehaviour (do not mind the methods of BiomM) :

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System;
 
 public class BiomsListGenerator : MonoBehaviour
 {
     public List<BiomM> bioms;
 
     public void Load ()
     {
         bioms = new List<BiomM> ();
         foreach (string key in DataHub.Bioms.Keys) {
             bioms.Add (BiomM.BiomToBiomM (key, DataHub.Bioms [key]));
         }
     }
 
     public void Save ()
     {
         List<BiomContainer.BiomSaveFormat> biomsSF = new List<BiomContainer.BiomSaveFormat> ();
         foreach (BiomM biom in bioms) {
             biomsSF.Add (BiomM.BiomMToBiom (biom));
         }
         BiomContainer.Save (biomsSF);
     }
 }
 
  //All you need to know are the properties of that class.
 
 [System.Serializable]
 public class BiomM
 {
 
     public string name;
 
     public string officialName;
 
     [Range (0f, 1000f)]
     public float minElevation;
 
     [Range (0f, 1000f)]
     public float maxElevation;
 
     [Range (0f, 1000f)]
     public float minHumidity;
 
     [Range (0f, 1000f)]
     public float maxHumidity;
 
     public bool primary;
 
     public GameObject modelPrefab;
 
     public GameObject guiPrifab;
 
     public BiomM (string name, string officialName, float minElevation, float minHumidity, float maxElevation, float maxHumidity, bool primary, GameObject modelPrefabName, GameObject guiPrifabName)
     {
         this.name = name;
         this.officialName = officialName;
         this.minElevation = minElevation;
         this.minHumidity = minHumidity;
         this.maxElevation = maxElevation;
         this.maxHumidity = maxHumidity;
         this.primary = primary;
         this.modelPrefab = modelPrefabName;
         this.guiPrifab = guiPrifabName;
     }
     public static BiomContainer.BiomSaveFormat BiomMToBiom (BiomM bm)
     {
         string s1 = "";
         string s2 = "";
         try {
             s1 = bm.modelPrefab.name;
         } catch {
         }
         try {
             s2 = bm.guiPrifab.name;
         } catch {
         }
         return new BiomContainer.BiomSaveFormat (bm.name, new Biom (bm.officialName, bm.minElevation / 1000, bm.minHumidity / 1000, bm.maxElevation / 1000, bm.maxHumidity / 1000, bm.primary, s1, s2));
     }
 
     public static BiomM BiomToBiomM (string name, Biom biom)
     {
         return new BiomM (
             name,
             biom.officialName,
             biom.minElevation * 1000,
             biom.minHumidity * 1000,
             biom.maxElevation * 1000,
             biom.maxHumidity * 1000,
             biom.primary,
             Resources.Load (BiomContainer.modelPrefabFolderPath + biom.modelPrefabName) as GameObject,
             Resources.Load (BiomContainer.guiPrefabFolderPath + biom.guiPrifabName) as GameObject
         );
     }
 
 
     public static BiomM BiomToBiomM (BiomContainer.BiomSaveFormat b)
     {
         return BiomToBiomM (b.key,b.biom);
     }
 }
 

My Editor Script:

 using UnityEngine;
 using System.Collections;
 using UnityEditor;
 
 [CustomEditor (typeof(BiomsListGenerator))]
 public class  BiomsListGeneratorEditor: Editor
 {
 
 
     public override void OnInspectorGUI ()
     {
 
         BiomsListGenerator blg = (BiomsListGenerator)target;
 
         DrawDefaultInspector (); //what should replace this?
 
         if (GUILayout.Button ("Load")) {
             blg.Load ();
         }
         if (GUILayout.Button ("Save")) {
             blg.Save ();
         }
     }
 }
Comment
Add comment
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

2 Replies

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

Answer by Adam-Mechtley · Oct 10, 2016 at 07:57 AM

Could you achieve this using a PropertyDrawer for the BioM type?

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 Daniil-Besedin · Oct 10, 2016 at 03:44 PM 0
Share

Yes! Thank you very much! I just overlooked that class because I focused too much on word "Editor". Solution is below if you are interested.

avatar image
0

Answer by Daniil-Besedin · Oct 10, 2016 at 03:38 PM

Here is the end result for those who are interested (new Editor Script):

 using UnityEngine;
 using System.Collections;
 using UnityEditor;
 using System.Collections.Generic;
 
 [CustomEditor (typeof(BiomsListGenerator))]
 public class  BiomsListGeneratorEditor: Editor
 {
 
     public Dictionary<int,bool> dictionary = new Dictionary<int, bool> ();
 
     public override void OnInspectorGUI ()
     {
         
         BiomsListGenerator blg = (BiomsListGenerator)target;
         serializedObject.Update ();
         SerializedProperty myIterator = serializedObject.FindProperty ("bioms");
         for (int i = 0; i < short.MaxValue; i++) {
             if (!dictionary.ContainsKey (i)) {
                 dictionary [i] = false;
             }
             if (i >= 3) {
                 dictionary [i] = EditorGUILayout.Foldout (dictionary [i], myIterator.FindPropertyRelative ("officialName").stringValue);
             }
             if (dictionary [i] && i >= 3) {
                 EditorGUILayout.PropertyField (myIterator);
                 EditorGUILayout.BeginHorizontal ();
                 if (GUILayout.Button ("Copy")) {
                     serializedObject.FindProperty ("bioms").InsertArrayElementAtIndex (i - 3);
                 }
                 if (GUILayout.Button ("Delete")) {
                     serializedObject.FindProperty ("bioms").DeleteArrayElementAtIndex (i - 3);
                 }
                 EditorGUILayout.EndHorizontal ();
                 EditorGUILayout.Space ();
                 EditorGUILayout.Space ();
             }
             if (!myIterator.Next (i < 3)) {
                 break;
             }
         }
         serializedObject.ApplyModifiedProperties ();
 
         if (GUILayout.Button ("Load")) {
             blg.Load ();
         }
         if (GUILayout.Button ("Save")) {
             blg.Save ();
         }
     }
 }
 
 
 [CustomPropertyDrawer (typeof(BiomM))]
 public class BiomMpd : PropertyDrawer
 {
 
     public override void OnGUI (Rect position, SerializedProperty property, GUIContent label)
     {
         float minElevation = property.FindPropertyRelative ("minElevation").floatValue;
         float maxElevation = property.FindPropertyRelative ("maxElevation").floatValue;
 
         float minHumidity = property.FindPropertyRelative ("minHumidity").floatValue;
         float maxHumidity = property.FindPropertyRelative ("maxHumidity").floatValue;
 
         property.FindPropertyRelative ("name").stringValue = EditorGUI.TextField (position, "Unique Name", property.FindPropertyRelative ("name").stringValue);
         property.FindPropertyRelative ("officialName").stringValue = EditorGUILayout.TextField ("Displayed Name", property.FindPropertyRelative ("officialName").stringValue);
 
         EditorGUILayout.BeginHorizontal ();
         minElevation = EditorGUILayout.FloatField (minElevation);
         EditorGUILayout.PrefixLabel ("Elevation");
         maxElevation = EditorGUILayout.FloatField (maxElevation);
         EditorGUILayout.EndHorizontal ();
 
         EditorGUILayout.MinMaxSlider (ref minElevation, ref  maxElevation, 0f, 1000f);
 
         EditorGUILayout.BeginHorizontal ();
         minHumidity = EditorGUILayout.FloatField (minHumidity);
         EditorGUILayout.PrefixLabel ("Humidity");
         maxHumidity = EditorGUILayout.FloatField (maxHumidity);
         EditorGUILayout.EndHorizontal ();
         EditorGUILayout.MinMaxSlider (ref minHumidity, ref  maxHumidity, 0f, 1000f);
 
         property.FindPropertyRelative ("primary").boolValue = EditorGUILayout.ToggleLeft (" Essential at the start of the game.", property.FindPropertyRelative ("primary").boolValue);
 
         property.FindPropertyRelative ("modelPrefab").objectReferenceValue = EditorGUILayout.ObjectField (
             new GUIContent ("In-Game Model Prefab", "Must be located in Assets/Resources/" + BiomContainer.modelPrefabFolderPath),
             property.FindPropertyRelative ("modelPrefab").objectReferenceValue, typeof(GameObject), false);
         property.FindPropertyRelative ("guiPrifab").objectReferenceValue = EditorGUILayout.ObjectField (
             new GUIContent ("GUI Representation Prefab", "Must be located in Assets/Resources/" + BiomContainer.guiPrefabFolderPath),
             property.FindPropertyRelative ("guiPrifab").objectReferenceValue, typeof(GameObject), false);
 
         property.FindPropertyRelative ("minElevation").floatValue = minElevation;
         property.FindPropertyRelative ("maxElevation").floatValue = maxElevation;
 
         property.FindPropertyRelative ("minHumidity").floatValue = minHumidity;
         property.FindPropertyRelative ("maxHumidity").floatValue = maxHumidity;
     }
 }

Comment
Add comment · 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

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

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

How to remove an item from a list of custom variables 1 Answer

Custom inspector. Pick one bool from a list. 1 Answer

c# foreach does not work 1 Answer

Custom Inspector changes not updating target in Edit Mode 1 Answer

InvalidOperationException: Operation is not valid due to the current state of the object 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