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 Genuigr · Jan 08, 2015 at 09:35 PM · editorserializationcustomcustom-inspectoritem

How to create a lightweight Item/Mineral/etc System

Hi people

I want to create a space game. There are asteroids and they contain minerals which I want to mine and sell... For now each asteroid is 'pure' meaning that it only contains one mineral type. I created several classes:

 [Serializable]
 public class Mineral {
 
     public string name;
     public int value;
 
     public Mineral() {
         name = "New Mineral";
         value = 0;
     }
 
     public override string ToString() {
         return "v:" + value;
     }
 
     public void OnGUI() {
         GUILayout.Label(name, EditorStyles.boldLabel);
         value = EditorGUILayout.IntField ("Value", value);
     }
 }



 public class Asteroid : MonoBehaviour {
 
     public Mineral mineral = null;
 
 }



 public class Pool {
     
     public static Dictionary<string, Mineral> minerals = new Dictionary<string, Mineral> ();
 
     public static void AddMineral(Mineral m) {
         minerals.Add (m.name, m);
     }
 
 }

Because I have no custom property drawers or something in the inspector for the asteroid there are fields for the name and value fields. But I don't want to create new mineral objects for each asteroid, I want to be able to select mineral objects from the Pool class. How do I do that? I assume I have to go with serialization and creating custom editors, because the mineral objects and the references to them in the asteroid objects have to exist in edit mode AND play mode. Any idea?

This is a more abstract and general questions, I don't need concrete code, except it helps you explaining :)

Comment
Add comment · Show 3
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 Victory Dan Greene · Jan 08, 2015 at 10:26 PM 0
Share

Is it that you want, say, Asteroids A, B, and C all to have references to the same instance of $$anonymous$$ineral, while Asteroids D and E have references to a different instance, Asteroids F, G, and H yet another, etc?

You could use an enumeration to do this, and set up your instances in a static property on the $$anonymous$$ineral class.

(Warning: haven't tried the following code... Call it conceptual pseudo-code.)

 public class $$anonymous$$ineral {
 
     public readonly string name ;
     public readonly uint value ;
 
     public $$anonymous$$ineral( string name, uint value ) {
         this.name = name ;
         this.value = value ;
     }
 
 
     // $$anonymous$$ineral.$$anonymous$$ind will be our comprehensive list of $$anonymous$$eral types
     public enum $$anonymous$$ind { Iron, Quartzite, Iridium, Dilithium }

     // registry will hold our "Platonic ideal" $$anonymous$$erals
     protected static $$anonymous$$ineral[] registry ;
     
     // initialize registry when loading class
     static $$anonymous$$ineral() {
         registry = new $$anonymous$$ineral[4] ;
         registry[ $$anonymous$$ind.Iron ] =      new $$anonymous$$ineral( name:"iron", value:"10" ) ;
         registry[ $$anonymous$$ind.Quartzite ] = new $$anonymous$$ineral( name:"quartzite", value:"3" ) ;
         registry[ $$anonymous$$ind.Iridium ] =   new $$anonymous$$ineral( name:"iridium", value:"36" ) ;
         registry[ $$anonymous$$ind.Dilithium ] = new $$anonymous$$ineral( name:"dilithium", value:"6000" ) ;
     }

     // get a $$anonymous$$eral in the registry
     static $$anonymous$$ineral Of$$anonymous$$ind( $$anonymous$$ind kind ) {
         return registry[ kind ] ;
     }
 
 }


 // Asteroid will have a public property referencing the type of $$anonymous$$eral
 public class Asteroid : $$anonymous$$onoBehaviour {
 
     public $$anonymous$$ineral.$$anonymous$$ind $$anonymous$$eral ;
 
 }
avatar image Genuigr · Jan 09, 2015 at 03:48 PM 0
Share

I like this idea! But is there a way to create the enums from unity itself? I think of a window that has a list of enums for example. Like a $$anonymous$$eral browser, kind of like the project tab.

I want to avoid having to hardcode them into the scripts, yes it works and isn't that hard, but I don't like it very much.

avatar image Victory Dan Greene · Jan 09, 2015 at 04:38 PM 0
Share

There's no way to create an enum from the Unity editor, no.

It sounds like you're looking to create a custom Editor. That's a lot of work, just to define a list...

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Glurth · Jan 09, 2015 at 12:51 AM

To make a list of options appear in the editor, I use an ENUM.
Once you have your list of enumerated values, create a public member of that type, and you will be presented with a drop down list in the editor. Each item on an Enumeration list is really just a "named number", and should be used like an integer in your code (which makes them great for working with arrays).

 public enum MineralType {NONE=0,GOLD=1,IRON=2,COPPER=3,KRYPTON=4};
 
 public Asteroid:MonoBehaviour
 {
     public MineralType composition;  // this looks like a drop down list in the editor,
                                      // showing all the values in the enum MineralType
 }



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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

editorguilayout.objectfield issue 1 Answer

How should I serialize data that is also editable in the Inspector? 2 Answers

Custom Editor for Monobehavior with custom property not retaining Gameobject references 0 Answers

Editable non-monobehaviour objects 1 Answer

Custom Inspector data serialization in JavaScript 0 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