Tz2/Assets/Zion/Scripts/FlowStepController.cs

206 lines
6.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using DefaultNamespace.ProcessMode;
using Framework.ProcessMode;
using MotionFramework;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using static InterfaceManager;
/// <summary>
///
/// </summary>
public class FlowStepController : MonoBehaviour
{
public static FlowStepController Instance;
/// <summary>
/// 步骤集合
/// </summary>
public List<FlowStep> flowSteps = new List<FlowStep>();
/// <summary>
/// 子步骤集合
/// </summary>
public List<FlowSubStep> flowSubSteps = new List<FlowSubStep>();
/// <summary>
/// 步骤初始化
/// </summary>
public List<StepInit> stepInits = new List<StepInit>();
/// <summary>
/// 插件
/// </summary>
private ProcessManager processManager;
private void Awake()
{
Instance = this;
}
// Start is called before the first frame update
void Start()
{
//查找场景内所有
//Invoke(nameof(Init), 3f);
}
/// <summary>
/// 初始化流程
/// </summary>
public void Init()
{
processManager = MotionEngine.GetModule<ProcessManager>();
var steps = processManager.CurrentProcessCollection.Steps;
if (steps != null && steps.Count > 0)
{
//根据steps个数在本物体下生成挂有FlowStep脚本的物体并为上边的脚本赋值
for (int i = 0; i < steps.Count; i++)
{
// 创建一个新的子物体
GameObject newStepObject = new GameObject("FlowStep_" + i);
newStepObject.transform.SetParent(this.transform); // 将新物体设为当前物体的子物体
// 为新物体添加FlowStep脚本
FlowStep flowStepScript = newStepObject.AddComponent<FlowStep>();
// 为FlowStep脚本赋值
flowStepScript.stepNum = i + 1;
flowStepScript.currentStep = steps[i].StepDescription;
flowStepScript.isActivate = i == 0 ? true : false;
flowStepScript.isCompleted = false;
StepInit stepInit = FindObjectByName<StepInit>(steps[i].StepDescription);
if (stepInit!= null)
{
flowStepScript.stepInit = stepInit;
}
flowSteps.Add(flowStepScript);
if (i > 0 && flowSteps.Count > (i - 1))
{
flowSteps[i - 1].nextStep = flowStepScript;
}
LoadProcessStepDescription(steps[i], flowStepScript);
if (i == steps.Count - 1)
{
Invoke(nameof(LoadFlowSubSteps), 1f);
}
}
}
processManager.OnStepActionsCompleteEvent += OnStepCompleted;
processManager.OnCurrentActionCompleteEvent += OnCurrentComplete;
}
/// <summary>
///
/// </summary>
private void LoadFlowSubSteps()
{
for (int k = 0; k < flowSubSteps.Count - 1; k++)
{
flowSubSteps[k].nextStep = flowSubSteps[k + 1];
}
}
/// <summary>
/// 加载子项
/// </summary>
/// <param name="step"></param>
/// <param name="flowStep"></param>
public void LoadProcessStepDescription(ProcessStep step, FlowStep flowStep)
{
List<ProcessStepDescription> Actions= step.Actions;
for (int i = 0; i < Actions.Count; i++)
{
GameObject newStepObject = new GameObject(Actions[i].Title);
newStepObject.transform.SetParent(flowStep.transform);
newStepObject.SetActive(false);
// 为新物体添加FlowStep脚本
FlowSubStep flowSubStepScript = newStepObject.AddComponent<FlowSubStep>();
flowSubStepScript.stepNum = i + 1;
flowSubStepScript.currentStep = Actions[i].Title;
flowSubStepScript.isCompleted = false;
StepInit stepInit = ReturnStepInit(Actions[i].Title);
if (stepInit != null)
{
flowSubStepScript.stepInit = stepInit;
Debug.Log(stepInit.name);
}
flowStep.flowSubSteps.Add(flowSubStepScript);
flowSubSteps.Add(flowSubStepScript);
newStepObject.SetActive(true);
flowSubStepScript.isActivate = (i == 0 && flowStep.stepNum == 1) ? true : false;
}
}
/// <summary>
/// 根据名称查找StepInit脚本
/// </summary>
/// <param name="title"></param>
/// <returns></returns>
public StepInit ReturnStepInit(string title)
{
StepInit stepInit = stepInits.Find(x => x.name == title && x.gameObject.activeSelf);
return stepInit;
}
/// <summary>
/// 处理步骤完成事件
/// </summary>
private void OnStepCompleted(ProcessStep step)
{
Debug.Log($"步骤{step.StepDescription}完成");
for (int i = 0; i < flowSteps.Count; i++)
{
if (flowSteps[i].currentStep == step.StepDescription)
{
flowSteps[i].isCompleted = true;
if (i + 1 < flowSteps.Count)
{
flowSteps[i + 1].isActivate = true;
}
}
}
// 在这里可以添加其他处理逻辑
}
/// <summary>
/// 处理当前动作完成事件
/// </summary>
/// <param name="step"></param>
public void OnCurrentComplete(ProcessStepDescription step)
{
Debug.Log($"<color=red>动作 {step.Title}完成 </color>");
for (int i = 0; i < flowSubSteps.Count; i++)
{
if (flowSubSteps[i].currentStep == step.Title)
{
flowSubSteps[i].isCompleted = true;
if (i + 1 < flowSubSteps.Count)
{
flowSubSteps[i + 1].isActivate = true;
}
}
}
}
/// <summary>
/// 需要加载的步骤
/// </summary>
public string title;
/// <summary>
///
/// </summary>
[ContextMenu("LoadCurrentComplete")]
public void LoadCurrentComplete()
{
for (int i = 0; i < flowSubSteps.Count; i++)
{
if (flowSubSteps[i].currentStep == title)
{
flowSubSteps[i].isActivate = true;
}
}
}
}