状态系统
- 原理
- 食用方法Demo
原理
食用方法Demo
导入包👈, 通过字符串调用扩展方法。
每个物体都创建了一个状态,并且加入了两个随机的分组中,通过id更新状态值。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace ZYF
{public class StateSysDemo : MonoBehaviour{[SerializeField]private string stateId;[SerializeField]private ZYF_StateSystem.State<bool> state;[SerializeField]private List<ZYF_StateSystem.State<bool>> stateGroup;[SerializeField]private List<ZYF_StateSystem.State<bool>> stateGroup2;private void Start(){float randomInterval = Random.Range(1, 10);this.stateId= $"{gameObject.name}";this.state =stateId.GetOrCreateState<bool>();//初始化状态UpdateState();//加入状态组int randomGroup = Random.Range(1, 3);this.stateGroup= stateId.AddState2Group<bool>(groupId:$"测试组_{randomGroup}");randomGroup = Random.Range(4, 6);this.stateGroup2 = stateId.AddState2Group<bool>(groupId:$"测试组_{randomGroup}");//切换状态InvokeRepeating(methodName:nameof(SwitchActive),time:randomInterval,repeatRate: randomInterval);}private void UpdateState(){UpdateState(gameObject.activeInHierarchy);}private void UpdateState(bool state){//更新状态值stateId.UpdateState<bool>(state);}void SwitchActive() {var newState = !gameObject.activeInHierarchy;gameObject.SetActive(newState);UpdateState(newState);}}
}