Created
October 29, 2019 08:09
-
-
Save tsubaki/c3b85643e9732f5eaf3e505026163ce3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using UnityEngine; | |
public class MyComponent : MonoBehaviour | |
{ | |
[SerializeReference] List<IMyInterface> dataarray = new List<IMyInterface>(); | |
void Start() | |
{ | |
foreach( var item in dataarray) | |
item.Action(); | |
} | |
// リストに要素を追加 | |
[ContextMenu("Create/Data1")] | |
public void AddMyData1() => dataarray.Add(new MyData1()); | |
[ContextMenu("Create/Data2")] | |
public void AddMyData2() => dataarray.Add(new MyData2()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using UnityEngine; | |
public interface IMyInterface | |
{ | |
void Action(); | |
} | |
[Serializable] | |
public class MyData1 : IMyInterface | |
{ | |
public string Name; | |
void IMyInterface.Action() => Debug.Log($"My Name is {Name}"); | |
} | |
[Serializable] | |
public class MyData2 : IMyInterface | |
{ | |
public int Value1, Value2; | |
void IMyInterface.Action() => Debug.Log($"{Value1} + {Value2} = {Value1 + Value2}!!!"); | |
} |
Author
tsubaki
commented
Oct 29, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment