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
1
Question by Jeston 1 · Sep 30, 2010 at 09:50 PM · editorprefabcustom

Custom Data structs & Inspector

Greetings Unity Community,

I am trying to do something a little advanced with my game, basically I am making a shooter which has 50+ permutations of guns. And my goal is to allow the designer to tweak all kinds of variables involving a gun. So my instict was to create a GunData class which represents all the fields for a gun.

[System.Serializable]
public class GunData : Object
{
    public string Name = "Unnamed";
    public float Force = 10.0f;
    public float FireRate = 0.05f;
    public float Damage = 5.0f;
    public float ReloadTime = 0.5f;
    public float Recoil = 1.0f;
    public float Accuracy = 1.0f;
    public float Range = 100.0f;
    public int BulletsPerClip = 40;
    public int Clips = 20;
    public int GunID;
    public Transform Mesh;
    private GunType _type;
//...
}

And so, this poses a problem as I need to know what all 50 guns will be as a player can have any two from a long array. So to make this data driven, I added a GunEditor to inspect these.... Which uses reflection and creates a field.

[CustomEditor(typeof(GunData))] public class GunEditor : Editor {

public void OnInspectorGUI() { Type targetType = target.GetType(); FieldInfo[] targetFields = targetType.GetFields(BindingFlags.Public | BindingFlags.Instance);

 foreach (FieldInfo field in targetFields)
 {
     EditorGUILayout.BeginHorizontal(); 
     EditorGUILayout.PrefixLabel(field.Name);

     field.SetValue(target, field.GetValue(target));

     EditorGUILayout.EndHorizontal(); 
 }

}

}

So this brings me to the real question... In my hiearchy I have a gameobject called Assets, which has a script GameData representing a singleton of all the objects that will be dynamically shifted around the scene alot. This includes a way for me to look up info in other scripts and to centralize it.

public class GameData : MonoBehaviour 
{
    public static GameData Instance = null;
    [SerializeField] public GunData[] GunData;
    // ...
}

My question is, I can see the array of Gun Data in the inspector when viewing my Assets node (GameData script component) but I can't find an easy way of figuring out how to populate the array with premade GunData's.

Anyone have suggestions ? Basically I want a pre-runtime array of gundata accessible through a GameData singleton, so that when a player does something like PickUp weapon it knows which prefab to instantiate among other things.

Comment
Add comment · Show 2
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 Orion 1 · Nov 18, 2010 at 02:08 PM 0
Share

Can't you just go to your array in the inspector, increase it's size and set up your GunData? Or do you mean reading the data from a file? Of course your CustomEditor will only kick in if the GunData is inspected on it's own. I also wonder why you need a custom inspector, as the default unity inspector should be totally sufficient for what you have.

avatar image Azound · Oct 12, 2011 at 04:37 AM 0
Share

I believe increasing the size of the array in the inspector only allows you to edit the individual items if they are structs, not classes. If they're classes it just allows you to drag and drop a pre-existing instance into the slot.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Brian-Kehrer · Dec 19, 2010 at 03:46 AM

For this, you should probably be using ScriptableObjects. They are assets that live in your project.

Comment
Add comment · Show 2 · 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 Azound · Oct 12, 2011 at 04:38 AM 0
Share

How do you create an instance of a ScriptableObject? Do I have to write completely custom code to create an instance and save it to disk?

avatar image jahroy · Oct 12, 2011 at 05:07 AM 0
Share

You can create the ScriptableObject class with a bunch of default values, then use an editor script to create instances of it all over the place. The instances can be edited individually.

To create an instance of a scriptable object from an editor script you do something like this:

 /* create multiple GunData objects named Gun1, Gun2, etc... */
 
 for ( var i = 0; i < 10; i ++ ) {
 
     var somePath : String = "Assets/Guns/Gun" + i + ".asset";
 
     var freshInstance = ScriptableObject.CreateInstance(GunData);
 
     AssetDatabase.CreateAsset(freshInstance, somePath);
 }

Here's how you might define a ScriptableObject with default values in javascript:

 class GunData extends ScriptableObject
 {
   var name   :  String = "Bop Gun";
   var power  :  float  =  20.4f;
 }


Thanks to the folks at Schell Games for an awesome presentation on ScriptableObjects at Unite 2011!

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

2 People are following this question.

avatar image avatar image

Related Questions

Extending "GameObject/Create Other" with own prefab 1 Answer

Saving its values through custom Editor 2 Answers

CustomEditor for TextureImporter? 2 Answers

Why my prefab is auto changing? 3 Answers

Color custom editor window ? 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