OperationDataBase.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. namespace ChivaXR.Op
  2. {
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using Sirenix.OdinInspector;
  7. /// <summary>
  8. /// 操作点类型
  9. /// </summary>
  10. public enum OperationDataType
  11. {
  12. Play, //播放 -激活后直接执行,结束后关闭激活
  13. Trigger, //监听触发类型 -监听触发
  14. Toggle, //选择状态类型 -开关(off/on)等状态
  15. Value //完成量完成程度 -0-1
  16. }
  17. /// <summary>
  18. /// 操作点状态
  19. /// </summary>
  20. public enum OperationDataStatus
  21. {
  22. InActive, //未激活
  23. Listening, //激活监听状态
  24. Playing, //激活播放状态
  25. }
  26. public enum OperationDisActiveMode
  27. {
  28. once,
  29. loop
  30. }
  31. public interface IDataState
  32. {
  33. //设置当前数据状态值 0=false;1=true
  34. void SetDataStateValue(float value);
  35. }
  36. public struct OperationEventArgs
  37. {
  38. public OperationDataBase opData;
  39. }
  40. public delegate void OperationDataEventHandle(object sender, OperationEventArgs e);
  41. /// <summary>
  42. /// 操作点数据基类
  43. /// </summary>
  44. public abstract class OperationDataBase : MonoBehaviour
  45. {
  46. private float opValue;
  47. [ReadOnly]
  48. [PropertyOrder(-2)]
  49. public string guid;
  50. [PropertyOrder(-2)]
  51. [LabelText("操作名称")]
  52. public string operationName;
  53. [PropertyOrder(-2)]
  54. [LabelText("操作描述")]
  55. [TextArea]
  56. public string operationDescription;
  57. [LabelText("操作模式")]
  58. public OperationDisActiveMode operationDisActioveMode = OperationDisActiveMode.once;
  59. public float OpValue
  60. {
  61. get
  62. {
  63. return opValue;
  64. }
  65. set
  66. {
  67. if (opValue != value)
  68. {
  69. opValue = value;
  70. OnValueChanged();
  71. CheckOperationDataDisActive();
  72. }
  73. }
  74. }
  75. [SerializeField]
  76. [HideInInspector]
  77. private OperationDataStatus operationDataStatus;
  78. public virtual OperationDataType OperationType { get; }
  79. [LabelText("操作点状态")]
  80. [ShowInInspector]
  81. [PropertyOrder(0)]
  82. public OperationDataStatus OperationStatus
  83. {
  84. get { return operationDataStatus; }
  85. set
  86. {
  87. if (operationDataStatus != value)
  88. {
  89. operationDataStatus = value;
  90. OperationDataStatusChanged(operationDataStatus);
  91. }
  92. }
  93. }
  94. /// <summary>
  95. /// 操作点激活状态改变
  96. /// </summary>
  97. public event OperationDataEventHandle OperationStatusChanged;
  98. public event OperationDataEventHandle OperationValueChanged;
  99. public void Update()
  100. {
  101. if (OperationStatus == OperationDataStatus.Listening)
  102. {
  103. PlayUpdata();
  104. }
  105. }
  106. public virtual void OnValueChanged()
  107. {
  108. OperationValueChanged?.Invoke(this, new OperationEventArgs() { opData = this });
  109. }
  110. public abstract void SetDataStateValue(float value);
  111. /// <summary>
  112. /// 当操作点状态改变时
  113. /// </summary>
  114. protected void OperationDataStatusChanged(OperationDataStatus status)
  115. {
  116. OperationStatusChanged?.Invoke(this, new OperationEventArgs() { opData = this });
  117. OnOperationDataStatusChanged(status);
  118. if (OperationStatus == OperationDataStatus.InActive)
  119. {
  120. if (OperationType == OperationDataType.Play || OperationType == OperationDataType.Trigger)
  121. {
  122. //关闭激活情况下不调用改变
  123. opValue = 0;
  124. return;
  125. }
  126. }
  127. if (OperationStatus != OperationDataStatus.InActive)
  128. {
  129. PlayOneShot();
  130. }
  131. }
  132. protected virtual void OnOperationDataStatusChanged(OperationDataStatus status) { }
  133. /// <summary>
  134. /// 激活时执行一次
  135. /// </summary>
  136. protected virtual void PlayOneShot() { }
  137. /// <summary>
  138. /// 激活后每帧执行
  139. /// </summary>
  140. protected virtual void PlayUpdata() { }
  141. protected virtual void CheckOperationDataDisActive()
  142. {
  143. switch (operationDisActioveMode)
  144. {
  145. case OperationDisActiveMode.once:
  146. OperationStatus = OperationDataStatus.InActive;
  147. break;
  148. case OperationDisActiveMode.loop:
  149. break;
  150. }
  151. }
  152. [PropertyOrder(1)]
  153. public bool isRegister = false;
  154. [HideIf("isRegister")]
  155. [Button("注册操作信息")]
  156. public void RegisterData()
  157. {
  158. if (string.IsNullOrEmpty(guid)) guid = System.Guid.NewGuid().ToString();
  159. isRegister = OperationManager.Instance.RegisterOpData(this);
  160. }
  161. [ShowIf("isRegister")]
  162. [Button("移除操作信息")]
  163. public void RemoveRegisterData()
  164. {
  165. isRegister = !OperationManager.Instance.RemoveOpData(this);
  166. }
  167. /// <summary>
  168. /// 关闭提示
  169. /// </summary>
  170. public virtual void CloseHint()
  171. {
  172. }
  173. /// <summary>
  174. /// 打开提示
  175. /// </summary>
  176. public virtual void OpenHint()
  177. {
  178. }
  179. /// <summary>
  180. /// 自动填写对应的信息
  181. /// </summary>
  182. [ContextMenu("自动填写名称及描述")]
  183. public void FillInInfo()
  184. {
  185. operationName = transform.name;
  186. operationDescription = transform.name.Split('_').Length <= 1 ? transform.name : transform.name.Split('_')[1];
  187. }
  188. }
  189. }