Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by Mrintoxx · Aug 02, 2020 at 06:50 PM · scriptableobjectcustomeditor

Show scriptable objects parameter into an array

Hi guys, I was tring to make a custom inspector for my project. but i encounter a problem in disposition of the fields.

What i have : alt text

What i wanted : I have an array with scriptable objects, each of them have propeties, and i whant to show them just below the array field. Just like that : alt text Sry for the draw it's not verry pretty but i hope you will understand what i've in my mind.

Thanks for help.

capture.png (49.5 kB)
capture2.png (54.4 kB)
Comment
Add comment · Show 1
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 Mrintoxx · Aug 02, 2020 at 11:02 PM 0
Share

Here's the code of the build manager and the editor

Build manager :

     public GameObject shopSlot;
     public Plant[] plants;
     public Transform slotsContent;
     public Plant plantToPlant;
 
     private void Start()
     {
         for (int i = 0; i < plants.Length; i++)
         {
             GameObject slot = Instantiate(shopSlot, slotsContent.position, Quaternion.identity);
             slot.transform.SetParent(slotsContent);
             slot.GetComponent<ShopSlot>().Setup(plants[i]);
         }
     }


and for the editor : public override void OnInspectorGUI() { base.OnInspectorGUI();

         Build$$anonymous$$anager build$$anonymous$$anager = (Build$$anonymous$$anager)target;
 
         for (int i = 0; i < build$$anonymous$$anager.plants.Length; i++)
         {
             CreateEditor(build$$anonymous$$anager.plants[i]).OnInspectorGUI();
         }
 
     }

For scriptableObject :

 public class Plant : ScriptableObject
 {
     public string nameLabel;
     public int cost;
     public Sprite icon;
     public GameObject plantPrefab;
 
 }

2 Replies

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

Answer by xxmariofer · Aug 02, 2020 at 08:29 PM

create an struct with the parameters and serialize that struct in the inspector and reference the struct from code

first you create your scriptable object classs

 [Serializable]
 [CreateAssetMenu(fileName = "Data", menuName = "ScriptableObjects/SpawnManagerScriptableObject", order = 1)]
 public class MyPlantScriptable : ScriptableObject
 {
     //fill your scriptableobject
 }

then your struct with the data and that scriptable object

 [System.Serializable]
 public struct Plant
 {
     public MyPlantScriptable plant;
     public string name;
     public int number;
 }

the final result alt text


captura.png (12.3 kB)
Comment
Add comment · Show 9 · 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 Mrintoxx · Aug 02, 2020 at 11:03 PM 0
Share

How can i do that i've never used struct.

   public struct _plant
     {
         public string _nameLabel;
         public Sprite _icon;
     }

Somethig like that i suppose but how can i reference it ?

Added my code in comment of the question.

avatar image xxmariofer Mrintoxx · Aug 03, 2020 at 06:11 AM 0
Share

add the labble in the top

  [System.Serializable]
  public struct _plant
  {
      public string _nameLabel;
      public Sprite _icon;
  }

and add a public _plant variable in your inspector manager

avatar image Mrintoxx xxmariofer · Aug 03, 2020 at 09:30 AM 0
Share

And i should use the struct instead the scriptable object ?

Show more comments
avatar image Mrintoxx · Aug 03, 2020 at 10:08 AM 0
Share

Yup, it's done but i don't have the result i wanted. alt text

is there a way to have each struc below the fiels in the array (it's not very important but better for lisibility) ?

capture.png (29.9 kB)
avatar image xxmariofer Mrintoxx · Aug 03, 2020 at 10:23 AM 0
Share

no, not like that, ill post it as an answer check it out, ill edit the first answer

avatar image Mrintoxx xxmariofer · Aug 03, 2020 at 10:41 AM 0
Share

Thank you so much fro help. i'm a bit annoying but is there a way to show the data stored in the scriptable object attached to the struct just below ? alt text

if not don't care, you helped me a lot :)

capture.png (30.0 kB)
Show more comments
avatar image
0

Answer by Mrintoxx · Aug 03, 2020 at 03:07 PM

Following the awnser of @xxmariofer i've made a struct who contains the scriptable object and then created a custom editor to show the properties of the scriptable object inside the array. But i can't access to the properties inside the scriptable object.

my editor script :

 public override void OnInspectorGUI()
     {
 
         // base.OnInspectorGUI();
         serializedObject.Update();
 
         BuildManager buildManager = (BuildManager)target;
 
         var shopSlotP = serializedObject.FindProperty("shopSlot");
         shopSlotP.objectReferenceValue = EditorGUILayout.ObjectField("Shop slot prefab", shopSlotP.objectReferenceValue, typeof(object), true);
 
         var slotContent = serializedObject.FindProperty("slotsContent");
         slotContent.objectReferenceValue = EditorGUILayout.ObjectField("Shop slot content", slotContent.objectReferenceValue, typeof(object), true);
 
         var plantsData = serializedObject.FindProperty("plantsData");
         EditorGUILayout.PropertyField(plantsData, true);
 
         for (int i = 0; i < plantsData.arraySize; i++)
         {
             var cost = serializedObject.FindProperty("plantsData").GetArrayElementAtIndex(i).FindPropertyRelative("cost");
             EditorGUILayout.PropertyField(cost);
         }
 
 
         serializedObject.ApplyModifiedProperties();
 
         //base.DrawDefaultInspector();
 
     }
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

136 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

Related Questions

Custom Editor for a scriptable object with an abstract list 2 Answers

Prevent double serialization of a custom class 0 Answers

Manually Saving ScriptableObject due to 2D Array... 0 Answers

Custom Editor Window not displaying anything 1 Answer

Problem with ScriptableObject and Custom Editor 2 Answers


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