Search Unity

Editor Script for setting multiple DragAndDrop objects to an Array

Discussion in 'Scripting' started by Subliminalman, Jan 23, 2013.

  1. Subliminalman

    Subliminalman

    Joined:
    Sep 11, 2012
    Posts:
    47
    Hi all,

    I'm working on a simple tool so I can DragAndDrop multiple Objects to an Editor Window that will assign it to a Monobehaviour Object that I set in the same Window.

    Currently it works until I hit play then it removes all of the objects.
    I took a look at this blog post on by Tim Cooper http://blogs.unity3d.com/2012/10/25/unity-serialization/
    but it doesn't seem to work for me so far.

    Below is my editor script and two Monobehaviour classes that are used with it, all are C#.
    Please note this is my first time working with Editor Scripts.

    LevelArrayEditorWindow.cs
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4. using System.Collections;
    5.  
    6. public class LevelArrayEditorWindow : EditorWindow {
    7.    
    8.     private SerializedObject m_Object;
    9.     private Level level;
    10.     private SerializedProperty m_ObstaclesCount;
    11.     [MenuItem("Level Tools/Level Array")]
    12.     public static void ShowLevelArrayEditorWindow()
    13.     {
    14.         LevelArrayEditorWindow.GetWindow(typeof(LevelArrayEditorWindow));
    15.     }
    16.    
    17.     public void OnGUI(){
    18.        
    19.         EditorGUILayout.BeginHorizontal();
    20.         GUILayout.Label ("Level", EditorStyles.boldLabel);
    21.         level = (Level)EditorGUILayout.ObjectField(level, typeof(Level));
    22.         EditorGUILayout.EndHorizontal();
    23.  
    24.         GUILayout.Label ("Level Objects", EditorStyles.boldLabel);
    25.        
    26.         DropAreaGUI();
    27.     }
    28.    
    29.     private void DropAreaGUI()
    30.     {
    31.         Event evt = Event.current;
    32.        
    33.         Rect dropArea = GUILayoutUtility.GetRect(0.0f, 100.0f, GUILayout.ExpandWidth(true));
    34.         GUI.Box(dropArea, "Add Obstacles");
    35.        
    36.         switch(evt.type)
    37.         {
    38.         case EventType.DragUpdated:
    39.         case EventType.DragPerform:
    40.            
    41.             if(!dropArea.Contains (evt.mousePosition)){
    42.                 break;
    43.             }
    44.            
    45.             DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
    46.            
    47.             if(evt.type == EventType.DragPerform){
    48.                 DragAndDrop.AcceptDrag();
    49.                
    50.                 foreach(GameObject draggedObject in DragAndDrop.objectReferences)
    51.                 {
    52.                     GameObject go = (GameObject)draggedObject; 
    53.                     if(!go)
    54.                         continue;
    55.                    
    56.                     Obstacle obs = go.GetComponent<Obstacle>();
    57.                    
    58.                     if(!obs)
    59.                         continue;
    60.                    
    61.                     AddObstacle(obs);
    62.                 }
    63.             }
    64.            
    65.             Event.current.Use ();
    66.             break;
    67.         }
    68.     }
    69.    
    70.     private void AddObstacle(Obstacle _obs){
    71.         bool hasObs=false;
    72.         foreach(Obstacle ob in level.obstacles)
    73.         {
    74.             hasObs = ob.Equals(_obs);  
    75.         }
    76.         if(!hasObs){
    77.             level.obstacles.Add(_obs);
    78.         }  
    79.     }
    80. }
    81.  
    Level.cs
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4. using System;
    5. using System.Collections;
    6. using System.Collections.Generic;
    7.  
    8. [Serializable]
    9. public class Level : MonoBehaviour
    10. {
    11.     public string levelName = "";
    12.     public string levelTheme = "";
    13.     public int levelIndex = 0;
    14.     public Texture backgroundTexture;
    15.     [SerializeField]
    16.     public List<Obstacle> obstacles;
    17.     public List<Obstacle> powerUps;
    18.     public List<Star> endStars;
    19.     public Pet pet;
    20.     public AudioSource audioSource;
    21.    
    22.     private List<Obstacle> resetObstacles;
    23.    
    24.     private List<Star> resetEndStars;
    25.    
    26.     void Start ()
    27.     {
    28.        
    29.         if(backgroundTexture != null)
    30.         {
    31.             Debug.Log("SET THE TEXTURE");
    32.             Material sky = new Material (Shader.Find ("RenderFX/Skybox"));
    33.             sky.SetTexture("_FrontTex", backgroundTexture);
    34.            
    35.             RenderSettings.skybox = sky;
    36.         }
    37.        
    38.         resetObstacles = obstacles;
    39.         resetObstacles.AddRange(powerUps);
    40.     }
    41.    
    42.     public void ResetLevel()
    43.     {
    44.         foreach(Obstacle obs in resetObstacles)
    45.         {
    46.             obs.LevelReset();
    47.         }
    48.         pet.ResetPet();
    49.     }
    50. }
    51.  
    Obstacle.cs
    Code (csharp):
    1.  
    2. using System;
    3. using UnityEngine;
    4. using System.Collections;
    5. using SmoothMoves;
    6. [Serializable]
    7. public class Obstacle : TextureFunctionMonoBehaviour
    8. {
    9.     #region Attributes
    10.     public float rollProbability;
    11.    
    12.     [HideInInspector]
    13.     public float roll;
    14.     public bool isActive = true;//Used for determining whether or not the item should be on the stack
    15.     public bool isItem = false;
    16.     public bool isDestroy = false;
    17.     public int score;
    18.     public Transform petTransform;
    19.     public ParticleSystem particle;
    20.     public AudioSource audioSource;
    21.     public BoneAnimation boneAnimation;
    22.     public bool isObserver = false;
    23.     protected Transform startTransform;
    24.     #endregion
    25.    
    26.     // Use this for initialization
    27.     void Awake(){
    28.         startTransform = transform;
    29.     }
    30.     void Start () {
    31.         isActive = true;
    32.     }
    33.    
    34.     // Update is called once per frame
    35.     void Update () {
    36.     }
    37.    
    38.     public float Roll(){
    39.         roll = UnityEngine.Random.Range(0, rollProbability);
    40.         return roll;
    41.     }
    42.    
    43.     public virtual void OnDestroy(){
    44.         if(particle != null){
    45.             particle.enableEmission = true;
    46.             particle.Play ();
    47.         }
    48.         if(audioSource != null){
    49.             audioSource.Play();
    50.         }
    51.         isActive = false;
    52.         if(GameTypes.currentGameType != GameTypes.GameType.Endless){   
    53.             gameObject.active = false;
    54.         }
    55.     }
    56.    
    57.     public virtual IEnumerator OnDestroy(float waitLength){    
    58.         if(particle != null){
    59.             particle.enableEmission = true;
    60.             particle.Play();
    61.         }
    62.         if(audioSource != null){
    63.             audioSource.Play();
    64.         }
    65.         yield return new WaitForSeconds(waitLength);
    66.  
    67.         isActive = false;
    68.         if(GameTypes.currentGameType != GameTypes.GameType.Endless){   
    69.             Debug.Log ("Destroy the object");  
    70.             gameObject.active = false;
    71.         }
    72.     }
    73.    
    74.     public virtual void LevelReset(){
    75.         gameObject.active = true;
    76.         isActive = true;
    77.         transform.position = startTransform.position;
    78.         transform.rotation = startTransform.rotation;
    79.         transform.localScale = startTransform.localScale;
    80.     }
    81. }
    82.  
    I was hoping to get this done fairly quickly but I just can't get past this snag.
     
  2. holyjewsus

    holyjewsus

    Joined:
    Mar 7, 2011
    Posts:
    624
    I'm not sure about this, but the level object field is probably being reset as well?

    I think you'll need to retrieve the added objects from the level object, because on play the editor is reinitiated right?

    I liked prime31's videos on youtube on the custom editors, you might check those out.
     
  3. Subliminalman

    Subliminalman

    Joined:
    Sep 11, 2012
    Posts:
    47
    *Sigh* It was super simple.

    Not the code mind you but what I was writing the code for which was to assign multiple objects at once on an array in the inspector.
    Theres a lock on top of the inspector that keeps that object there.

    This was a good learning experience to say the least.

    Thanks for the tips on the vids though I'm going to look through those for future reference.