Pro_Unit

Unity Inteface Segregation Example

Jan 15th, 2019
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.33 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3.  
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. namespace Test
  7. {
  8.     public class TextsSetter : MonoBehaviour
  9.     {
  10.  
  11.         private Text _textComponent = null;
  12.         public Text textComponent { get { if (_textComponent == null) _textComponent = GetComponent<Text> (); return _textComponent; } }
  13.  
  14.         [SerializeField] GameEventIMesagable _Event;
  15.         [SerializeField] MesagableOne mesagableOne;
  16.         [SerializeField] MesagableTwo mesagableTwo;
  17.         public void OnGetMessable (IMesagable mesagble)
  18.         {
  19.             textComponent.text = mesagble.Message;
  20.         }
  21.  
  22.         [ContextMenu ("SendOne")]
  23.         void SendOne ()
  24.         {
  25.             _Event.Raise (mesagableOne);
  26.         }
  27.  
  28.         [ContextMenu ("SendTwo")]
  29.         void SendTwo ()
  30.         {
  31.             _Event.Raise (mesagableTwo);
  32.         }
  33.  
  34.     }
  35.  
  36.     public interface IMesagable
  37.     {
  38.         string Message { get; }
  39.     }
  40.  
  41.     [System.Serializable]
  42.     public class MesagableOne : IMesagable
  43.     {
  44.         public string message;
  45.         public string Message { get { return message; } }
  46.     }
  47.  
  48.     [System.Serializable]
  49.     public class MesagableTwo : IMesagable
  50.     {
  51.         public string message;
  52.         public string Message { get { return message; } }
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment