• 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 khoatic · Apr 22, 2014 at 04:37 AM · editorobjectserializationcustom-inspector

how can I create a new Object from custom Editor

So Im trying to have it so when I drag a object in a box in the inspector it Creates a new SoundObj, then adds that object to the SoundManager's Sounds list. In the inspector Im wanting it to show all SoundObj's in the SoundManagers Sound list along with the values to be changed such as name in the code i have below. The main issue im having is in the DropAreaGUI function of the inspector class. Im trying to create a new Sound Obj to add to the list but its not working. it seems like I can use the new operator here, but I dont know what else I should do instead. On a side note as well, i dont want to add more objects to the scene based on t$$anonymous$$s script. Id prefer to keep t$$anonymous$$s data soley in data, since thats mainly all its going to be used for. Please help I've been $$anonymous$$tting my head against a wall for the past couple days working on t$$anonymous$$s trying to figure it out.

//Inspector

     using UnityEngine;
     using System.Collections;
     using System.Collections.Generic;
     using UnityEditor;
     
     [CanEditMultipleObjects]
     [CustomEditor(typeof(SoundManager))]
     public class SoundInspector : Editor
 
 {
 
     SerializedObject mang;
     SerializedProperty name;
     SerializedProperty sounds;
     [HideInInspector]
     public List<SoundObj> list;
     public SoundManager TargetObject;
     public List<bool> objFold;
 
 
     public void OnEnable()
     {
         mang = new SerializedObject(target);//SoundManager
         name = mang.FindProperty("Name");//Test Param*can ignore*
         sounds = mang.FindProperty("Sounds");//SoundManager List
     }
     public override void OnInspectorGUI()
     {
         mang.Update();
         GUILayout.Space(10f);
         DropAreaGUI();
         GUILayout.Space(5f);
         GUILayout.Label("Sound Objects", EditorStyles.boldLabel);
         EditorGUILayout.BeginHorizontal();
         EditorGUILayout.PropertyField(name);
         EditorGUILayout.EndHorizontal();
 
         EditorGUILayout.BeginHorizontal();
         EditorGUILayout.PropertyField(sounds);
         for (int i = 0; i < sounds.arraySize; i++)
         {
             //Want to show the SoundObj objects the SoundManager list is holding
             //All params the SoundObj has as well
             SerializedProperty sound = sounds.GetArrayElementAtIndex(i);
             SerializedProperty sName = sound.FindPropertyRelative("name");
             EditorGUILayout.BeginHorizontal();
             EditorGUILayout.PropertyField(sName);
             EditorGUILayout.EndHorizontal();
         }
         EditorGUILayout.EndHorizontal();
         mang.ApplyModifiedProperties();
        
     }
     public void DropAreaGUI()
     {
         Event evt = Event.current;
         Rect drop_area = GUILayoutUtility.GetRect(0.0f, 50.0f, GUILayout.ExpandWidth(true));
         GUI.Box(drop_area, "Add FMODASSET Into Box");
 
         switch (evt.type)
         {
             case EventType.DragUpdated:
             case EventType.DragPerform:
                 if (!drop_area.Contains(evt.mousePosition))
                     return;
 
                 DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
 
                 if (evt.type == EventType.DragPerform)
                 {
                     DragAndDrop.AcceptDrag();
 
                     foreach (Object dragged_object in DragAndDrop.objectReferences)
                     {
                         FMODAsset asset = dragged_object as FMODAsset;
                         if (asset == null)
                         {
                             continue;
                         }
                         //Trying to create new SoundObj, then put in SoundManager List
                         //From Editor
                         SoundObj obj =  new SoundObj()
                           obj.setName("Poop");
                           Debug.Log(obj.name);
                           sounds.InsertArrayElementAtIndex(0);
                           sounds.GetArrayElementAtIndex(0).objectReferenceValue = obj;
                           Debug.Log(sounds.GetArrayElementAtIndex(sounds.arraySize - 1).CountInProperty().ToString());
                           Debug.Log(sounds.GetArrayElementAtIndex(sounds.arraySize-1).GetProperty("name"));
                           mang.ApplyModifiedProperties();
                     }
                 }
                 Event.current.Use();
                 break;
         }
     }
 
 }


//SoundManager

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class SoundManager : MonoBehaviour {
 
     public List<SoundObj> Sounds;
     [SerializeField]
     private string Name;
 
     public void Update()
     {
         Debug.Log(Name);
     }
 }

//SoundObj

 using UnityEngine;
 using System.Collections;
 using FMOD.Studio;
 
 
 public class SoundObj : MonoBehaviour
 {
     public string name = "";
 }
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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

Editable non-monobehaviour objects 1 Answer

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

Custom Inspector data serialization in JavaScript 0 Answers

Select a readonly value from dropdown using PropertyDrawers 0 Answers

Access another objects property in an editor script 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