- Home /
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 this script. Id prefer to keep this data soley in data, since thats mainly all its going to be used for. Please help I've been hitting my head against a wall for the past couple days working on this 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 = "";
}