Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Created October 29, 2019 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsubaki/c3b85643e9732f5eaf3e505026163ce3 to your computer and use it in GitHub Desktop.
Save tsubaki/c3b85643e9732f5eaf3e505026163ce3 to your computer and use it in GitHub Desktop.
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());
}
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}!!!");
}
@tsubaki
Copy link
Author

tsubaki commented Oct 29, 2019

スクリーンショット_2019_10_29_17_01

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment