Advertisement
Guest User

Untitled

a guest
Apr 25th, 2020
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.42 KB | None | 0 0
  1. File Classes.cs
  2. ----------------------------------------
  3. using UnityEngine;
  4. using System;
  5.  
  6. [Serializable]
  7. public class BaseClass
  8. {
  9.     public float SomeValue;
  10.    
  11.     public virtual void SendMessage()
  12.     {
  13.         Debug.Log("There's nothing here!");
  14.     }
  15. }
  16.  
  17. [Serializable]
  18. public class ChildClass : BaseClass
  19. {
  20.     public string SpecialMessage;
  21.    
  22.     public override void SendMessage()
  23.     {
  24.         Debug.Log("Message for you Sir! " + SpecialMessage);
  25.     }
  26. }
  27. ----------------------------------------
  28. File: Beda.cs
  29.  
  30. using UnityEngine;
  31. using UnityEditor;
  32. using System.Collections.Generic;
  33.  
  34. public class Beda : MonoBehaviour
  35. {
  36.  
  37.     public List<BaseClass> myValues = new List<BaseClass>();
  38.    
  39.    
  40.     public void Start()
  41.     {
  42.         for(int i = 0; i< myValues.Count; i++)
  43.         {
  44.             Debug.Log(myValues[i].GetType().Name);
  45.         }
  46.     }
  47. }
  48.  
  49. #if UNITY_EDITOR
  50. [CustomEditor(typeof(Beda))]
  51. public class BedaEditor: Editor
  52. {
  53.     public override void OnInspectorGUI()
  54.     {
  55.         base.OnInspectorGUI();
  56.        
  57.         if(GUILayout.Button("Create 2 BaseClass and 2 ChildClass"))
  58.         {
  59.             var bedaScript = (Beda)target;
  60.            
  61.             bedaScript.myValues.Clear();
  62.            
  63.             bedaScript.myValues.Add(new BaseClass());
  64.             bedaScript.myValues.Add(new BaseClass());
  65.             bedaScript.myValues.Add(new ChildClass());
  66.             bedaScript.myValues.Add(new ChildClass());
  67.            
  68.             for(int i = 0; i < 4; i++)
  69.             {
  70.                 Debug.Log($"Item #{i} Type {bedaScript.myValues[i].GetType().Name}");
  71.             }
  72.         }
  73.     }
  74. }
  75.  
  76. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement