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
0
Question by tykematt · Feb 22, 2017 at 08:31 AM · editorarray

Show Array Default Value in the Editor

Hi,

I have a simple object that has a Array that I want to contain one of each item in an enum by default.

How can I get the Array to show these default values in the Editor? for the most part they will not change but in a few cases (specifically while I'm testing the game) I want to change the values in the array in a few instances of the object.

Thanks for your help, Matt

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

1 Reply

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

Answer by Adam-Mechtley · Feb 22, 2017 at 08:46 AM

 using System;
 using System.Collections.Generic;
 using System.Collections.ObjectModel;
 using System.Linq;
 using UnityEngine;
 
 public enum MyEnum { FirstThing, SecondThing, ThirdThing }
 
 public class NewBehaviourScript : MonoBehaviour {
 
     static NewBehaviourScript()
     {
         List<MyEnum> defaults = new List<MyEnum>();
         foreach (var v in Enum.GetValues(typeof(MyEnum)))
             defaults.Add((MyEnum)v);
         s_DefaultEnumValues = new ReadOnlyCollection<MyEnum>(defaults.ToArray());
     }
 
     static readonly ReadOnlyCollection<MyEnum> s_DefaultEnumValues;
 
     [SerializeField]
     List<MyEnum> m_EnumValues = s_DefaultEnumValues.ToList();
 }
Comment
Add comment · Show 3 · 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 tykematt · Feb 22, 2017 at 09:22 AM 0
Share

This is perfect, exactly what I was after. Thanks.

I think I understand how this works. But in the interest of me developing my c# skills could you explain why it works?

It seems that assigning the value in the constructor allows the array/list to be populated before it is shown in the editor? Why do we have to give the static ReadOnlyCollection values and then assign this to the List?

avatar image Adam-Mechtley tykematt · Feb 22, 2017 at 09:45 AM 0
Share

There are a couple of ways to assign a default value to a field on a $$anonymous$$onoBehaviour or ScriptableObject:

  1. Using a field initializer (what I did here)

  2. Assign a value in an instance constructor for the $$anonymous$$onoBehaviour/ScriptableObject (if you do this, you should make the constructor protected so no one using your code is tempted to try to call it directly)

  3. Assign a value in the Reset() method for the $$anonymous$$onoBehaviour/ScriptableObject (this only gets called in the editor and would not happen if you e.g., add the component at run-time).

The reason I get the default values in the static constructor (not to be confused with an instance constructor in e.g., option 2 above) is because the values of this enum will never change, so i only want to obtain these default values one time, the first time this class is used. The reason I store them in a ReadOnlyCollection as opposed to e.g., a list or array is to prevent any unintended modifications to these default values at any later point from within the NewBehaviourScript class.

avatar image tykematt Adam-Mechtley · Feb 22, 2017 at 09:56 AM 0
Share

Awesome. Thanks for taking the time to explain that to me, really appreciate it!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Serialize [,] 1 Answer

Getting the Tag Array 2 Answers

How do I delete or empty an array of custom structs during edit mode? 1 Answer

How can I recreate the Array Inspector element for a custom Inspector GUI? 7 Answers

Is there a way to remove array entries in the editor? 4 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