Skip to content

Instantly share code, notes, and snippets.

@takashicompany
Created March 25, 2014 16:34
Show Gist options
  • Save takashicompany/9765792 to your computer and use it in GitHub Desktop.
Save takashicompany/9765792 to your computer and use it in GitHub Desktop.
Unityでシーン内のGameObjectの共通化をInterfaceで行った例。
// 共通化のインターフェース
public interface IMyButton
{
void Press ();
}
// IMyButtonを実装したクラス
public class StartButton : MonoBehaviour, IMyButton
{
public void Press ()
{
// Press Function...
}
}
// IMyButtonを実装したクラス
public class SelectButton : MonoBehaviour, IMyButton
{
public void Press ()
{
// Press Function...
}
}
public class View : MonoBahaviour
{
public IMyButton startButton;
public IMyButton selectButton;
void Awake ()
{
// IMyButtonインターフェースはMonoBehaviourとは何も関係がないため、
// gameObjectのプロパティがないため、コンパイルエラーが起きる。
startButton.gameObject.SetActive(false);
selectButton.gameObject.SetActive(false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment