Skip to content

Instantly share code, notes, and snippets.

@smallrice45
Last active April 17, 2016 10:10
Show Gist options
  • Save smallrice45/e1ba6a3dc567fb251067624c972d9bbb to your computer and use it in GitHub Desktop.
Save smallrice45/e1ba6a3dc567fb251067624c972d9bbb to your computer and use it in GitHub Desktop.
擴展 UGUI研究院之控件以及按钮的监听事件系统(五) 內文的Code
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.Events;
public class CharacterCustomizableCreateManager : MonoBehaviour {
public List<Toggle> sexToggleGroup;
public Slider hairSlider;
public InputField ageText;
void Start ()
{
foreach(Toggle elemet in sexToggleGroup.ToArray()){
EventTriggerListener.Get(elemet.gameObject).onClick = OnSexSelect;
EventTriggerListener.Get(elemet.gameObject).onSubmit = OnSexSelect;
}
EventTriggerListener.Get(hairSlider.gameObject).onFloatValueChanged = OnHairSelect;
EventTriggerListener.Get(ageText.gameObject).onStringValueChanged = OnAgeChange;
}
private void OnSexSelect(GameObject go){
Toggle goToggle = go.GetComponent<Toggle>();
if (goToggle.isOn){
switch (go.name){
case "MaleToggle":
currentCharacterData.sex = 0;
break;
case "FemaleToggle":
currentCharacterData.sex = 1;
break;
default:
currentCharacterData.sex = 0;
break;
}
}else{
goToggle.isOn = true;
}
}
private void OnHairSelect(GameObject go, float value){
Debug.Log(value);
}
private void OnAgeChange(GameObject go, string text){
Debug.Log(text);
}
}
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class EventTriggerListener : UnityEngine.EventSystems.EventTrigger{
public delegate void VoidDelegate (GameObject go);
public delegate void FloatDelegate (GameObject go, float delta);
public delegate void StringDelegate (GameObject go, string text);
public VoidDelegate onClick;
public VoidDelegate onDown;
public VoidDelegate onEnter;
public VoidDelegate onExit;
public VoidDelegate onUp;
public VoidDelegate onSelect;
public VoidDelegate onUpdateSelect;
public FloatDelegate onFloatValueChanged;
public StringDelegate onStringValueChanged;
public VoidDelegate onSubmit;
public VoidDelegate onScroll;
public VoidDelegate onMove;
private Slider m_Slider;
private InputField m_InputField;
private float cacheFloat;
private string cacheString;
private void Start(){
m_Slider = this.GetComponent<Slider>();
m_InputField = this.GetComponent<InputField>();
}
static public EventTriggerListener Get (GameObject go)
{
EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
if (listener == null) listener = go.AddComponent<EventTriggerListener>();
return listener;
}
public override void OnPointerClick(PointerEventData eventData)
{
if(onClick != null) onClick(gameObject);
}
public override void OnPointerDown (PointerEventData eventData){
if(onDown != null) onDown(gameObject);
}
public override void OnPointerEnter (PointerEventData eventData){
if(onEnter != null) onEnter(gameObject);
}
public override void OnPointerExit (PointerEventData eventData){
if(onExit != null) onExit(gameObject);
}
public override void OnPointerUp (PointerEventData eventData){
if(onUp != null) onUp(gameObject);
}
public override void OnSelect (BaseEventData eventData){
if(onSelect != null) onSelect(gameObject);
}
public override void OnUpdateSelected (BaseEventData eventData){
if(onUpdateSelect != null) onUpdateSelect(gameObject);
if (onFloatValueChanged != null){
if(m_Slider.value != cacheFloat){
onFloatValueChanged(gameObject, m_Slider.value);
}
cacheFloat = m_Slider.value;
}
if (onStringValueChanged != null){
if(m_InputField.text != cacheString){
onStringValueChanged(gameObject, m_InputField.text);
}
cacheString = m_InputField.text;
}
}
public override void OnSubmit (BaseEventData eventData){
if(onSubmit != null) onSubmit(gameObject);
}
public override void OnScroll (PointerEventData eventData){
if(onScroll != null) onScroll(gameObject);
}
public override void OnMove (AxisEventData eventData){
if(onMove != null) onMove(gameObject);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment