SlideShare a Scribd company logo
1 of 46
Download to read offline
EventSystemまわりの話
k_nagamine @ Jollystics Inc.
自己紹介
• 長峰慶三 @KzoNag
• Jollystics Inc.
• コンシューマ系ゲーム会社→現職
• Unity / Xamarin
• C#
今日話すこと
• EventSystem, InputModule, Raycaster
• IEventSystemHandler
• UnityEvent
*注意
• 個人の調査・経験・感覚で話します
• 正確じゃないこともあるかもしれません
• 間違ってたら教えてください
EventSystem
UIオブジェクトを作ると出てくるやつ
EventSystem
• http://docs.unity3d.com/ja/current/Manual/
EventSystem.html
• 入力をうまく処理してオブジェクトにイベン
トを送信する仕組み
入力制御といえばInputクラス
void Update()
{
if(Input.GetMouseButtonDown(0))
{
Debug.Log(“マウスの左ボタンが押された”);
}
}
Inputクラスを使うと管理が大変
• マウスとタッチの管理
• 入力の状態を自分で保持して管理
• どのオブジェクトがクリックされたか
• 3Dと2Dオブジェクトの違い
EventSystemは
そのあたりをいいかんじに
管理してくれる
EventSystemの構成
• EventSystem
• InputModule
• Raycaster
InputModule
• 毎フレーム入力(Inputクラス)を監視
• 特定の条件を満たしたらオブジェクトにイベントを発行

(例)クリックイベントなど
• デフォルトでStandalone,Touchがある

→マウスとタッチを気にしなくていい
• カスタムモジュールを実装可能

→Oculusなどの特殊な入力デバイスに対応するとか
Raycaster
• どのオブジェクトがイベントの対象になるか
• InputModuleから使用される
• GraphicRaycaster : UI, Canvasにアタッチ
• PhysicsRaycaster : Collider(3D), Cameraにアタッチ
• Physics2DRaycaster : Collider2D, Cameraにアタッチ
EventSystem
• 複数のInputModuleとRaycasterを保持・管理
• (選択中のオブジェクトの管理)
デモ
カスタマイズ
• InputModuleとRaycasterを実装して拡張可能
• たとえばMultiTouchInputModuleとか?
• デフォルトクラスのソースコードを参考に
• Unity 4.6 から利用できる UI での EventSystem をカス
タムする方法について調べてみた

http://tips.hecomi.com/entry/2014/09/25/233823
IEventSystemHandler
オブジェクトにイベントを
送信するって?
特定のオブジェクトに対してあるメソッド
を実行するように投げたい



(例)クリックされたら
OnClickメソッドを呼び出す
SendMessageがあるじゃん!
void Update()
{
if(targetがクリックされたら)
{
target.SendMessage(“OnClick”, target.name);
}
}
void OnClick(string name)
{
Debug.Log(name + “がクリックされたよ!”);
}
SendMessageこわい
• 文字列で指定

→呼び出し関係が分かりにくくて追いにくい

→変更に弱い

→パフォーマンス悪そう?
IEventSystemHandler
• SendMessageの問題点を解消
• インターフェースを利用した明示的な定義
IEventSystemHandler
• あるメソッドをもつインターフェースを定義
• イベントを受け取りたいコンポーネントでインター
フェースを実装
• 送信元は送信先オブジェクトとインターフェースを指
定してイベント送信
• インターフェースを実装するすべてのコンポーネント
が反応してメソッド実行
IEventSystemHandler
public interface ICustomEventHandler : IEventSystemHandler

{

void OnReceiveEvent (CustomEventData eventData);

}



public class CustomEventReceiver : MonoBehaviour,
ICustomEventHandler

{

public void OnReceiveEvent (CustomEventData eventData)

{

Debug.Log (“OnReceive”);

}

}
IEventSystemHandler


public class CustomEventSender : MonoBehaviour

{

public GameObject receiver;
void Send()

{

ExecuteEvents.Execute<ICustomEventHandler> (receiver, null, 

(_handler, _eventData) => 

{

_handler.OnReceiveEvent((CustomEventData)_eventData); 

} );

}

}

デモ
メッセージングシステム
• SendMessageは手軽だけど怖い
• IEventSystemHandlerは手間だけど安心
EventSystemでのイベント
• IEventSystemHandlerの仕組みを利用
• 多くのインターフェイスが用意されている
• インターフェースを実装したコンポーネント
を付ければイベントを受け取れる
デモ
UnityEvent
インスペクターで
メソッド指定
ありそうなパターン
• インスペクターでメソッド名を文字列指定
• SendMessageでメソッド呼び出し
public string clickMethod;
void Update()
{
if(targetがクリックされたら)
{
target.SendMessage(clickMethod, target.name);
}
}
SendMessageこわい
• 文字列で指定

→呼び出し関係が分かりにくくて追いにくい

→変更に弱い

→パフォーマンス悪そう?
こういうのある気がする!
UnityEvent
• C#のeventみたいなイメージ
• インスペクターで設定可能
• ソースからの追加・削除も可能
• UI関連で使われてる
UnityEvent
• UnityEventを継承したSerializableなクラスを定義
• 引数は4つまで
• 静的コール:インスペクターで指定した引数が渡
される
• 動的コール:コードで実行時の引数が渡される
UnityEvent
public class MouseEventExecuter : MonoBehaviour 

{

[System.Serializable]

public class MouseEvent : UnityEvent<Vector3>{}



public MouseEvent onMouseDown;

void Update () 

{

if (Input.GetMouseButtonDown (0)) 

{

onMouseDown.Invoke (Input.mousePosition);

}

}

}



デモ
利用例 “InputBehaviour”
• EventSystemからのイベントをもろもろ受け
取れるInputBehaviour
• 必要なメソッドだけoverrideして定義
• インスペクターで指定することも可能
デモ
まとめ
• EventSystemは入力をいいかんじで制御してく
れる仕組み
• EventSystem,InputModule,Raycasterで構成
• インターフェースベースのメッセージング
• UnityEventを利用したメソッド定義
まとめ
EventSystem,InputModule,Raycaster
IEventSystemHandler UnityEvent
汎用的に使える仕組み
利用
ドキュメント
• イベントシステム

http://docs.unity3d.com/ja/current/Manual/
EventSystem.html
• UnityEvent 

http://docs.unity3d.com/ja/current/Manual/
UnityEvents.html
• UI関連ソースコード

https://bitbucket.org/Unity-Technologies/ui
参考
• Unity 4.6 から利用できる UI での EventSystem をカスタ
ムする方法について調べてみた

http://tips.hecomi.com/entry/2014/09/25/233823
• SendMessageに変わる新しいメッセージシステム

http://tsubakit1.hateblo.jp/entry/2015/04/13/010645
• Unityの新GUI、UGUIのイベント制御について

http://tsubakit1.hateblo.jp/entry/2014/12/23/233000
発表資料
• スライド

http://www.slideshare.net/KeizoNagamine/
event-system
• ソースコード

https://github.com/KzoNag/
UnityEventSystem

More Related Content

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

EventSystemまわりの話@UnityFukuoka07